简体   繁体   中英

Tranaction and connection pooling in asp.net

I have used my database access code as below:

//gets the connection and create transaction
SqlTransaction transaction = SelectConnection();

try
{
    dsRecordDataset = new DataSet();
    SqlParameter[] param = new SqlParameter[1];
    string[] taleNames = new string[1];
    taleNames[0] = "RecordSet";
    param[0] = new SqlParameter("@Mode", Mode);
    SqlHelper.FillDataset(transaction, CommandType.StoredProcedure, 
                          "spProject", dsRecordDataset, taleNames, param);
    transaction.Commit();
    return dsRecordDataset;
}
catch (Exception ex)
{
    transaction.Rollback();
    throw ex;
}

Problem which I have found is I have never closed the connection so that the connection pool can overflow and give an error to the user sometimes. I need to clarify the following

  1. When does the connection pool reset according to default settings in asp.net?
  2. Does committing transaction has anything to do with the connection?

I tried to use following code after try catch block

finally
{
    if (transaction.Connection.State == ConnectionState.Open)
    {
        transaction.Connection.Close();
    }
}

but then the connection is null and gives a error.

  1. Is there any way to close the connection in above code?

when does the connection pool reset according to default settings in asp.net?

If MinPoolSize is either not specified in the connection string or is specified as zero, the connections in the pool will be closed after a period of inactivity. However, if the specified MinPoolSize is greater than zero, the connection pool is not destroyed until the AppDomain is unloaded and the process ends.

does committing transaction has anything to do with the connection? No, committing a connection does not (should not) close the connection

is there any way to close the connection in above code?

You can do this using either the Close or Dispose methods of the Connection object, or by opening all connections inside a using statement in C# so that they will be returned to the connection pool. Connections that are not explicitly closed might not be added or returned to the pool.

You got the answers, but for the question 3) you might find the using statement intresting in this case. However it means a code change, but it is a good way to keep the resource critical objects safe. (like your SqlConnection)

using (SqlConnection connection = new SqlConnection(connectionString))  
{  
    SqlCommand command = connection.CreateCommand();  

    command.CommandText = "mysp_GetValue";  
    command.CommandType = CommandType.StoredProcedure;  

    connection.Open();  
    object ret = command.ExecuteScalar();  
} 

It is just a very simple example, but this statement automaticly calls the Dispose method of an object which implements the IDisposable interface. (Like the SqlConnection)

If you are intrested in this, here you can find more details and explanations: Your Friend the C# Using Statement

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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