Commit 06aa3e5f authored by Andy Yates's avatar Andy Yates
Browse files

Changing the code and now we issue a begin_work() call and eval the call.

parent 114b97ad
...@@ -468,11 +468,13 @@ previous setting. If your DBI/DBD driver does not support manual commits ...@@ -468,11 +468,13 @@ previous setting. If your DBI/DBD driver does not support manual commits
then this code will break. The code will turn off the then this code will break. The code will turn off the
C<disconnect_when_idle()> method to allow transactions to work as expected. C<disconnect_when_idle()> method to allow transactions to work as expected.
If the connection given already had AutoCommit off we will issue a rollback All connections are issued a C<begin_work()> call on the DBI handle which
before handing the connection into the closure. This ensures the connection informs the target engine we are starting a transaction. Giving this method
contains as fresh information from the database as possible. An effect of a connection already in a transaction will cause an error to be raised and
using REPEATABLE READ transaction isolation (InnoDB's default) is that the abortion of the running transaction.
your data is as fresh as when you started your current transaction. To
An effect of using REPEATABLE READ transaction isolation (InnoDB's default)
is that your data is as fresh as when you started your current transaction. To
ensure the freshest data use C<SELECT ... from ... LOCK IN SHARE MODE> ensure the freshest data use C<SELECT ... from ... LOCK IN SHARE MODE>
or C<SELECT ... from ... LOCK FOR UPDATE> if you are going to issue or C<SELECT ... from ... LOCK FOR UPDATE> if you are going to issue
updates. updates.
...@@ -496,6 +498,9 @@ sub transaction { ...@@ -496,6 +498,9 @@ sub transaction {
my $original_dwi; my $original_dwi;
my $ac; my $ac;
my $error;
my $result;
#If we were already in a transaction then we do not do any management of the #If we were already in a transaction then we do not do any management of the
#session & wait for the parent transaction(s) to finish #session & wait for the parent transaction(s) to finish
my $perform_transaction = $self->_perform_transaction_code(); my $perform_transaction = $self->_perform_transaction_code();
...@@ -503,19 +508,18 @@ sub transaction { ...@@ -503,19 +508,18 @@ sub transaction {
$original_dwi = $dbc->disconnect_when_inactive(); $original_dwi = $dbc->disconnect_when_inactive();
$ac = $dbc->db_handle()->{'AutoCommit'}; $ac = $dbc->db_handle()->{'AutoCommit'};
$dbc->db_handle()->{'AutoCommit'} = 0; $dbc->db_handle()->{'AutoCommit'} = 0;
#Issue a rollback as a signal for a fresh transaction if AutoCommit was off eval { $dbc->db_handle()->begin_work() };
$dbc->db_handle()->rollback() if(! $ac); $error = $@ if $@;
$self->_enable_transaction(); $self->_enable_transaction() unless $error;
} }
my $error; if(!$error) {
my $result; eval {
$result = $callback->($dbc);
eval { $dbc->db_handle()->commit() if $perform_transaction;
$result = $callback->($dbc); };
$dbc->db_handle()->commit() if $perform_transaction; $error = $@;
}; }
$error = $@;
if($perform_transaction) { if($perform_transaction) {
if($error) { if($error) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment