Feature Request #3432

Nested Transactions

Added by Chris Bandy over 1 year ago. Updated 3 months ago.

Status:Feedback Start date:11/25/2010
Priority:Normal Due date:
Assignee:Chris Bandy % Done:

0%

Category:-
Target version:Kohana v3.x - Unscheduled
Resolution: Points:

Description

Lorenzo Pisani wrote:

Just wanted to point this note out: https://github.com/kohana/database/commit/287261386651a393fc7cf7d4f71bed44b9583f18#commitcomment-183951
I don't know if you guys want to handle it but it's quite crucial when working with complex actions

Isaiah DeRose-Wilson wrote:

I use nested transactions too, you can see how I do it in this commit and a quick example here

I'm not sure if this is too much magic to include in the Database module. I find it very handy, but it does make transactions work differently from the way you might expect (i.e. rolling back all changes when you call rollback, and committing all changes as soon as you call commit).


Related issues

related to Kohana v3.x - Feature Request #2278: Add transaction support Kohana v3.x - v3.1.0 Closed 10/24/2009

History

Updated by Chris Bandy over 1 year ago

"Nested" transactions may not be the right idea to generalize since every database supports savepoints. Instead of hiding savepoints, I'm thinking we should leverage them and abstract BEGIN / COMMIT somewhat.

I'll try and write some code in the next couple days.

MySQL 5 PostgreSQL SQLite 3 SQL Server Oracle1
Begin a transaction START TRANSACTION START TRANSACTION BEGIN TRANSACTION BEGIN TRANSACTION SET AUTOCOMMIT OFF
Set a savepoint SAVEPOINT {name} SAVEPOINT {name} SAVEPOINT {name} SAVE TRANSACTION {name} SAVEPOINT {name}
Release a savepoint RELEASE SAVEPOINT {name} RELEASE [SAVEPOINT] {name} RELEASE [SAVEPOINT] {name} - -
Revert to savepoint ROLLBACK TO {name} ROLLBACK TO {name} ROLLBACK TO {name} ROLLBACK TRANSACTION {name} ROLLBACK TO {name}
Revert transaction ROLLBACK ROLLBACK ROLLBACK ROLLBACK ROLLBACK
Commit transaction COMMIT COMMIT COMMIT COMMIT COMMIT

1 Oracle is in a transaction unless AUTOCOMMIT has been set to ON

Updated by Chris Bandy over 1 year ago

  • Status changed from New to Feedback

Not too bad! I'll probably break this into three commits instead of the current two.

https://github.com/cbandy/kohana-database/compare/kohana:3.1.x...feature/3432-savepoints

  1. MySQL::_execute($statement)
  2. Database::savepoint($name)
  3. Nested transactions through savepoints

Updated by Chris Bandy over 1 year ago

Lorenzo, Isaiah and I talked about the above implementation on IRC.

The API behaves similarly to SQL in that rollback() or commit() without any parameters will end the current transaction. This is different from another DBAL that hides automatic nesting from the user. This difference may be counterintuitive.

On the other hand, this API supports automatic nesting by requiring a little more effort from the user. The trade-off seemed acceptable since it makes the API more consistent and flexible.

Transactions

/* Basic/single transaction */

// Start transaction
$db->begin();
try
{
    // Save everything
    $db->commit();
}
catch ()
{
    // Abort everything
    $db->rollback();
}
/* Automatic nesting */

// Start transaction or generate savepoint
$token = $db->begin();
try
{
    // Save transaction or release savepoint
    $db->commit($token);
}
catch ()
{
    // Abort transaction or savepoint
    $db->rollback($token);
}

 

Savepoints

/* Savepoints */

// Start transaction
$db->begin();

// Define a savepoint
$db->savepoint('first_part');

// Generate a savepoint
$name = $db->savepoint();

// Abort two savepoints
$db->rollback('first_part');

// Save everything
$db->commit();
/* Savepoints with nesting */

// Start transaction or generate savepoint
$token = $db->begin();
try
{
    // Define a savepoint
    $db->savepoint('anything');

    // Save transaction or release two savepoints
    $db->commit($token);
}
catch ()
{
    // Abort transaction or savepoints
    $db->rollback($token);
}

Updated by Kiall Mac Innes over 1 year ago

  • Project changed from Kohana v3.x to Database
  • Category deleted (Modules:Database)

Updated by Chris Bandy 11 months ago

  • Target version changed from v3.2.0 to v3.3.0

Savepoint tracking is not as trivial as I made it out to be. For example, using the same name twice (is valid and) replaces the old point with the new one.

Updated by Jeremy Bush 5 months ago

  • Target version changed from v3.3.0 to Unscheduled

Updated by Enrique Piatti 3 months ago

we were kicking this for more than a year, it's time to make a goal here :P
what is the state of this now? I want to help.

Also available in: Atom PDF