mysql-native: preview2

Steven Schveighoffer via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Thu Feb 2 06:08:19 PST 2017


On 2/2/17 1:50 AM, Suliman wrote:
> On Thursday, 2 February 2017 at 05:28:10 UTC, Nick Sabalausky wrote:
>> Made a couple more long-needed changes while I'm at it:
>>
>> https://github.com/Abscissa/mysql-native-experimental
>> Tag: v0.2.0-preview2
>>
>> - For better clarity, renamed `mysql.db.MysqlDB` to
>> `mysql.pool.MySqlPool`.
>>
>> - Package mysql.connection no longer acts as a package.d, publicly
>> importing other modules. To import all of mysql-native, use `import
>> mysql;`
>
> Thanks! Could you explain about pool. I googled about it, and still
> can't understand when it up new connections?
>
> How can I imagine what connection is? Because without it hard to
> understand what difference between connections with and without pool.
>
> Am I right understand that if I use pool I can create connection
> instance one time in DB class constructor end every new connection will
> be created on demand?

Just to answer some of this, because I had questions about the pool as well.

The ConnectionPool is a mechanism to allow a limited resource (or a 
resource you want to limit, depending how you look at it) to be pooled 
for use. It's basically a free-list. It also abstracts away the details 
of opening a connection (i.e. server ip, username, password). It's nice 
because it makes opening a connection cleaner and straightforward.

The resulting connection can only be used in one fiber at a time, and 
the connection pool itself can only be used in one thread ever (there is 
no sharing of connection pools between threads). The resulting 
connection struct is reference counted, so it automatically releases 
back to the pool when it goes out of scope.

One issue I had with MysqlDB (now MySqlPool) is that it doesn't allow 
you to actually put a limit on the connections. In other words, you 
can't say "only allow 50 simultaneous connections". The effect is that 
you save the connection initialization, but you can't put a hard cap on 
connections used in your thread. If you have 10,000 fibers running at 
once, then you may get 10,000 connections to the database that are left 
open. If each of those is a socket, you are consuming a lot of resources 
during down times.

Not to mention that the allocation of the 10,000th connection has to 
first go on a linear search through the 9,999 other connections to find 
an open one (this seems like it could be solved better with a 
linked-list freelist?).

See here: https://github.com/mysql-d/mysql-native/issues/74

BTW, I solved this by copying MysqlDB into my local project and adding 
the appropriate parameter to the constructor :)

-Steve


More information about the Digitalmars-d-announce mailing list