Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Eloquent (PDO): Check that Transaction is Active

I feel that this should be a simple answer. However, I haven't been able to find a direct answer in the documents or anywhere else.

In the Laravel PHP framework, I have a situation similar to the following where I open a database-transaction:

(Of course, my example is grossly simplified from the real-world stuff I'm working with, so please refrain from "why are you doing it this way" type responses). It's the principle that interests me.

try {

    if ($conditions == $criteria) {
       DB::connection('oracle')->beginTransaction();
    }
    // blah...

Later on in the code, I simply want to check whether a transaction is on-going. The pseudo-code for my condition statement would look something like this:

    if ( DB::connection('oracle')->transactionIsOngoing() ) {

        // do some stuff with the on-going transaction
        DB::connection('oracle')->commit();

        // if I were to execute "DB::connection('oracle')->transactionIsOngoing()"
        // again here it would return FALSE, because the commit command has
        // completed the open transaction

    }

What is the actual code that I should use to replace DB::connection('oracle')->transactionIsOngoing() with?

like image 258
cartbeforehorse Avatar asked Sep 17 '25 09:09

cartbeforehorse


2 Answers

Illuminate\Database\ConnectionInterface::transaction has a transactionLevel property which returns the number of active transactions.

Documentation can be found here: https://laravel.com/api/5.6/Illuminate/Database/ConnectionInterface.html#method_transactionLevel

like image 166
Joe Avatar answered Sep 19 '25 03:09

Joe


To get the transaction level you can use

Illuminate\Support\Facades\DB::transactionLevel();

it returns integer

like image 45
Yevgeniy Afanasyev Avatar answered Sep 19 '25 01:09

Yevgeniy Afanasyev