On Wed, 24 Oct 2012 20:14:15 +0200
Sönke Ludwig sludwig@rejectedsoftware.com wrote:
Am 10/24/2012 6:59 PM, schrieb Nick Sabalausky:
On Wed, 24 Oct 2012 08:23:49 +0200
Sönke Ludwig sludwig@rejectedsoftware.com wrote:Am 10/24/2012 5:01 AM, schrieb Nick Sabalausky:
To avoid the cost of establishing a new connection to a MySQL DB
on potentially every page request, I tried making one connection
to the DB upon startup of my vibe.d app and then use that one
connection for each request. Worked fine until I tried making
concurrent page requests, at which point I (perhaps
unsurprisingly) got a message about the wrong fiber accessing a
connection and then it died with an InvalidMemoryOperationError.
(But annoyingly, I can't seem to reproduce it now.)So I realize that's probably not a smart thing to do anyway, to
share a single connection across fibers. But how would I go about
reusing DB connections when possible? By using ConnectionPool,
maybe? But I'm not quite sure how to use that.In my modified version of the mysql driver there is a trivial
MysqlDB class that uses a ConnectionPool.lockConnection()
returns a RAII struct that makes sure that a single connection is
not used twice.Usually I would also mirror the methods of the Connection class for
convenience, but since I don't really use it myself, I didn't put
so much time into it yet.https://github.com/rejectedsoftware/mysql-native/blob/master/source/mysql/db.d
So, if I'm understanding this right, ConnectionPool.lockConnection
creates and opens a new connection when the current fiber doesn't
already have an open connection, and returns an existing open
connection when the current fiber does have one?It keeps a pool of connections and picks an unused one + calls
acquire() (or creates a new one if no unused connection exists). When
it goes out of scope (or better when the ref count drops to zero), it
calls release() on the connection so other fibers may call acquire().So connections are always only temporarily pinned to a cetain fiber.
And I assume these unused connections can remain open while in the pool
and being aquired by another fiber, right? Sounds awesome and exactly
what I need :)