Closing DataMapper DB connection
my rails application generates lots of small sqlite databases using DataMapper. After data saved, .sqlite-file must be uploaded on a remote server and destroyed locally.
My question is how to make DataMapper close .sqlite db connection and free repo's memory? Application should generate many databases, so it's important to save server resources.
Only way I googled is
DataObjects::Pooling.pools.each do {|pool| pool.dispose}
which is totally unacceptable for me I think because it seems to be closing all DataMapper connections, however few databases can be generated in parallel threads and I want to destroy DataMapper's repository too.
Sorry for开发者_如何学Python my English.
Also, DataMapper::Repository.adapters
is a hash of current Repository objects. You may be able to dig around in there to get at the connections.
I'm not aware of any nice way of doing this. This discussion is pertinent, however:
http://www.mail-archive.com/datamapper@googlegroups.com/msg02894.html
Apparently, it is possible to reopen a connection using DataMapper.setup()
, but it seems that the closing of connections is handled automatically.
However, maybe these observations will help:
It's possible to store a reference to the Adapter, e.g.
a = DataMapper.setup(:default, "sqlite:db/development.sqlite3")
Viewing this object shows that the path is stored, implying that it's for that particular connection, rather than a SQLite adapter in general, or such:
p a
#<DataMapper::Adapters::SqliteAdapter:0x00000001aa9258 @name=:default, @options={"scheme"=>"sqlite", "user"=>nil, "password"=>nil, "host"=>nil, "port"=>nil, "query"=>nil, "fragment"=>nil, "adapter"=>"sqlite3", "path"=>"db/development.sqlite3"}, @resource_naming_convention=DataMapper::NamingConventions::Resource::UnderscoredAndPluralized, @field_naming_convention=DataMapper::NamingConventions::Field::Underscored, @normalized_uri=sqlite3:db/development.sqlite3?scheme=sqlite&user=&password=&host=&port=&query=&fragment=&adapter=sqlite3&path=db/development.sqlite3>
Presumably, this can somehow be marked for garbage collection or something (will simply setting it to nil
work?).
There is also a close_connection()
method in DataMapper::Adapters::DataObjectsAdapter
, but it's protected, and I'm not sure whether or how this could be used.
Hope this provides some pointers!
精彩评论