Note that there are some explanatory texts on larger screens.

plurals
  1. POActiveRecord, Sqlite3, and BusyException
    text
    copied!<p>I have a rails app configured with mixed mysql and sqlite3 db connections, and to associate specific models with sqlite3, i add an "establish_connection 'sqlite_db_config_name'" line to each class definition. </p> <p>When I try to save any sqlite3-connected model object individually, the save is successful, but when I try to save an object that is composed of other objects (via has_many), I get a BusyException. I have a feeling it's because each object has its own connection to the db and the top-level object locks the database then calls the member objects' save methods and they can't acquire the lock. </p> <p>I'm assuming there's a way to make this work and I'm using establish_connection improperly. </p> <p>Anyone else encounter this? </p> <p>database.yml config:</p> <p>dev:</p> <pre><code>development: adapter: mysql database: maindb username: root password: host: localhost sqlite: adapter: sqlite3 database: db/db.sqlite3 timeout: 15000 </code></pre> <p>model definitions:</p> <pre><code>class Foo &lt; ActiveRecord::Base establish_connection 'sqlite' belongs_to :bar end class Bar &lt; ActiveRecord::Base establish_connection 'sqlite' has_many :foo def addFoo(item) self.foos &lt;&lt; item end end class MysqlModel &lt; ActiveRecord::Base end </code></pre> <p>Other:</p> <p>Ruby 1.8.7<br> Rails 2.3.4<br> Ubuntu 10.04</p> <h2>Update:</h2> <p>I tried using inheritance to isolate the establish_connection statement in a single class, based on the rails doc's explanation that "This feature is implemented by keeping a connection pool in ActiveRecord::Base that is a Hash indexed by the class. If a connection is requested, the retrieve_connection method will go up the class-hierarchy until a connection is found in the connection pool.". But for some reason rails associated the subclasses of the sqlite-connected class with the default mysql connection. So I've given up on attempting a has_many/belongs_to relationship with sqlite, and de-normalized my models.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload