use strict; use warnings; use DBI; use Try::Tiny; $|=1; my $redo; my $dbh; print 'mysql username: '; chomp(my $username=); print 'mysql password: '; system('stty','-echo');chomp(my $pw=);system('stty','echo'); print $/; sub reconn { $dbh = DBI->connect('dbi:mysql:scratch;mysql_enable_utf8=1',$username,$pw,{mysql_auto_reconnect => 1}) or die $DBI::errstr; } reconn; my $sth; # MariaDB [scratch]> create table foo(bar int,baz int,primary key(bar)); # Query OK, 0 rows affected (0.02 sec) # # MariaDB [scratch]> insert foo(bar)value(1); # Query OK, 1 row affected (0.01 sec) # # MariaDB [scratch]> desc foo; # +-------+---------+------+-----+---------+-------+ # | Field | Type | Null | Key | Default | Extra | # +-------+---------+------+-----+---------+-------+ # | bar | int(11) | NO | PRI | 0 | | # | baz | int(11) | YES | | NULL | | # +-------+---------+------+-----+---------+-------+ # 2 rows in set (0.00 sec) # # # MariaDB [scratch]> select * from foo; # +-----+------+ # | bar | baz | # +-----+------+ # | 1 | NULL | # +-----+------+ # 1 row in set (0.00 sec) $\=$/; my $rows; $dbh->ping or reconn; $sth = $dbh->prepare("insert foo(bar)value(2);"); $redo = 1; while ($redo) {$redo=0; try {$rows=$sth->execute();} catch {$redo = /deadlock found/i;}} print "new: $rows"; undef $rows; $dbh->ping or reconn; $sth = $dbh->prepare("insert ignore foo(bar)value(1);"); $redo = 1; while ($redo) {$redo=0; try {$rows=$sth->execute();} catch {$redo = /deadlock found/i;}} print "old ignore: $rows"; undef $rows; $dbh->ping or reconn; $sth = $dbh->prepare("insert foo(bar)value(1) on duplicate key update baz=1;"); $redo = 1; while ($redo) {$redo=0; try {$rows=$sth->execute();} catch {$redo = /deadlock found/i;}} print "old on duplicate key update: $rows"; #### new: 1 old ignore: 0E0 old on duplicate key update: 2 #### # MariaDB [scratch]> select * from foo; # +-----+------+ # | bar | baz | # +-----+------+ # | 1 | NULL | # +-----+------+ # 1 row in set (0.00 sec) # # MariaDB [scratch]> insert foo(bar)value(2); # Query OK, 1 row affected (0.00 sec) # # MariaDB [scratch]> select row_count(); # +-------------+ # | row_count() | # +-------------+ # | 1 | # +-------------+ # 1 row in set (0.00 sec) # # MariaDB [scratch]> insert ignore foo(bar)value(1); # Query OK, 0 rows affected, 1 warning (0.00 sec) # # MariaDB [scratch]> select row_count(); # +-------------+ # | row_count() | # +-------------+ # | 0 | # +-------------+ # 1 row in set (0.01 sec) # # MariaDB [scratch]> insert foo(bar)value(1) on duplicate key update baz=1; # Query OK, 2 rows affected (0.00 sec) # # MariaDB [scratch]> select row_count(); # +-------------+ # | row_count() | # +-------------+ # | 2 | # +-------------+ # 1 row in set (0.00 sec)