1

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


首先..回滚在尝试中没有失败..

   // 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);
   }

但是..回滚在catch里面失败了..

是的,我故意在尝试下面创建错误以使其进入 catch ..

   // 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.

请问有什么线索吗?

4

1 回答 1

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 插入行查询,但或运行所有他或不运行任何内容和 ROLLBACK

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
于 2020-06-02T04:44:09.340 回答