douzhuo1858 2013-04-03 12:32
浏览 33
已采纳

PDO MySQL事务

I have 3 queries regarding PDO and Transactions. I'm pretty new at transactions, so please understand my misunderstandings!

First, i'll explain the situation: I have created a PDO wrapper in a singleton pattern, which accesses a MySQL database with innoDB tables. The reason I've created a wrapper is to provide extra functionality and improve portability.

Each time an object needs access to the database, it retrieves the same connection.

My questions are as follow:

1) I understand if I begin a transaction with PDO::beginTransaction(), then that sets MySQLs autocommit mode to off. However does this affect just the current users connection to the database, or every visitors connection, since its the database which contains the autocommit value, not the PHP script?

2) I have a need to perform MySQL queries which are not relevant to the actual transaction. These queries are performed in unrelated objects, however still use the same MySQL connection as the transaction. I've just found out that the unrelated (to the transaction) queries are still being included in the transaction, which is causing undesired effects. Is there any way around this? Or is my PHP design incorrect for this use of transactions?

3) I'm using PHP 5.1. It supports the following functions: PDO::beginTransaction(), PDO::commit() and PDO::rollBack().

I understand that if I call PDO::rollBack(), when a transaction is not in effect, a PDOException is thrown. The solution to this is to use PDO::inTransaction() to check for transactions before using rollBack(), however this isn't added to PHP until 5.3.

At the moment, when my wrapper starts a transaction, it updates a static variable to true, and when its rolled back, or committed, it sets the variable back to false. Is this a good solution? Or is there a pre-existing solution I'm unaware of?

Many thanks for your help

  • 写回答

2条回答 默认 最新

  • dtc9222 2013-04-03 12:39
    关注
    1. Transaction per connection. But some queries might be affected by each other from different connections.
    2. For InnoDB connections all queries in transactions. But with autocommit it commits after each of them. That's why you are always in transaction.
    3. You can do that with your own variable... Also you can use autocommit variable to find out that you have started a transaction. You can do it with query select @@autocommit.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化