Cannot allocate more connections. Connection pool is at maximum. Increase Max Pool Size我的是定时程序,程序一直开启,一个小时执行一个方法 我想问,出现这个错误是服务器的问题,还是我的程序问题, 要如何解决?谢谢大家!
补充下问题, getDate()//执行这个方法的时候是访问(Rhapso)数据库 getFat()//执行这个方法的时候是访问(muete)数据库 在执行getDate()方法时出现了,上面的问题(Cannot allocate more connections. Connection pool is at maximum. Increase Max Pool Size ) 但当getDate()执行完,getFat()方法是可以执行的, 两个方法连接数据库我都用的是 using (AseConnection connection = new AseConnection(configString)) { using (AseCommand cmd = new AseCommand(sql, connection)) { try { cmd.CommandTimeout = 500; //connection.Open(); AseDataAdapter ada = new AseDataAdapter(cmd); DataSet ds = new DataSet(); ada.Fill(ds); return ds.Tables[0]; } catch (Sybase.Data.AseClient.AseException e) { DataTable ds1 = new DataTable(); return ds1; //throw (new Exception("Error in the Database " + e.Message)); } 我用的是这个链接呀,using 不是自己开自己关吗? } } 这个是什么问题呢?
SqlConnection conn = new SqlConnection(@"Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=Dingger;Data Source=YFNET2012\sql2000"); SqlCommand com = new SqlCommand("select 1",conn); if (conn.State == ConnectionState.Closed) conn.Open();//要用时就打开, object o = com.ExecuteScalar(); intValue = Convert.ToInt32(o); //用完后就关 conn.Close();//一个关 conn.Dispose(); 这个可以释放资源
SqlConnection conn = new SqlConnection(@"Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=Dingger;Data Source=YFNET2012\sql2000");
SqlCommand com = new SqlCommand("select 1",conn);
conn.Open();//一个开
object o = com.ExecuteScalar();
intValue = Convert.ToInt32(o);
conn.Close();//一个关
conn.Dispose();
{
using (AseCommand cmd = new AseCommand(sql, connection))
{
try
{
cmd.CommandTimeout = 500;
//connection.Open();
AseDataAdapter ada = new AseDataAdapter(cmd);
DataSet ds = new DataSet();
ada.Fill(ds);
return ds.Tables[0];
}
catch (Sybase.Data.AseClient.AseException e)
{
DataTable ds1 = new DataTable();
return ds1;
//throw (new Exception("Error in the Database " + e.Message));
}
我用的是这个链接呀,using 不是自己开自己关吗?
}
}
给个范例(其中一部分)public void Close()
{
if (Connection.State.Equals(ConnectionState.Open))
{
Connection.Close(); //连接处于打开状态,关闭连接
}
} /// <summary>
/// 析构函数,释放非托管资源
/// </summary>
~DataBase()
{
try
{
if (Connection != null)
Connection.Close();
}
catch{}
try
{
Dispose();
}
catch{}
} //公有方法,释放资源
public void Dispose()
{
if (Connection != null) // 确保连接被关闭
{
Connection.Dispose();
Connection = null;
}
}
getDate()//执行这个方法的时候是访问(Rhapso)数据库
getFat()//执行这个方法的时候是访问(muete)数据库
在执行getDate()方法时出现了,上面的问题(Cannot allocate more connections. Connection pool is at maximum. Increase Max Pool Size
)
但当getDate()执行完,getFat()方法是可以执行的,
两个方法连接数据库我都用的是
using (AseConnection connection = new AseConnection(configString))
{
using (AseCommand cmd = new AseCommand(sql, connection))
{
try
{
cmd.CommandTimeout = 500;
//connection.Open();
AseDataAdapter ada = new AseDataAdapter(cmd);
DataSet ds = new DataSet();
ada.Fill(ds);
return ds.Tables[0];
}
catch (Sybase.Data.AseClient.AseException e)
{
DataTable ds1 = new DataTable();
return ds1;
//throw (new Exception("Error in the Database " + e.Message));
}
我用的是这个链接呀,using 不是自己开自己关吗?
}
}
这个是什么问题呢?
SqlCommand com = new SqlCommand("select 1",conn);
if (conn.State == ConnectionState.Closed)
conn.Open();//要用时就打开,
object o = com.ExecuteScalar();
intValue = Convert.ToInt32(o);
//用完后就关
conn.Close();//一个关
conn.Dispose();
这个可以释放资源
你在try catch 后 加个 finally
然后 把数据库的关闭写进去这样 不管什么情况数据库联接都关闭了
conn.Dispose();
就是这个dispose()方法 使用这个方法释放连接!还有在你访问数据库的时候记得也要用dispose()方法把资源释放掉哦!以前也是一样的问题!呵呵!开始写代码都是这样的!
可以用Using或者执行完之后关闭
注意用完及时关闭数据库
试用一下try catch 可以用 来捕捉错误。
using 是个作用域