Move transaction counter from Thread.current to the connection object

I’ve updated the patch for the ticket at

New ticket here:

It moves the counter for the number of open transactions from Thread.current (which seems a very odd place to keep it), to the connection object. This means that transactions across multiple database connections can be properly supported.

Comments please.


Anyone able to look at, comment on, or apply this ticket?

Transactions across multiple database connections won't really 'work'
with this will they? an error in one won't roll back the other and
you don't get transactional guarantees like you would with a full two
phase commit. Are there any other functional benefits?

However, the connection handling code is pretty gross and Nick Sieger
from the jruby guys has been looking at refactoring it to use a
connection pool. I'd try to touch base with him and get this merged
into his branch which will eventually make its way into the mainline.

Quite right, they won’t be fully functional in that sense, but it is certainly an improvement. An exception in the inner transaction will cause the outer one to rollback as long as it isn’t caught and discarded (as the test shows).

This change at least makes multi-database transactions usable for those who need it. Also, according to the original Trac ticket (, this used to work before Rails 2.0.