On Thursday, May 31, 2012 4:57:35 AM UTC-7, Mario Ruiz wrote:
>
> I'm using Sequel with Sinatra and I created a method to get the content
> from database, something like:
> def getContent(sql)
> require 'sequel'
> dsn={
> :adapter=>"oracle",
> :database=>"mydatabase",
> :user=>"myuser",
> :password=>"mypassword"
> }
> connection = Sequel.connect(dsn)
> res=connection.fetch(sql)
> connection.disconnect
> Sequel::DATABASES.delete(connection)
> return res
> end
>
>
Sequel.connect returns a Sequel::Database object (you probably shouldn't
store it in a variable named connection, since that has other meanings in
Sequel). Sequel::Database#fetch returns a Sequel::Dataset object (not a
result, so storing it in a variable named res is a bad idea).
Sequel::Database#disconnect disconnects any available connections.
Sequel::DATABASES.delete removes the Database object from the stored array
of databases. However, you are returning the Dataset object created by
fetch, which still has the Database object associated to it. When you call
a method on the Dataset object (e.g. #all, #each), it's going to cause a
query to the Database object, which is going to instantiate a new
underlying connection object (using ruby-oci8), and you are never cleaning
up that connection object.
I'm not sure exactly what you are doing with the dataset, but given your
variable naming, you probably just want to do:
res=connection.fetch(sql).all
In fact, I'd recommend you change your code to:
require 'sequel'
def getContent(sql)
dsn={
:adapter=>"oracle",
:database=>"mydatabase",
:user=>"myuser",
:password=>"mypassword"
}
Sequel.connect(dsn){|db| db.fetch(sql).all}
end
This uses the block form of connect, which takes care of disconnecting and
removing from Sequel::DATABASES.
Thanks,
Jeremy
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/sequel-talk/-/-oyGbTNGtpkJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sequel-talk?hl=en.