[Dbix-class] Multiple connections

Dave Howorth dhoworth at mrc-lmb.cam.ac.uk
Thu Jan 16 15:22:59 GMT 2014


Dave Howorth wrote:
> Peter Rabbitson wrote:
>> On Thu, Jan 16, 2014 at 11:06:14AM +0000, Dave Howorth wrote:
>>> I think I need to make two connections to the same MySQL database with
>>> different settings of the mysql_enable_utf8 option. But calling
>>> connection twice seems to result in a single connection.
>> connection() is not a proper way to connect to a database. See the 
>> source of connect() [1] for the immediatelly obvious answer (and by 
>> proxy an explanation of the problem you are experiencing).
> 
> Ah ha! Thank you, thank you.

Apparently I still don't understand enough to use this effectively. If I do

    my $schemaw = TDB::Schema->connect($dsn, $user, $password);

    $dbi_params = {
        mysql_enable_utf8 => 1,
        on_connect_do     => "SET NAMES 'utf8'",
    };

    my $schemau = TDB::Schema->connect($dsn, $user, $password,
                                       $dbi_params);

things work, but I don't have the dsn etc immediately available in the
place where I need to make the second call and if I replace the second
connect with

    my $schemau = $schemaw->clone($dbi_params);

things don't work. Is there some way to use clone? Or is there some way
to retrieve the connection info so I can reuse it?

I suppose I'm still interested in the answer to my original question:

Is there a good (authoritative?) description of how DBIx::Class manages
database connections? Including whatever is done by other packages
underneath.

>> Please let us know what made you use connection() as opposed to 
>> connect() - clearly the docs need adjusting.
> 
> Whatever it was is a long time ago and lost in the fog, sorry. I
> migrated from CDBI, if that might account for it.

Actually I've just noted a couple of places, both in
DBIx::Class::Manual::Cookbook

The first occurrence of the text 'connect' on the page is in the example
'Test File test.pl'

my $schema = My::Schema->connection('dbi:Pg:dbname=test');

and then in the section I was actually using:

MySQL

MySQL supports unicode, and will correctly flag utf8 data from the
database if the mysql_enable_utf8 is set in the connect options.

  my $schema = My::Schema->connection('dbi:mysql:dbname=test',
                                      $user, $pass,
                                      { mysql_enable_utf8 => 1} );

Cheers, Dave



More information about the DBIx-Class mailing list