On Tuesday, April 11, 2017 at 2:36:52 PM UTC-7, Clifford Hammerschmidt wrote: > > Hi, > > Does: > > Sequel.connect(API.configuration.database) > > Return a single connection, or a connection factory? I've been seeing > issues with postgresql that seem to indicate it is a single connection on > which a single command can be run at a time, but it has been suggested it > will "pool" the connections and new commands to the database will get new > connections automatically. >
A connection pool is used internally, but if a thread has a connection checked out and requests a connection, it always gets the same connection. > > > The error I get is: > > Sequel::DatabaseError: PG::UnableToSend: another command is already in > progress > > I suspect this is caused by streaming results while creating new commands > based on those results, e.g. > > query.each do |row| > do and update based on the row > end > > The Dataset#each documentation specifically warns against doing that: http://sequel.jeremyevans.net/rdoc/classes/Sequel/Dataset.html#method-i-each As I understand it, a db connections (generally) only supports one active > command; so while the query is streaming the results the "reader" command > is active. When the update command is attempted the "another command is > already in progress" would happen. To avoid this I've seen a pattern in > other languages that acquire a new connection from the pool for each > command to be issued and return it back to the pool after the command > finishes. From reading through the sequel postgres code I can't see > anywhere this would be happening automatically, and it appears to acquire > the connection from the pg driver just once. That would make me think the > caller would be responsible for wrapping each command in a new connection > to avoid this issue of stacking up commands on a single connection. > > Do I have that right, or am I missing something? > I think you have it correct. You should probably use Dataset#all instead of Dataset#each, or use the sharding support or a separate thread to force a separate connection if you do want to use Dataset#each. Thanks, Jeremy -- You received this message because you are subscribed to the Google Groups "sequel-talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/sequel-talk. For more options, visit https://groups.google.com/d/optout.
