简体   繁体   English

ASP.NET中的事务和连接池

[英]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? 什么时候根据asp.net中的默认设置重置连接池?
  2. Does committing transaction has anything to do with the connection? 提交事务与连接有关系吗?

I tried to use following code after try catch block 我在尝试catch块后尝试使用以下代码

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? 什么时候根据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. 如果未在连接字符串中指定MinPoolSize或将其指定为零,则一段时间不活动后,池中的连接将关闭。 However, if the specified MinPoolSize is greater than zero, the connection pool is not destroyed until the AppDomain is unloaded and the process ends. 但是,如果指定的MinPoolSize大于零,则直到AppDomain卸载并且过程结束,连接池才会被销毁。

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. 您可以使用Connection对象的Close或Dispose方法,或通过在C#中的using语句内打开所有连接,以将它们返回到连接池中来执行此操作。 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. 您已经找到了答案,但是对于问题3),在这种情况下,您可能会发现using语句很有趣。 However it means a code change, but it is a good way to keep the resource critical objects safe. 但是,这意味着更改代码,但这是确保资源关键对象安全的好方法。 (like your SqlConnection) (例如您的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. 这只是一个非常简单的示例,但是此语句自动调用实现IDisposable接口的对象的Dispose方法。 (Like the SqlConnection) (就像SqlConnection)

If you are intrested in this, here you can find more details and explanations: Your Friend the C# Using Statement 如果您对此感兴趣,可以在这里找到更多详细信息和解释: 您的朋友C#Using语句

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

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