I have read the content of  "SqlConnection.Close() method rolls back any pending transactions." in msdn library, and msdn library also said "SqlConnection.Close() method releases the connection to the connection pool, or closes the connection if connection pooling is disabled."
I created a stored procedure in Northwind db like this:create proc dbo.TestProc
as
begin transaction
 delete dbo.[Order Details];
go  I created some code to execute the stored procedure like this:
         
        static public void Test()
        {
            string connectionString = "Integrated Security=true;Initial Catalog=Northwind;server=(local);Connection Lifetime = 60";
            
            SqlConnection sqlconnection = new SqlConnection();
            sqlconnection.ConnectionString = connectionString;
            SqlCommand sqlCommand = new SqlCommand("dbo.TestProc");
            sqlCommand.CommandType = CommandType.StoredProcedure;
            sqlCommand.Connection = sqlconnection;
            try
            {
                sqlconnection.Open();
                sqlCommand.ExecuteNonQuery();
            }
            catch
            {                
            }
            finally
            {
                sqlconnection.Close();
            }
        }Clearly, the database connection is put into the pool after the Sqlconnection.Close() is invoked. But after the Sqlconnection.Close() is invoked, the transaction is still on the database connection, and at that time the connection is in the pool.
I have create a query like this:select open_tran
from dbo.sysprocesses as p
 inner join dbo.sysdatabases as d
  on p.dbid = d.dbid
where d.[name] = N'Northwind';and the result has only one row:open_tran 
--------- 
1(1 row(s) affected)
I think the not completed(committed or rollbacked) transaction in the example procedure is not a pending transaction, because the Sqlconnection.Close() method is not rollback the transaction. 
Thus, my question are:a)  What is a pending transaction?
b)  How to check transaction count in c# code, besides execute T-SQL batch include "@@error".
c)  Is the not completed transaction in T-SQL only manual committed? Are there better methods to solve the not completed transaction problem?

解决方案 »

  1.   

    try:  static public void Test() 
            { 
                string connectionString = "Integrated Security=true;Initial Catalog=Northwind;server=(local);Connection Lifetime = 60"; 
                
                SqlConnection sqlconnection = new SqlConnection(); 
                sqlconnection.ConnectionString = connectionString; 
                SqlCommand sqlCommand = new SqlCommand("dbo.TestProc"); 
                sqlCommand.CommandType = CommandType.StoredProcedure; 
                sqlCommand.Connection = sqlconnection; 
                SqlTransaction myTrans;            // Start a local transaction,and it's a pending local transaction
                myTrans = sqlconnection.BeginTransaction();
                sqlCommand.Transaction = myTrans;
                try 
                { 
                    sqlconnection.Open(); 
                    sqlCommand.ExecuteNonQuery(); 
                    myTrans.Commit();
                } 
                catch 
                {  
                    try
                    {
                          myTrans.Rollback();
                    }
                    catch (SqlException ex)
                    {
                          if (myTrans.Connection != null)
                          {
                                //code....
                          }
                    }
                  
                } 
                finally 
                { 
                    sqlconnection.Close(); 
                } 
            } 
      

  2.   

    happyflystone: 
    I exactly think your code is feasible! Thank you very much!But, msdn library said: "Pending transactions started using Transact-SQL or BeginTransaction are automatically rolled back. "So, these are my question:a) How to start a pending transaction using Transact-SQL? 
    b) And the what's meaning of the word of pending? Is the word of Pending used to express a status of transactions, or express a kind of transactions that are started by some special ways?