end0tknr's kipple - web写経開発

太宰府天満宮の狛犬って、妙にカワイイ

DBIC(DBIx::Class) for perl を使って、AutoCommit=0な接続は、お作法違反らしい

package TestSchema;
use strict;
use warnings;
use base qw/DBIx::Class::Schema/;

my $DB_CONF =
    {db_name=>'rdstest',
     host=>'ないしょ.cbsplvtrcd5c.us-west-2.rds.amazonaws.com',
     port=>'3306',
     db_user=>'end0tknr',
     db_pass=>'ないしょ',
     db_opt=>
     {AutoCommit=> 0, #####ココ
      PrintError=> 0,
      RaiseError=> 1,
      ShowErrorStatement =>1,
      AutoInactiveDestroy=>1,
      mysql_enable_utf8  =>1,
      mysql_auto_reconnect=> 0}
    };

my $DB_STR = join(';',
                  "DBI:mysql:database=$DB_CONF->{db_name}",
                  "host=$DB_CONF->{host}",
                  "port=$DB_CONF->{port}");

__PACKAGE__
    ->connection($DB_STR,
                 $DB_CONF->{db_user},
                 $DB_CONF->{db_pass},
                 $DB_CONF->{db_opt});

__PACKAGE__->load_classes(qw/User/);

1;
__END__

#!/usr/local/bin/perl
use strict;
use lib qw/lib/;
use TestSchema;
use Data::Dumper;

main();

sub main {

    my $schema = TestSchema->connect();

    my $i = 0;
    my $result;
    while ($i++ < 5 ) {
        my $member = select_db($schema);
        print "$i UID:", $member->uid, "\n";
    }

}


sub select_db {
    my ($schema) = @_;

    my $member =
        $schema->resultset('User')->find({uid =>'user001'});
    return $member;
}

と書いて、呼んでみたら、

$ ./test_dbic.pl
DBIx::Class::Storage::DBI::connect_info():
You provided explicit AutoCommit => 0 in your connection_info.
This is almost universally a bad idea
(see the footnotes of DBIx::Class::Storage::DBI for more info).
If you still want to do this you can set $ENV{DBIC_UNSAFE_AUTOCOMMIT_OK}
to disable this warning. at lib/TestSchema.pm line 27

と注意されました。

トランザクションを遣いましょう」ということですね。