Following your description, I made an example to try to duplicate the behavior you were describing.
However, it does not do what you say. It seems to work fine.
#!/usr/bin/perl -w
use DBI;
use strict;
package One;
sub new {
my $class = shift;
my $self = bless {}, $class;
my $dbh = shift || return undef;
$self->{dbh} = $dbh;
return $self;
}
sub execute {
my $self = shift;
my $query = shift;
my $sth= $self->{dbh}->prepare($query);
my $rows = $sth->execute or die $DBI::errstr;;
print "$rows rows affected\n";
}
sub begin_work{
my $self = shift;
$self->{dbh}->begin_work;
}
sub commit{
my $self = shift;
$self->{dbh}->commit;
}
package Two;
sub new {
my $class = shift;
my $self = bless {}, $class;
my $dbh = shift || return undef;
$self->{dbh} = $dbh;
return $self;
}
sub execute {
my $self = shift;
my $query = shift;
my $sth= $self->{dbh}->prepare($query);
my $rows = $sth->execute or die $DBI::errstr;;
print "$rows rows affected\n";
}
package main;
my $dbh= DBI->connect("dbi:SQLite:dbname","","",{RaiseError => 1,AutoC
+ommit=>1})
#my $dbh = DBI->connect("DBI:mysql:dbname",
# 'user', 'password', {RaiseError => 1, AutoCommit=>1})
or die "can't connect\n";
$dbh->do(qq{delete from table1 where t2id =5});
$dbh->do(qq{delete from table2 where t2id =5});
my $query = qq{ insert into table1 values (5,"xxx") };
my $one = new One $dbh or die "can't create One\n";
my $two = new Two $dbh or die "can't create Two\n";
$one->begin_work(); # transaction starts in module One
$one->execute($query); # transaction continues in module One
$query = qq{insert into table2 values(11,"yyy", 5)};
$two->execute($query); # transaction continues in module Two
$one->commit(); # transaction ends in module One
$dbh->disconnect();
I tried this example with two different DBD drivers (MySQL and SQLite) and it worked as I expected. The transaction is processed correctly. Try to add a call to $dbh->rollback() after the first execute, and it will cancel the insertion without problems. It
means that the transaction is really split across packages.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.