Yes this behavior is fine, but why is the connection dropped after each
query? My stacktrace is like:
DB.with_server(params) do
DB[:table].insert(row1)
#connection released
DB[:table].insert(row1)
#connection released
DB[:table].insert(row1)
#connection released
end
lib/sequel/adapters/do.rb:
def execute(sql, opts=OPTS) synchronize(opts[:server]) do |conn| begin
command = conn.create_command(sql) res = log_connection_yield(sql,
conn){block_given? ? command.execute_reader : command.execute_non_query}
rescue ::DataObjects::Error => e raise_error(e) end if block_given? begin
yield(res) ensure res.close if res end elsif opts[:type] == :insert
res.insert_id else res.affected_rows end end end
lib/sequel/database/connecting.rb:
def synchronize(server=nil)
@pool.hold(server || :default){|conn| yield conn}
end
lib/sequel/connection_pool/sharded_threaded.rb: def hold(server=:default)
server = pick_server(server) t = Thread.current if conn =
owned_connection(t, server) return yield(conn) end begin conn = acquire(t,
server) yield conn rescue Sequel::DatabaseDisconnectError, *@error_classes
=> e sync{@connections_to_remove << conn} if conn && disconnect_error?(e)
raise ensure sync{release(t, conn, server)} if conn end end
A option like you described would be perfect for our problem, but if the
connection is hold inside the with_server block and is not disconnected
after a query we would not need such a feature.
Thanks,
Jannis
On 21 October 2016 at 16:06, Jeremy Evans <[email protected]> wrote:
> On Friday, October 21, 2016 at 6:56:40 AM UTC-7, Jannis Hübl wrote:
>>
>> Hello,
>>
>> we are using sequel to connect to different databases using the
>> DB#with_server method. While we are in the block every time we are doing a
>> database query sequel creates a new connection to the database.
>> This is because every connection is disconnected in the release method of
>> Sequel::ArbitraryServers, if a server is passed as Hash like it is in our
>> case. Also the connection is deleted from the cache.
>>
>> # If server is a hash, delete the thread from the allocated
>> # connections for that server. Additionally, if this was the last
>> thread
>> # using that server, delete the server from the @allocated hash.
>> def release(thread, conn, server)
>> if server.is_a?(Hash)
>> a = @allocated[thread]
>> a.delete(server)
>> @allocated.delete(thread) if a.empty?
>> disconnect_connection(conn)
>> else
>> super
>> end
>> end
>>
>> Is there a workaround to keep the connection? Or is this a bug?
>>
>
> Currently, no there isn't a way to keep the connection, and this is
> expected behavior, not a bug. If you use arbitrary servers, Sequel creates
> a new connection for each block, and disconnects at the end. Sequel does
> this because otherwise the connections would leak. If you plan to connect
> to the same servers on a regular basis, I would use real shards in the
> :servers option when creating the Database object.
>
> I suppose as a new feature, I would consider an option to cache the
> connection, via some key present in the connection options hash. The user
> would then be responsible for any connection leaks generated if they chose
> to use the feature.
>
> 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.
>
--
Jannis Hübl
Software Developer
metoda gmbh
------------------------------------
Telefon: +49 89 124 137 800
E-Mail: [email protected]
Internet: www.metoda.com
------------------------------------
Registernummer: HRB 209729
Registergericht: Amtsgericht München
USt-IdNr.: DE261171098
Geschäftsführer: Stefan Bures
------------------------------------
metoda gmbh | bayerstraße 69 | 80335 münchen
Please consider the environment before printing this e-mail. Thank you.
Follow us on XING <https://www.xing.com/companies/metodagmbh>, LinkedIn
<https://www.linkedin.com/company/preisanalytics-gmbh?trk=top_nav_home>,
Facebook <https://www.facebook.com/metoda-431720550341594/?ref=hl>, Twitter
<https://twitter.com/metodaDE> and Google
<https://plus.google.com/105002344648313517353/posts>
--
<http://info.metoda.com/metoda--researchresults>
--
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.