Database connection release problems
" Timeout expired. Timeout period has expired, but has yet to get a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached. "
A look is the database connection pool is full, it should be no reason for the release of the open, but I have to use the Connection object using timely released, how will this happen? It took a long time and finally to reason, so write down the record it.
Extended Tips:
- connections SQL database query the database directly executable :() the SELECT [ dec ] .client_net_address, [ des ] . [ Program_name ] , [ des ] . [ Host_name ] , the Count ( [ dec ] .session_id) AS the connection_count the FROM SYS .dm_exec_sessions the AS [ des ] the INNER the JOIN sys.dm_exec_connections the AS [ On Dec ] the ON [ des ].session_id = [dec].session_id GROUP BY [dec].client_net_address, [des].[program_name], [des].[host_name] ORDER BY [des].[program_name], [dec].[client_net_address]
Item code:
//创建连接对象的工厂类 public class ConnectionFactory { private static readonly string connString = ConfigurationManager.ConnectionStrings["SQLServerDatabase"].ConnectionString; public static IDbConnection CreateConnection() { IDbConnection conn = new SqlConnection(connString); conn.Open(); return conn; } } //UserInfoDAL类 public class UserInfoDAL:IDAL.IUserInfoDAL { private IDbConnection _conn; public IDbConnection Conn { get { //工厂实例化一个连接对象 return _conn = ConnectionFactory.CreateConnection(); } } //根据id获取entity public UserInfo GetEntity(string id) { using (Conn) { string query = "select * from UserInfo where UserInfo_id = @UserInfo_id"; //使用dapper return userInfo = Conn.Query<UserInfo>(query, new { UserInfo_id = id }).SingleOrDefault(); } } }
Code is basically a form of the above, it seems there is nothing wrong with it. Here's to debug it.
First create a unit test:
[TestMethod] public void TestConnectionCount() { SQLServerDAL.UserInfoDAL userInfoDAL = new SQLServerDAL.UserInfoDAL(); Model.UserInfo userInfo = userInfoDAL.GetEntity("3"); userInfo = userInfoDAL.GetEntity("3"); userInfo = userInfoDAL.GetEntity("3"); userInfo = userInfoDAL.GetEntity("3"); }
The reason is because the discovery of improper use get, they simply do not get the current solutions:
public class UserInfoDAL:IDAL.IUserInfoDAL { public IDbConnection Conn; public UserInfo GetEntity(string id) { using (Conn=ConnectionFactory.CreateConnection()) { string query = "select * from UserInfo where UserInfo_id = @UserInfo_id"; return Conn.Query<UserInfo>(query, new { UserInfo_id = id }).SingleOrDefault(); } } }