in reply to DBI and sqlite concurrency

Sorry it took me awhile to get to this. It’s longish but thread is dead so I’m skipping <readmore/>. Transactions are great and trivial to use and even nest with DBIx::Class; DBIx::Class::Storage::TxnScopeGuard. You can generate a schema from an existing DB—DBIx::Class::Schema::Loader—so you don’t have to write the BEGIN style code; it’s only there for an encapsulated example.

#!/usr/bin/env perl use strictures; use 5.010; BEGIN { # Sample packages. package MyApp::Schema::Address { use parent "DBIx::Class::Core"; __PACKAGE__->table("address"); __PACKAGE__->add_columns( id => { data_type => 'integer', is_auto_increment => 1 }, address => { data_type => 'varchar', size => '100' } ); __PACKAGE__->set_primary_key("id"); }; package MyApp::Schema { use parent "DBIx::Class::Schema"; # For real -> __PACKAGE__->use_namespaces; __PACKAGE__->load_classes("Address"); # <- For this example. }; } my $use_transaction = shift; say "Doing it ", $use_transaction ? "with" : "without", " transaction. +"; my $schema = MyApp::Schema->connect("dbi:SQLite::memory:"); $schema->deploy; # Create some addresses. my $addresses = $schema->resultset("Address"); for ( 1 .. 10 ) { $addresses->create({ address => "$_ Street" }); } my $count = $addresses->count; eval { $use_transaction ? process_with_txn_guard() : process_without_txn_guard(); }; if ( $@ ) { print "We had an error! $@"; say "We have ", $addresses->count, " left in the DB"; say "Sucks to be you..." unless $count == $addresses->count; } exit 0; sub process_without_txn_guard { for my $address ( $addresses->all ) { # Do some local processing with $address. say "Deleting ", $address->address; $address->delete; die "Bad stars" if rand(1) > .8; # Intermittent failure. } # Do remote "global" processing with all addresses. } sub process_with_txn_guard { my $guard = $schema->txn_scope_guard; process_without_txn_guard(); $guard->commit; } __END__ moo@cow~>pm-1142097 Doing it without transaction. Deleting 1 Street Deleting 2 Street Deleting 3 Street We had an error! Bad stars at pm-1142097 line 57. We have 8 left in the DB Sucks to be you... moo@cow~>pm-1142097 with_txn Doing it with transaction. Deleting 1 Street Deleting 2 Street Deleting 3 Street We had an error! Bad stars at pm-1142097 line 58. We have 10 left in the DB