Permalink
Browse files

If an exception occurs while committing a transaction, attempt to rol…

…lback

I believe most databases will automatically rollback if a commit
fails, but at least SQLite doesn't appear to.  To play it safe,
always attempt to rollback on a failed commit.

This fixes issues if deferred foreign key constraints are violated
on SQLite.
  • Loading branch information...
1 parent 4ca9ae4 commit faff09430052ff4e288fb9156ac5872a4808f99b @jeremyevans committed Nov 30, 2012
Showing with 12 additions and 2 deletions.
  1. +2 −0 CHANGELOG
  2. +9 −1 lib/sequel/database/query.rb
  3. +1 −1 spec/core/database_spec.rb
View
@@ -1,5 +1,7 @@
=== HEAD
+* If an exception occurs while committing a transaction, attempt to rollback (jeremyevans)
+
* Support setting default string column sizes on a per-Database basis via default_string_column_size (jeremyevans)
* Reset Model.instance_dataset when extending the model's dataset (jeremyevans)
@@ -333,7 +333,15 @@ def _transaction(conn, opts={})
begin
committed = commit_or_rollback_transaction(e, conn, opts)
rescue Exception => e2
- raise_error(e2, :classes=>database_error_classes, :conn=>conn)
+ begin
+ raise_error(e2, :classes=>database_error_classes, :conn=>conn)
+ rescue Sequel::DatabaseError => e4
+ begin
+ rollback_transaction(conn, opts)
+ ensure
+ raise e4
+ end
+ end
ensure
remove_transaction(conn, committed)
end
@@ -683,7 +683,7 @@ def @db.ret_commit
end
e.should_not be_nil
e.wrapped_exception.should be_a_kind_of(ec)
- @db.sqls.should == ['BEGIN', 'DROP TABLE test;']
+ @db.sqls.should == ['BEGIN', 'DROP TABLE test;', 'ROLLBACK']
end
specify "should handle errors when sending ROLLBACK" do

0 comments on commit faff094

Please sign in to comment.