php - Update different databases in transaction (Laravel) -
for our web application, written in laravel, use transactions update database. have separated our data cross different database (for ease, let's "app" , "user"). during application update, event fired update user statistics in user database. however, application update may called part of transaction on application database, code structure looks follows.
db::connection('app')->begintransaction(); // ... db::connection('user')->dostuff(); // ... db::connection('app')->commit();
it appears attempt start transaction on user connection (since single query creates implicit transaction) while in game transaction not work, causing deadlock (see innodb status output). used innotop more information, while showed lock wait, did not show query blocked. there lock present on user table, not find it's origin. relevant output shown below:
the easy solution pull out user operation transaction, since actual code more complicated (the dostuff
happens somewhere in nested method called during transaction , called different places), far trivial. dostuff
part of transaction, not see how transaction can span multiple databases.
what reason situation causes deadlock , possible run dostuff
on user database within transaction, or have find entirely different solution queueing events execution afterwards?
i have found way solve issue using workaround. hypothesis was trying use app connection update user database, after forcing use user connection, still locked. switched around , used app connection update user database. since have joins between databases, have database users proper access, no issue. solved problem.
that is, final code turned out like
db::connection('app')->begintransaction(); // ... db::connection('app')->table('user.users')->dostuff(); // pay attention line! // ... db::connection('app')->commit();
Comments
Post a Comment