Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't force reconnect while within transaction and connection lost #15865

Closed
halaei opened this issue Oct 12, 2016 · 7 comments
Closed

Can't force reconnect while within transaction and connection lost #15865

halaei opened this issue Oct 12, 2016 · 7 comments

Comments

@halaei
Copy link
Contributor

halaei commented Oct 12, 2016

  • Laravel Version: 5.3.*

Description:

It seems that the framework don't provide any way to reconnect to database while within a transaction and connection is lost.

Steps To Reproduce:

It is hard to reproduce. Not having the option of handling such a situation is bad. It may happen if someone is working with daemon queue workers and for some reason a transaction is not closed within a job and is leaked out. It may also happen for queries that are expected to take long.

    public function reconnect($force = false)
    {
        ...
    }
    ...
    public function setPdo($pdo, $force = false)
    {
        if ($this->transactions >= 1 && ! $force) {
            throw new RuntimeException("Can't swap PDO instance while within transaction.");
        }

        $this->transactions = 0;

        $this->pdo = $pdo;

        return $this;
    }
@themsaid
Copy link
Member

what do you suggest? how do you think we should handle this?

@halaei
Copy link
Contributor Author

halaei commented Oct 12, 2016

I was working on a PR to add a $force = true param to DB::reconnect().

@taylorotwell
Copy link
Member

There is another PR already open to fix this I believe? #15511

@halaei
Copy link
Contributor Author

halaei commented Oct 12, 2016

#15511 sounds to be different. My PR lets the client code do this:

try {
DB::beginTransaction();
...
//possibly nested transactions are here without any extra try-catch
...
DB::commit();
} catch (Exception $e) {
   try {
   while(DB::transactionLevel() > 0) DB::rollBack();
   } catch (Exception $e) {
       //This looks only possible after the pull request: to handle lost connection or any other reason that makes rollbacks impossible.
       // Without this PR the application must be aborted, a terrible situation for daemon workers as an example.
       DB::reconnect(true);
       throw $e;
   }
}

@themsaid
Copy link
Member

@halaei was this taken care of by any of your PRs?

@halaei
Copy link
Contributor Author

halaei commented Oct 27, 2016

Does github have a problem with replies via email? I answered 2ice via email but both are failed I think?
Sorry for possible duplicate replies:
@themsaid Yes check this please: #15931
It is simple and hopefully working. This issue is now closed for 5.3. I may not try for a proper change for 5.1 though.

@halaei halaei closed this as completed Oct 27, 2016
@halaei
Copy link
Contributor Author

halaei commented Oct 27, 2016 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants