in reply to Re^2: Dead Lock replication in Sql server
in thread Dead Lock replication in Sql server

If you need code to replicate a deadlock in Microsoft SQL Server, I think you are wrong here. Creating a deadlock in a database is not a Perl problem, it is more an SQL problem.

Once you have the SQL to create the deadlock, use DBI to send the SQL to the server.

Then fix the issue by using Exception::Class::TryCatch.

  • Comment on Re^3: Dead Lock replication in Sql server

Replies are listed 'Best First'.
Re^4: Dead Lock replication in Sql server
by sunil19 (Initiate) on Jun 30, 2017 at 11:50 UTC

    Here is my sample code which iam using to replicate the dead lock and using trycatch to handle the dead lock

    Transaction A:sample

    use strict; use DBI; use Execption::Class:TryCatch; ###opening connections try eval{ my $sql = "BEGIN TRAN \n"; my $sql1 = "Update dead_test set a = '11' where b= 1 \n"; my $sql2 = "WAITFOR DELAY '00:00:05'\n" my $sql3 = "Update dead_test1 set a = '22' where b= 2 \n"; my $sql4 = "COMMIT \n" ; $sql = $sql.$sql1.$sql2.$sql3.$sql4; $sql = $dbh->pepare($sql); $sql ->execute(); }; if catch(my $err){ print $err; eval { $dbh - >rollback }; }

    Transaction B:sample

    use strict; use DBI; use Execption::Class:TryCatch; ###opening connections try eval{ my $sql = "BEGIN TRAN \n"; my $sql1 = "Update dead_test1 set a = '11' where b= 2 \n"; my $sql2 = "WAITFOR DELAY '00:00:05'\n" my $sql3 = "Update dead_test set a = '22' where b= 1 \n"; my $sql4 = "COMMIT \n" ; $sql = $sql.$sql1.$sql2.$sql3.$sql4; $sql = $dbh->pepare($sql); $sql ->execute(); }; if catch(my $err){ print $err; eval { $dbh - >rollback }; }

    Iam executing both the transaction from 2 unix sessions parallel. As of now it doesn;t return or print any error. The question is how to capture the error from that is return from database and rollback if it is a deadlock error (erronumber 1205).

      FYI, you can create a multiline string with this syntax:

      my $sql = <<_SQL_; BEGIN TRAN Update my_test1 set a = '33' where b= 2 WAITFOR DELAY '00:00:05' Update my_test set a = '44' where b= 1 COMMIT _SQL_

      Your code has many typos and mistakes, there's no way it even compiles. So you can't have been executing that.

        I haven't copied my code exactly the way it is. its just and mock up example. I trying to use TryCatch package to catch my exception that returns from the database (sql server). But i couldn't able to do it ( my experience in using perl is 4 days). If there are any example which i can refer where the database throws certain error (say Deadlock 1205) i need to handle data error.please let me know if there are any references or links which can i refer. Thanks

Re^4: Dead Lock replication in Sql server
by sunil19 (Initiate) on Jun 30, 2017 at 11:17 UTC
    i have code that can replicate the dead lock issue.can you help me to code to using DBI to send tht SQL to server and also the fix it by using Exception::Class::TryCatch.

      Have you read DBI? It contains lots of information on how to use it.

      Then read Exception::Class::TryCatch on how to catch the error. The modules documentation also contains lots of information on how to use it.

      Where exactly do you have problems?