How to disconnect an existing ruby sequel connection to a database?
Asked Answered
C

1

5

I mean the one which was previously established as

DB = Sequel.sqlite('my_blog.db')

or

DB = Sequel.connect('postgres://user:password@localhost/my_db')

or

DB = Sequel.postgres('my_db', :user => 'user', :password => 'password', :host => 'localhost')

or etcetera.

The Sequel::Database class has no public instance method called "disconnect" or so though it has "connect" one.

Maybe somebody already faced that problem. I would appreciate any idea.

Colis answered 6/5, 2010 at 18:8 Comment(4)
Doesn't this work: sequel.rubyforge.org/rdoc/classes/Sequel/Database.html#M000332 It's not logical for the disconnect method to be class method BTW.Desirable
It works, but it disconnects all of the established connections instead of the one, although that method is of public instance kindColis
You're right, I meant public instance method. Corrected. ThanksColis
Which one? DB is a Database, not a Connection. Sequel's connection pools have disconnect method as well.Desirable
E
19

As Mladen Jablanović points out, you can just do:

DB.disconnect

Which will disconnect all of the available connections in that Sequel::Database instance's connection pool. You can't choose a specific connection to disconnect, and it wouldn't make sense to. The sharded connection pools do support disconnecting all connections for a specific shard, though.

Eelgrass answered 7/5, 2010 at 15:19 Comment(3)
After you told that wouldn't make sense, I started to doubt that I understand connection pools right. The following code assumes that multiple modules can simultaneously use specific connection from DB hash: [[[ DB = {}; DB[:a] Sequel.postgres('a', opts); DB[:b] Sequel.postgres('b', opts); DB[:c] Sequel.postgres('c', opts) ]]]. The task is in disconnecting all the DB[:a] connections, actually in releasing the specific database. Is the above approach right? Is that possible? DB[:a].disconnect doesn't work as desired, or I do misunderstand? BTW, many thanks for the toolkit!Colis
Sorry for the ugly message, I'm not yet unaccustomed to Stackoverflow's methods. The example code and clarifications should probably be in a question((Colis
You don't ever get to choose a specific connection to use. Assuming that DB[:a] is your Sequel::Database, DB[:a].disconnect will disconnect all available connections in DB[:a]'s connection pool. If you have more questions, you may want to post on Sequel's Google Group with the code you are using.Eelgrass

© 2022 - 2024 — McMap. All rights reserved.