使用 Oracle DB 在 Java Web 应用程序中超过最大空闲时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1573669/
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
Exceeding Maximum Idle Time in Java Web Application with Oracle DB
提问by Rintoul
I have a Java web application connecting to an Oracle database running on another machine (not sure if this is relevant or not). I am using DBCP for connection pooling. The web application is running in JBoss 4.2.2 and we are defining our datasource as a bean in Spring.
我有一个 Java Web 应用程序连接到在另一台机器上运行的 Oracle 数据库(不确定这是否相关)。我正在使用 DBCP 进行连接池。Web 应用程序在 JBoss 4.2.2 中运行,我们将数据源定义为 Spring 中的 bean。
We are using Hibernate for ORM.
我们正在将 Hibernate 用于 ORM。
We are getting errors occasionally like so: "ORA-02396: exceeded maximum idle time, please connect again".
我们偶尔会收到这样的错误:“ORA-02396:超出最大空闲时间,请重新连接”。
I have tried adding properties to our DBCP BasicDataSource called "removeAbandoned" (true) and "removeAbandondedTimeout" (120) to no avail.
我曾尝试向我们的 DBCP BasicDataSource 添加名为“removeAbandoned”(真)和“removeAbandondedTimeout”(120)的属性,但无济于事。
Any help would be appreciated. If I need to provide more information, please let me know - I'm not all that knowledgeable about the inner workings of connection pooling, etc...
任何帮助,将不胜感激。如果我需要提供更多信息,请告诉我 - 我对连接池的内部工作原理不太了解,等等......
回答by serg10
Try setting the testWhileIdle
property to true
when configuring your datasource. You'll also need a test query - for Oracle, something like select 1 from dual
will suffice.
在配置数据源时尝试将该testWhileIdle
属性设置为true
。您还需要一个测试查询 - 对于 Oracle,类似的东西select 1 from dual
就足够了。
This will prompt dbcp to nudge any idle connections to keep them fresh.
这将提示 dbcp 轻推任何空闲连接以保持它们新鲜。
You can also consider evicting connections that become idle if you don't mind recreating them once they are needed later. Have look at the documentationdescribing the configuration options for the minEvictableIdleTimeMillis
, timeBetweenEvictionRunsMillis
and maxIdle
/ minIdle
properties.
如果您不介意在以后需要它们时重新创建它们,您也可以考虑驱逐空闲的连接。查看描述,和/属性的配置选项的文档。minEvictableIdleTimeMillis
timeBetweenEvictionRunsMillis
maxIdle
minIdle
回答by Kevin
See this thread on the spring forums.
请参阅spring 论坛上的此主题。
The users either ended up switching to c3p0 or adding the testWhileIdleproperty to the spring configuration:
用户要么最终切换到 c3p0,要么将testWhileIdle属性添加到 spring 配置中:
<prop key="hibernate.dbcp.testWhileIdle">true</prop>
<prop key="hibernate.dbcp.validationQuery">
select 1 from dual
</prop>
回答by Gandalf
I would switch to the Oracle Universal Connection Pool library. It's optimized for Oracle JDBC Connections, but also will work with any JDBC driver if you ever switch. It's also JDBC 4.0 compliant and is actually being worked on. C3P0, Proxool, DBCP are all kind of stagnant. It will validate you connection by doing a ping to Oracle rather then a query also, which is much faster.
我会切换到 Oracle 通用连接池库。它针对 Oracle JDBC 连接进行了优化,但如果您切换,也可以与任何 JDBC 驱动程序一起使用。它也符合 JDBC 4.0,并且实际上正在开发中。C3P0、Proxool、DBCP 都有些停滞不前。它将通过对 Oracle 执行 ping 而不是查询来验证您的连接,这要快得多。