Zend适配器事务回滚失败MSSQL

时间:2012-10-09 03:11:04

标签: zend-framework rollback

我有这个奇怪的错误,我使用MSSQL 2008 R2和ZendFw 1.12


首先..回滚没有在try ..

中失败
   // Zend_DB_Table Adapter
   $adapter = Zend_Db_Table::getDefaultAdapter();

   // Start Transaction
   $adapter->beginTransaction();

   try {
          $adapter->rollBack();
          return (int) 1;
   } catch (Exception $e) {
          throw new Api_Exception($e->getMessage(), $e->getCode(), $e);
   }

但是..内部回滚失败了..

是的,我故意在试试波纹时创建错误,让它进入内部..

   // Zend_DB_Table Adapter
   $adapter = Zend_Db_Table::getDefaultAdapter();

   // Start Transaction
   $adapter->beginTransaction();

   try {
          return (int) A;
   } catch (Exception $e) {
          $adapter->rollBack();
          throw new Api_Exception($e->getMessage(), $e->getCode(), $e);
   }

返回.. Zend_Db_Adapter_Sqlsrv_Exception: A transaction must be started by calling sqlsrv_begin_transaction before calling sqlsrv_commit or sqlsrv_rollback.

有任何线索吗?

1 个答案:

答案 0 :(得分:0)

嗨,去看看这个链接Rollback in SQL

您可以使用安全且简单的代码来100%运行(运行所有查询行),也可以不运行任何代码

|query 1| = like insert into or select or ...
|count of lines| = number off query

DECLARE @rowcount int  set @rowcount = 0 ; 
BEGIN TRANSACTION [Tran1]
BEGIN TRY 
 <Query 1> ; set @rowcount = (@rowcount + @@ROWCOUNT); 
 <Query 2> ; set @rowcount = (@rowcount + @@ROWCOUNT); 
...
IF @rowcount =  <count of lines>
  COMMIT TRANSACTION[Tran1]
ELSE
  ROLLBACK TRANSACTION[Tran1]
END TRY
  BEGIN CATCH
  ROLLBACK TRANSACTION[Tran1]
END CATCH

例如,此代码将2次插入行查询中,但要么全部运行,要么不运行任何内容并回滚

DECLARE @rowcount int  set @rowcount = 0 ; 
BEGIN TRANSACTION [Tran1]
BEGIN TRY 
 insert into [database].[dbo].[tbl1] (fld1) values('1') ;
    set @rowcount = (@rowcount + @@ROWCOUNT); 
 insert into [database].[dbo].[tbl2] (fld1) values('2') ;
    set @rowcount = (@rowcount + @@ROWCOUNT); 

IF @rowcount =  2
  COMMIT TRANSACTION[Tran1]
ELSE
  ROLLBACK TRANSACTION[Tran1]
END TRY
  BEGIN CATCH
  ROLLBACK TRANSACTION[Tran1]
END CATCH