framework icon indicating copy to clipboard operation
framework copied to clipboard

[ORM] Nested transactions emulation

Open DavertMik opened this issue 13 years ago • 16 comments

Eloquent should implement nested transaction emulation. This simple trick will ensure that you can use a transaction inside other transaction safely. Also it speeds up integration and functional testing.

As you know, MySQL does not support nested transactions. Most of ORMs emulate them by creating a simple counter $activeTransactions = 0 (or using MySQL savepoints). When a new transaction startes counter is incremented. When transaction finishes, and $activeTransactions > 1 counter is decremented, but commit / rollback is not executed.

In other words: commit/rollback will be executed only for the first transaction started.

Take a look in Doctrine DBAL Transaction for reference:

https://github.com/doctrine/dbal/blob/master/lib/Doctrine/DBAL/Connection.php#L1031 https://github.com/doctrine/dbal/blob/master/lib/Doctrine/DBAL/Connection.php#L1049

This helps in testing purposes as it may perform fast database cleanups by rolling back the active transaction. But it's worth and simple feature on it's own.

With this feature we can improve Laravel4 module in Codeception for data auto cleanup.

DavertMik avatar Jun 19 '13 20:06 DavertMik

Does GitHub really not have a +1 button?

Well...+1

JeffreyWay avatar Jun 19 '13 20:06 JeffreyWay

:+1:

JonoB avatar Jun 19 '13 20:06 JonoB

I implemented a custom transaction method in L3 just a because of this, too.

JoostK avatar Jun 19 '13 22:06 JoostK

+1

izidorome avatar Jun 26 '13 17:06 izidorome

:+1:.

jonathanmarvens avatar Jul 27 '13 05:07 jonathanmarvens

So is this only a MySQL thing? Other databases support nested transactions fine?

taylorotwell avatar Aug 01 '13 13:08 taylorotwell

100% for Postgres and probably Oracle/MSSql too. Except SQLite (he-he).

DavertMik avatar Aug 01 '13 15:08 DavertMik

@taylorotwell Both MySQL and Postgres use "SavePoints" to emulate a nested transaction.

However, even without save-point support, it would be nice if the ORM supported the ability to recognize when it was inside a transaction and continue the transaction without throwing a PDO error, like so: http://www.php.net/manual/en/pdo.begintransaction.php#109753

Rican7 avatar Aug 01 '13 23:08 Rican7

Done. We can add in the save point stuff later if we want.

taylorotwell avatar Aug 02 '13 15:08 taylorotwell

Thanks @taylorotwell! Let me know if I can be of any help with this feature.

Rican7 avatar Aug 02 '13 16:08 Rican7

+1

@taylorotwell This would be so useful.

KennedyTedesco avatar Apr 30 '14 19:04 KennedyTedesco

:+1: looking forward

blankyao avatar Dec 04 '14 16:12 blankyao

+1

vigneshgurusamy avatar Oct 29 '15 16:10 vigneshgurusamy

+1 any news? :D

punchi avatar Nov 27 '15 17:11 punchi

This is already done.

On Fri, Nov 27, 2015 at 11:38 AM, Alfredo Concha [email protected] wrote:

+1

any news? :D

Reply to this email directly or view it on GitHub: https://github.com/laravel/framework/issues/1686#issuecomment-160180910

taylorotwell avatar Nov 27 '15 19:11 taylorotwell

HAHAHAHHA :smile:

RobinMalfait avatar Nov 28 '15 11:11 RobinMalfait