AutoCommit issue
Posted by:
chen gene
Date: February 05, 2015 08:12PM
Hi, I set autocommit=1 in my perl script, but found the record cannot be inserted.
[root@dhcpc tmp]# cat dbd1.pl
#!/usr/bin/perl
use DBI;
my $dbh = DBI->connect( "DBI:mysql:database=test;host=localhost", "root", "123456", { 'RaiseError' => 1, AutoCommit => 1 } );
my $sql =qq{insert into test(name, id) values('hello ', 2)};
$dbh->do($sql);
my $query = $dbh->prepare("SELECT name FROM test");
$query->execute();
while ( $ary = $query->fetchrow_arrayref() ) {
print ( $ary->[0] . "\n");
}
[root@dhcpc tmp]# perl dbd1.pl
hello
[root@dhcpc tmp]# perl dbd1.pl
hello
but if I execute commit, it runs as expected.
[root@dhcpc tmp]# cat dbd2.pl
#!/usr/bin/perl
use DBI;
my $dbh = DBI->connect( "DBI:mysql:database=test;host=localhost", "root", "123456", {AutoCommit => 0 } ) ;
# my $dbh = DBI->connect( "DBI:mysql:database=test;host=localhost", "root", "123456", { 'RaiseError' => 1, AutoCommit => 1 } );
my $sql =qq{insert into test(name, id) values('hello ', 2)};
$dbh->do($sql);
my $query = $dbh->prepare("SELECT name FROM test");
$query->execute();
$dbh->commit();
while ( $ary = $query->fetchrow_arrayref() ) {
print ( $ary->[0] . "\n");
}
[root@dhcpc tmp]# perl dbd2.pl
hello
[root@dhcpc tmp]# perl dbd2.pl
hello
hello
btw, mysql is 5.6.22 and I set autocommit=off in /etc/my.cnf