[odb-users] Configure MySQL client connection

Boris Kolpackov boris at codesynthesis.com
Thu Dec 31 02:13:32 EST 2015


Hi Aarón,

Aarón Bueno Villares <abv150ci at gmail.com> writes:

> First of all, thank you for your amazing library.

Thanks, I am glad you are enjoying it.


> What I would like to do is to configure some parameters of my MySQL
> connection, like `SET NAMES` or `SQL_MODE`, but, since each time I'm going
> to start a transaction, it catchs a possibly new connection, I understand
> that I should send a native SQL query inside each transaction.
> 
> [...]
> 
> Is there a way to specify these parameters globally for every connection?

There is not way to specify them, but there is a way to execute statement
for every newly created connection. See Section 17.3, "MySQL Connection and
Connection Factory", specifically, connection_pool_factory::create().


> A possible way to avoid that is using a same connection all the time
> (suppose, for simplicity, that my application runs just over the main
> process' thread) but I'm not sure about how to do that.

If you only perform database operations from a single thread (and you
don't obtain multiple connections explicitly), then the pool will create
just one connection (there is no reason to create more). So in this case
you could execute your "configuration statements" at the beginning and
be reasonably sure that it will be the same connection as the one used
by all the subsequent transactions. There is, however, a possibility that
the (network) connection to the database will be lost at some point. In
this case, the pool will try to automatically re-connect by creating a
new connection. If this happens, then with this approach the new connection
will be used "unconfigured". So the above approach (overriding create())
is more robust.

Boris



More information about the odb-users mailing list