DBIx::Class::Manual::SQLHackers::Transactions - DBIx::Class for SQL Hackers - DELETE
BEGIN; SELECT users.id, users.username FROM users WHERE id = 1; UPDATE users SET username = 'fred' WHERE id = 1; COMMIT;
To create a transaction, put all your changes inside a coderef, and pass it to the txn_do method on the Schema object. Transactions can also be safely nested, in which case all but the top level transaction blocks become noops.
my $schema = MyDatabase::Schema->connect('dbi:SQLite:my.db');
$schema->txn_do( sub { my $user = $schema->resultset('User')->find({ id => 1 }); die if(!$user); $user->update({ username => 'fred' }); } );
To install DBIx::Class::Manual::SQLHackers::Transactions, copy and paste the appropriate command in to your terminal.
cpanm
cpanm DBIx::Class::Manual::SQLHackers::Transactions
CPAN shell
perl -MCPAN -e shell install DBIx::Class::Manual::SQLHackers::Transactions
For more information on module installation, please visit the detailed CPAN module installation guide.