开发者

How to avoid 'Transaction managed block ended with pending COMMIT/ROLLBACK' error across methods

开发者 https://www.devze.com 2023-04-11 11:28 出处:网络
I have a situation where I have applied the @transaction.commit_manually decorator to a method in which I am importing information passed back in an http request response.I need to control committing

I have a situation where I have applied the @transaction.commit_manually decorator to a method in which I am importing information passed back in an http request response. I need to control committing and ro开发者_JAVA技巧lling back depending on whether business validation rules pass or fail.

Now, when I have some sort of validation failure I have a separate method in which I log an error to the database. This action should always commit immediately, while leaving the primary transaction in its current state. However, what happens is if I apply the @transaction.commit_on_success decorator to the error capturing routine, my primary transaction commits automatically as well. If I don't apply the @transaction.commit_on_success decorator, then, I receive the 'Transaction managed block ended with pending COMMIT/ROLLBACK' error as soon as a call is made to the error capturing routine.

I am using MYSQL database version 5.1.49 using storage engine INNODB.

Is there a way to persist the open transaction in the calling routine while committing the transaction in the second routine?


Django's default transaction management doesn't support nested transactions. In general, transactions can't be nested. Everything that's done in the midst of a transaction is either committed or rolledback. So when you commit the transaction, no matter where you commit the transaction, it's atomic.

Looking around online, I found a snippet that might be a good starting point for you. It essentially overrides the commit_on_success decorator, adding a form of reference counting. In a sense, it forgoes committing if it's not the last out.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号