On Monday, August 27, 2012 5:33:28 PM UTC-7, Ravi wrote:
>
> ok, I think I am doing lot of "bad" things here ( may be because I started
> using Sequel a few days back)
>
You haven't explained why you need to disconnect existing connections. In
a well designed application/database configuration, you should not need to
do so.
> What I exactly want to do (may not be a good method to do)
> I want to perform 2-3 basic db operation:
>
> So created a class dbConnect as I specified earlier:
> class dbConnect
> def initialize(table_name)
> @DB = Sequel.connect("myswl:...", :max_connection=>3,
> :pool_sleep_time=>0.01, :pool_timeout=>30)
> @dataset = @DB[table_name]
> end
> def add(parameters)
> #perform insert query here @dataset.insert(parametrs)
> end
> #similarly update and fetch
> end
>
> I have other program which is multi-threaded and each thread (not a db
> thread) calls this class and perform certain query at the same time as
> follows:
>
> #for each thread in the code
> db = dbConnect.new(:test_table)
> db.add(list_of_param)
> db.update(list_of_param)
> ####
>
> Thats it.
>
This is a bad idea. For one, it probably leaks memory (since the database
object is cached in Sequel::DATABASES). Why can't you assign the
database/dataset object to a constant and just work with that? Why do you
want to create a new database/dataset for each thread?
> What action I can take now with what I understood is:
> Making the connection as SingleThreaded (or max_connection=>1) and
> disconnecting the database.
>
> class dbConnect
> def initialize(table_name)
> @DB = Sequel.connect("myswl:...")
> Sequel.single_threaded = true
> @dataset = @DB[table_name]
> end
>
> def disconnect
> @DB.disconnect #here I dont need synchronize if the connection is
> Single threaded..
> end
> end
>
> db = dbConnect.new(:test_table)
> db.add(list_of_param)
> db.update(list_of_param)
> db.disconnect
>
> I know it is not optimal (it is just a working code!). I really appreciate
> if you can correct or optimize it.
>
This has the same issues as the code above. I can't see a reason for it.
Do this instead:
# Do this exactly once when starting your app
DB = Sequel.connect("mysql:...")
# do this each time you want to add/update
DB[:test_table].add(list_of_param)
DB[:test_table].update(list_of_param)
If you must wrap it in a class:
# classes in ruby must start with a capital letter
class DbConnect
DB = Sequel.connect("mysql:...", :max_connection=>3,
:pool_sleep_time=>0.01, :pool_timeout=>30)
def initialize(table_name)
@dataset = DB[table_name]
end
def add(parameters)
@dataset.insert(parameters)
end
# similarly for update/fetch
end
You should not need to disconnect manually except in unusual circumstances.
Doing so is basically a workaround for a broken or poorly designed
database configuration. It's better handled by fixing the database
configuration if you have control over that.
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/-/DDZkqI6XetMJ.
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.