Ovid has asked for the wisdom of the Perl Monks concerning the following question:
I have a base class, Generic::Database, which handles all interactions with MS SQL Server. The actual SQL is generated by classes that inherit from Generic::Database. I am using the DBI module and have autocommit set to false. Most operations on the database are going to be simple updates of one record. For these operations, I decided to handle committing the transaction with the DESTROY method in the base class (these are ISAPI scripts and therefore not persistent database connections). The DESTROY method looks like this:
sub DESTROY { my $self = shift; if ( $self->{ _error } ) { cluck "There was a problem with the transaction: $self->{ _err +_str }"; $self->{ _dbh }->rollback; } else { $self->{ _dbh }->commit; } $self->{ _dbh }->disconnect; }
For most of the program, this works fine. However, for some (not all) update sql statements, the DESTROY method is never triggered and I need to explicitly call $dbh->commit for them:
sub update_admin_message { my ( $self, $data_hash ) = @_; my ( $fields, $values ) = $self->_format_update_data( $data_hash ) +; my $sql = "UPDATE adminMessage SET $fields"; my $data = $self->_update_database( $sql, $values ); $self->{ _dbh }->commit if ! $self->{ _error }; }
The update_admin_message is in the Generic::Database::Modify class which inherits from Generic::Database. If I take out the last line of that method, the update silently fails because DESTROY is never called. Any ideas?
Cheers,
Ovid
Vote for paco!
Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re (tilly) 1: Committing database transactions with DESTROY
by tilly (Archbishop) on Oct 16, 2001 at 21:21 UTC | |
by Ovid (Cardinal) on Oct 16, 2001 at 22:11 UTC | |
by tilly (Archbishop) on Oct 16, 2001 at 23:30 UTC | |
|
Re: Committing database transactions with DESTROY
by AidanLee (Chaplain) on Oct 16, 2001 at 20:40 UTC | |
|
Re: Committing database transactions with DESTROY
by perrin (Chancellor) on Oct 16, 2001 at 21:01 UTC | |
|
Re: Committing database transactions with DESTROY
by Hero Zzyzzx (Curate) on Oct 16, 2001 at 20:49 UTC |