[Dbix-class] How to

Dave Howorth dhoworth at mrc-lmb.cam.ac.uk
Tue Jun 19 09:26:19 GMT 2012


Hardik Joshi wrote:
> I am posting my code for dbi and dbix here please have a look.
> 
> * DBI here AutoCommit = 0
> 
> my $dbh = DBI->connect($dsn, $user, $password,
>                       { RaiseError => 1, AutoCommit => 0 });
> my $sel_sql = qq{select * from test where id = 1 for update};
> my $rs = $dbh->selectrow_array($sel_sql);
> sleep(10);
> my $ins_sql = qq{insert into test(value1, value2) values(10.10,20.1)};
> my $sth = $dbh->prepare($ins_sql);
> $sth->execute();
> $dbh->commit();
> $dbh->disconnect();
> 
> 
> * DBIx here AutoCommit = 0
> 
> my $schema = DB::Model::test->new();
> my $rs = $schema->resultset('Test');
> my $test_res = $rs->search( { 'id' => 1 }, { 'for' => 'update' } )->first();
> sleep(10);
> $rs->create({value1 => 10.45, value2 => 20.70});
> $schema->storage->txn_commit();
> 
> 
> here sleep(10) will hold execution for 10 second so meanwhile you can
> execute same script from other terminal to generate the scenario.

I think you will find that DBIC has not issued any database commands
when you call sleep. i.e. it has not started a transaction. So your code
is not generating the race condition you think it is. As Hailin
suggested, run with DBIC_TRACE=1. You need to actually fetch some data
before you sleep.

I still don't understand the UPDATE vs INSERT issue.



More information about the DBIx-Class mailing list