#!/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,AutoCommit=>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();