[Catalyst] Transactions with mysql - can't locate db_Main

Ross Crawford ross.crawford at gmail.com
Wed Jan 18 12:18:10 CET 2006


Hi all,

I'm a relative newbie to Catalyst. My application is using MySQL 4 with 
Model::CDBI, with AutoCommit=>1, and requires the use of transactions. I 
found this page http://wiki.class-dbi.com/wiki/Using_transactions which 
describes how to do it with Class::DBI, so I tried that basically 
unchanged, but when I execute do_transaction, I get the error:

Can't locate object method "db_Main" via package "jbrick::Model::jbrick" 
on the line:

	my $dbh = $class->db_Main;

jbrick::Model::jbrick is my CDBI class, where I have the do_transaction 
function.

If I comment out that line, and the line:

	local $dbh->{AutoCommit};

it works fine, but of course commits each SQL seperately. Here is the 
complete function, in case:

sub do_transaction {
	my($class,$code, at args) = @_;

	$class->_invalid_object_method('do_transaction()') if ref($class);

	my @return_values = ();
	my $dbh = $class->db_Main;

	# Localize AutoCommit database handle attribute
	# and turn off for this block.
	local $dbh->{AutoCommit};

	eval {
		@return_values = $code->(@args);
		$class->dbi_commit;
	};
	if ($@) {
		my $error = $@;
		eval { $class->dbi_rollback; };
		if ($@) {
			my $rollback_error = $@;
			$class->_croak("Transaction aborted: $error; Rollback failed: 
$rollback_error\n");
		} else {
			$class->_croak("Transaction aborted (rollback successful): $error\n");
		}
		$class->clear_object_index;
		return;
	}
	return(@return_values);
}



More information about the Catalyst mailing list