Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/Data/ObjectDriver/BaseObject.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ our $HasWeaken;
eval q{ use Scalar::Util qw(weaken) }; ## no critic
$HasWeaken = !$@;

our $HasAtFork;
eval q{ use POSIX::AtFork }; ## no critic
$HasAtFork = !$@;

use Carp ();

use Class::Trigger qw( pre_save post_save post_load pre_search
Expand Down Expand Up @@ -668,6 +672,14 @@ sub has_partitions {
});
}

# At fork, forget about parent's current transaction status.
if ( $HasAtFork ) {
POSIX::AtFork->add_to_child(sub {
@WorkingDrivers = ();
$TransactionLevel = 0;
});
}

1;

__END__
Expand Down
33 changes: 33 additions & 0 deletions lib/Data/ObjectDriver/Driver/DBI.pm
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ use Data::ObjectDriver::SQL;
use Data::ObjectDriver::Driver::DBD;
use Data::ObjectDriver::Iterator;

our $HasWeaken;
eval q{ use Scalar::Util qw(weaken) }; ## no critic
$HasWeaken = !$@;

our $HasAtFork;
eval q{ use POSIX::AtFork }; ## no critic
$HasAtFork = !$@;

__PACKAGE__->mk_accessors(qw( dsn username password connect_options dbh get_dbh dbd prefix reuse_dbh force_no_prepared_cache));


Expand All @@ -36,6 +44,21 @@ sub init {
}
$driver->dbd(Data::ObjectDriver::Driver::DBD->new($type));
}

if ( $HasWeaken && $HasAtFork ) {
# Purge Cached dbh, and reset transaction level
weaken(my $driver_weaken = $driver);
POSIX::AtFork->add_to_child(sub {
eval {
if ( my $dbh = $driver_weaken->dbh ) {
$dbh->{InactiveDestroy} = 1;
$driver_weaken->dbh(undef);
}
$driver_weaken->txn_active(0);
};
});
}

$driver;
}

Expand Down Expand Up @@ -752,4 +775,14 @@ sub last_error {
return $driver->dbd->map_error_code($DBI::err, $DBI::errstr);
}

if ( $HasAtFork ) {
# Purge all reusable db handles
POSIX::AtFork->add_to_child(sub {
for my $handle ( values %Handles ) {
$handle->{InactiveDestroy} = 1;
}
%Handles = ();
});
}

1;