Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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...
commit faff09430052ff4e288fb9156ac5872a4808f99b 1 parent 4ca9ae4
@jeremyevans authored
View
2  CHANGELOG
@@ -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)
View
10 lib/sequel/database/query.rb
@@ -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
View
2  spec/core/database_spec.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.