#383 open
Sean Kirby

ActiveRecord should use savepoints for nested transactions

Reported by Sean Kirby | June 10th, 2008 @ 12:54 PM | in 2.2

From http://dev.rubyonrails.org/ticke... in the old rails trac.

Jonathan Viney has created a patch that enables doesn't break existing transaction semantics but allows for transactional behaviour to be tested in transactional fixtures.

http://svn.viney.net.nz/things/r...

Comments and changes to this ticket

  • Jeremy Kemper

    Jeremy Kemper June 10th, 2008 @ 04:35 PM

      • → Assigned user changed from “” to “Jeremy Kemper”

    I'd love to support savepoints. We've had the discussion regarding the default behavior of nested transactions, though, and it's not the way to go. Nested transactions should be explicit, otherwise we're going to flood the database with junk transactions that really only mean "include me in a transaction" not "start a new transaction". Let's extend the existing semantics instead of stepping on them. And add better test coverage ;)

  • Jeremy Kemper

    Jeremy Kemper June 10th, 2008 @ 04:35 PM

      • → Milestone changed from “” to “2.2”
  • Tarmo Tänav

    Tarmo Tänav June 16th, 2008 @ 02:02 PM

    Jeremy, the plugin by Jonathan Viney actually does make transaction{} only create a nested transaction if an explicit ":force => true" option is passed to it. The only exception being transactional tests where one additional level of non-explicit transactions can be created (because the test class itself uses the first level).

    So these changes should not change rails behavior outside tests.

    This plugin however does not include the ability to configure rails behavior for implicit transactions (those automatically created by save/destroy). Which means that you will not be able to make some activerecord model always create an implicit transaction for saving itself, even if a higher level transaction already exists. But this is a capability not strictly neccessary for the nested transactions feature to be useful, as you can always create an explicit "transaction :force => true" block around a save if you need it.

  • jonathan

    jonathan July 3rd, 2008 @ 07:12 AM

      • → Tag changed from “” to “activerecord nested savepoint transaction”

    I'd like to see #533 included first (moving the transaction counter from Thread.current to the connection object), and then I'll add an updated patch for the savepoints.

  • Jeremy Kemper

    Jeremy Kemper August 29th, 2008 @ 11:10 PM

      • → State changed from “new” to “open”

    #533 is in -- interested in porting your work over to Rails master?

  • jonathan

    jonathan August 31st, 2008 @ 05:38 AM

    I've attached a patch for master.

    The handling of increment/decrement_open_transactions has been moved to the connection object rather than handling it in AR::Base.transaction, seemed better to me.

    I've added a transactional_fixtures accessor to the connection which is set by the fixtures. Not the nicest, but I wasn't sure what else to do.

    There are some failures for databases that don't support savepoints (eg. sqlite). These are a bit hard to avoid unless we add a supports_savepoints? to the connection.

    Anyway, this should get the ball rolling.

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Source available from github

The Git repository resides at http://github.com/rails

Check out the current development trunk (Edge Rails) with:

git clone git://github.com/rails/rails.git

The latest development for the 1.2.x and 2.0.x releases are on the 1-2-stable and 2-0-stable branches.

Creating a bug report

When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.

Security vulnerabilities should be reported via an email to security@rubyonrails.org, do not use trac for reporting security vulnerabilities. All content in trac is publicly available as soon as it is posted.

Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too".

Shared Ticket Bins

Attachments