开发者

Mysql: ROLLBACK for multiple queries

I have more than three MySql queries in a PHP script triggered by scheduled task. If a query catch an error, script throw an exception and rollback that Mysql query. It works fine.

However if first query works fine, but not 2nd query, throw an exception, it rollback 2nd one but not 1st query.

I am using begin_trans(), commit and rollback() for individual queries because Sometimes i need to rollback one query, sometimes all queries. Is there any way to rollback one query or all queries?

Thanks in advance

开发者_如何学运维

UPDATE:

I got it working, there was no problem with in begin_trans(), commit and rollback(), the database connection config was different for one query from other queries, crazy code without any comments!!!


The only thing that has to be rolled back is a write operation (INSERT, UPDATE, or DELETE). I'll assume that you're using the word "query" to mean something other than a SELECT operation.

If you want several SQL statements to succeed or fail together, you'll need to specify a transaction.

UPDATE:

Now I'm confused; it's no wonder that you are.

A transaction is an all-or-nothing proposition. It sounds to me like you're confusing two separate use cases: one where you want a single query in a transaction and another where you want several in one transaction. Combining the two is confusing you and, I'm sure, your users.

One you commit a transaction, you can't roll it back. So you'll have to make up your mind: either operation A is part of its own transaction or grouped with B, C, and D in another. But not both.


A rollback is applied to all queries within current transaction.

[edited after question update]

MySQL currently does not support nested transaction so it's all or nothing deal. You can only rollback all queries within a transaction or commit all (successful ones).


The kind of rollback that one gets is completely dependent on how one defines the transaction.

And that is dependent on business use case.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜