简体   繁体   English

ZF2 中的 Zend\\Db 如何控制事务?

[英]How does Zend\Db in ZF2 control transactions?

The ZF1 Zend_Db reference manual has an entire section on performing transactions. ZF1 Zend_Db 参考手册有一整节是关于执行事务的。

The ZF2 Zend\\Db reference manual lacks any documentation on transactions. ZF2 Zend\\Db 参考手册缺少任何关于事务的文档。

How do I perform transactions in ZF2?如何在 ZF2 中执行事务? Example code would be helpful.示例代码会有所帮助。

You've got it.你已经知道了。 The proper way to Begin, Commit, and Rollback Transactions is as follows:开始、提交和回滚事务的正确方法如下:

$this->getAdapter()->getDriver()->getConnection()->beginTransaction();

$this->getAdapter()->getDriver()->getConnection()->commit();

$this->getAdapter()->getDriver()->getConnection()->rollback();

Just to put this out there too you can also get the Last ID created by:只是为了把它放在那里,你还可以通过以下方式获得最后一个 ID:

$this->getAdapter()->getDriver()->getConnection()->getLastGeneratedValue()

If you are using pgSQL you will need to add the sequence to return the Last ID created:如果您使用 pgSQL,则需要添加序列以返回创建的最后一个 ID:

$this->getAdapter()->getDriver()->getConnection()->getLastGeneratedValue('mail_mailid_seq')

The missing documentation is curious.丢失的文档很奇怪。

To find out what happened, I had to dive into the API docs for Zend\\Db\\Adapter.为了找出发生了什么,我不得不深入研究 Zend\\Db\\Adapter 的API 文档

It looks like beginTransaction , rollback and commit are defined in Zend\\Db\\Adapter\\Driver\\ConnectionInterface .看起来beginTransactionrollbackcommit是在Zend\\Db\\Adapter\\Driver\\ConnectionInterface 中定义的 This means that they are methods callable on every single adapter connection.这意味着它们是可在每个单独的适配器连接上调用的方法。 Unfortunately the connection itself is rather buried.不幸的是,连接本身被埋没了。

What I'm not clear on -- and can't provide an example for at this time -- is figuring out which object you actually call these methods on.我不清楚——目前无法提供一个例子——是弄清楚你实际上在哪个对象上调用了这些方法。 In the worst case, it looks like you might want to call $adapter->getDriver()->getConnection()->beginTransaction() .在最坏的情况下,看起来您可能想要调用$adapter->getDriver()->getConnection()->beginTransaction()

Eww.呃。

I'm hoping someone else with more knowledge, and a copy of ZF2 handy, will see this and provide a better option.我希望有更多知识的其他人以及手边的 ZF2 副本会看到这一点并提供更好的选择。

Don't forget that you can just issue BEGIN TRANSACTION / ROLLBACK / COMMIT / SET autocommit=... SQL statements yourself.不要忘记,您可以自己发出BEGIN TRANSACTION / ROLLBACK / COMMIT / SET autocommit=... SQL 语句。 This is probably OK, as it doesn't look like Zend\\Db keeps track of the transaction state.可能没问题,因为它看起来不像 Zend\\Db 跟踪事务状态。

There are two matter for doing transaction.做交易有两件事。
1 - MyISAM is not a transactional engine , so change tables engine to InnoDB. 1 - MyISAM 不是事务引擎,因此将表引擎更改为 InnoDB。
2 - Transaction query( "START TRANSACTION;" OR "ROLLBACK;" ) connection must be same with other queries(Insert or Update). 2 - 事务查询( "START TRANSACTION;" OR "ROLLBACK;" )连接必须与其他查询(插入或更新)相同。
For doing this in ZF2 you should get current db adapter and use it in all queries.要在 ZF2 中执行此操作,您应该获取当前的 db 适配器并在所有查询中使用它。

This code will not work correctly :此代码将无法正常工作

    $this->getAdapter()->getDriver()->getConnection()->beginTransaction();  
    //do some jobs - e.g : multiple tables update or insert.  
    $this->getAdapter()->getDriver()->getConnection()->rollback();   

Since $this->getAdapter()->getDriver()->getConnection() Creates new db connection.由于$this->getAdapter()->getDriver()->getConnection()创建了新的数据库连接。

Use following code instead:请改用以下代码:

    $connection = $this->getAdapter()->getDriver()->getConnection();
    $connection->beginTransaction();
    //do some jobs - e.g : multiple tables update or insert. 
    $connection->rollback();

For check if your connections is correct , just enable query log in mysql.要检查您的连接是否正确,只需在 mysql 中启用查询日志。
After running query you will see connection number before each query in mysql log.Those must be same in all transaction queries.运行查询后,您将在 mysql 日志中的每个查询之前看到连接号。所有事务查询中的这些必须相同。

I used beginTransaction , rollback and commit in controller.我在控制器中使用了beginTransactionrollbackcommit

where I performed number of transactions on different models where I using predefined functions without any control transactions (not necessary for single DB transaction).我在不同模型上执行了许多事务,其中我使用预定义的函数而没有任何控制事务(单个 DB 事务不需要)。

Using $this->getAdapter()->getDriver()->getConnection()->beginTransaction();使用$this->getAdapter()->getDriver()->getConnection()->beginTransaction();
gives error on undefined getAdapter() methods.undefined getAdapter()方法上给出错误。

So I perform following way,所以我执行以下方式,

  //begain tarnsaction
                $db = Zend_Db_Table_Abstract::getDefaultAdapter();

                $db->beginTransaction();

//rollback

                $db->rollback();

//commit db changes

                $db->commit();

Hope it may useful to solve problem.希望对解决问题有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM