database 池连接超时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20352677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pooled connection timed out
提问by Coder
On the production server sometimes randomly the connection fails to the ORacle database. I get a lot of
在生产服务器上,有时会随机连接到 ORacle 数据库失败。我得到了很多
Oracle.DataAccess.Client.OracleException
Pooled connection request timed out
at Oracle.DataAccess.Client.OracleException.HandleErrorHelper(Int32 errCode, OracleConnection conn, IntPtr opsErrCtx, OpoSqlValCtx* pOpoSqlValCtx, Object src, String procedure, Boolean bCheck, Int32 isRecoverable)
at Oracle.DataAccess.Client.OracleException.HandleError(Int32 errCode, OracleConnection conn, IntPtr opsErrCtx, Object src)
at Oracle.DataAccess.Client.OracleConnection.Open()
at ws.DataConnection() in path.
I searched for some solutions but no luck. It's strange that the Exception has no Identifier like Ora-123... I use OracleDataAccess client. Sometimes this problem is for 5-10 seconds, sometimes I have to restart the IIS (6.1, Windows Server 2008 R2) to solve the problem. Is frustrating... I cannot set pooling=false because we have a huge website. Any solutions?
我搜索了一些解决方案,但没有运气。奇怪的是异常没有像 Ora-123 这样的标识符......我使用 OracleDataAccess 客户端。有时这个问题持续5-10秒,有时我必须重新启动IIS(6.1,Windows Server 2008 R2)才能解决问题。令人沮丧......我无法设置 pooling=false 因为我们有一个巨大的网站。任何解决方案?
采纳答案by Nick Zimmerman
The most common cause of this that I am aware of is failing to properly handle IDisposable objects associated with Oracle.DataAccess.Client.
我所知道的最常见的原因是无法正确处理与 Oracle.DataAccess.Client 关联的 IDisposable 对象。
There is probably some code that you have out there that is not properly disposing of some objects. This will cause Oracle to hold on to connections that are not actually in use causing you to run out of available connections in the pool. Restarting IIS solves it because it kills all those connections.
您可能有一些代码没有正确处理某些对象。这将导致 Oracle 保留实际未使用的连接,从而导致池中的可用连接用完。重新启动 IIS 可以解决它,因为它会杀死所有这些连接。
Review your code carefully and make sure that all of the IDisposable objects are being properly disposed of or encapsulated in using statements.
仔细检查您的代码并确保所有 IDisposable 对象都被正确处理或封装在 using 语句中。
回答by drew_w
The most common connection issues that I have seen are:
我见过的最常见的连接问题是:
- The application is leaving connections open. This is using up all the available connections and so connections are randomly being refused. The easiest code solution for this is to make sure the application closes the connections as quickly as possible (rather than waiting for variables to leave scope). A "using" statement in .NET works well for this.
- Your connection pool may need to be recycled. You might refer to this site for more information: http://docs.oracle.com/cd/E11882_01/java.112/e12265/manage.htm#BABICIII
- You may just not have a large enough connection pool (default max size is 100). Try increasing this.
- 应用程序将连接保持打开状态。这会耗尽所有可用的连接,因此连接会被随机拒绝。最简单的代码解决方案是确保应用程序尽快关闭连接(而不是等待变量离开作用域)。.NET 中的“using”语句对此很有效。
- 您的连接池可能需要回收。您可以参考此站点以获取更多信息:http: //docs.oracle.com/cd/E11882_01/java.112/e12265/manage.htm#BABICIII
- 您可能只是没有足够大的连接池(默认最大大小为 100)。尝试增加这个。
One other site that might help is this one: http://blog.ilab8.com/2011/09/02/odp-net-pooling-and-connection-request-timed-out/
另一个可能有帮助的网站是这个:http: //blog.ilab8.com/2011/09/02/odp-net-pooling-and-connection-request-timed-out/
回答by Tod Thomson
The key is to call .Dispose() on your DbContext and/or make sure that your container is calling .Dispose() for you (by overriding dispose and ensuring that for each new DbContext you have a corresponding call to .Dispose()).
关键是在你的 DbContext 上调用 .Dispose() 和/或确保你的容器正在为你调用 .Dispose()(通过覆盖 dispose 并确保对于每个新的 DbContext 你有一个对 .Dispose() 的相应调用) .
You might like to use the destructor ~MyDbContext() as a quick hack just to check that calling Dispose() will fix the issue where your app is leaving connections open (i.e. not calling Dispose() on your DbContext).
您可能希望使用析构函数 ~MyDbContext() 作为快速黑客,只是为了检查调用 Dispose() 将解决您的应用程序保持连接打开的问题(即不在您的 DbContext 上调用 Dispose())。
It seems that with the Oracle provider you MUSTcall Dispose yourself (or have the container do it for you), otherwise you will leak / run out of connections.
似乎使用 Oracle 提供程序,您必须自己调用 Dispose(或让容器为您完成),否则您将泄漏/耗尽连接。
I can provide some example code if you like?
如果您愿意,我可以提供一些示例代码吗?
回答by Punisher
Make sure you are not connecting to the database recusively. Such as:
确保您没有以递归方式连接到数据库。如:
// collection to wrap several db records
private List<YourClassItems> list
get
{
if (Session["FOO"] == null)
{
// this method connect to the database
List<YourClass> lst = GetItems();
Session["FOO"] = lst;
return lst;
}
return (List<YourClass>)Session["FOO"];
}
// then we have the GetItems() method
private List<YourClass> GetItems()
{
// get several items from database.
while (read())
{
// assume this row is an item
RowItem i = read.Row;
// THIS might be your problem. It will recursively call this method
yourClassItems.Add(i);
}
//to solve this, create a List<YourClass> tempList and then
//yourClassItems = tempList outside the loop
}

