On Monday, August 27, 2012 1:51:15 PM UTC-7, Jeremy Evans wrote:
>
> On Monday, August 27, 2012 1:28:34 PM UTC-7, Ravi wrote:
>>
>> I think increasing the number of max_connection solved the isseue. I did 
>> not see any more "Too many connections" errors since past three days.
>>
>
> Good.
>  
>
>> Few more question: When I open a connection to db, does the connection 
>> close automatically. How the connection is handled by Sequel and what is 
>> its scope?
>>
>
> The connection stays open until:
>
> 1) A disconnect is detected on it.
> 2) The connection is disconnected manually via Database#disconnect.
>  
>
>> eg:
>> I have a following class dbConnect to connect to db:
>>
>> class dbConnect
>>   def initialize
>>     @db = Sequel.connect("myswl:...", :max_connection=>3, 
>> :pool_sleep_time=>0.01, :pool_timeout=>30)
>>   end
>>   def execute_query
>>     #perform some query here
>>   end
>> end
>> Can I define a function to close this particular (@db) connection or it 
>> will be handled by Sequel??
>>
>
> That's not a connection, that's a Database object (which has a pool of 
> connections).  If you really want a separate Database object and connection 
> pool just to execute one query, you can pass a block to Sequel.connect:
>
>    Sequel.connect("myswl:...", :max_connection=>3, :pool_sleep_time=>0.01, 
> :pool_timeout=>30) do |db|
>     # ...
>    end
>
> That's not generally a good idea, though.
>  
>
>>
>> Now, in another file:
>>
>>   def db_perform
>>      db = dbConnect.new
>>      db.exec_query
>>   end
>> db_perform
>>
>> Scope of this connection? Does the db connection automatically close at 
>> the end of the above function (db_perform)?
>>
>
> No, connections will remain in the connection pool so that they can be 
> used by other threads.  If you really want to control the scope of 
> connections, use the master branch, set :connection_handling=>:disconnect, 
> and use Database#synchronize to manually control the scope of each 
> connection.
>

ok, I think I am doing lot of "bad" things here ( may be because I started 
using Sequel a few days back)

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.

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.

Thanks,
Ravi

>
> 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/-/A65KhkS7yIoJ.
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.

Reply via email to