NAME
DBIx::Class::Manual::SQLHackers::Transactions - DBIx::Class for SQL Hackers - DELETE
Transactions
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.
- 1. Create a Schema object representing the database you are working with:
-
my $schema = MyDatabase::Schema->connect('dbi:SQLite:my.db');
- 2. Call the txn_do method on the Schema object:
-
$schema->txn_do( sub { my $user = $schema->resultset('User')->find({ id => 1 }); die if(!$user); $user->update({ username => 'fred' }); } );