oracle 如何在 spring DriverManagerDataSource 上设置超时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3700795/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 21:30:39  来源:igfitidea点击:

How can I set a timeout on spring DriverManagerDataSource

javaoraclespringjdbc

提问by Grzenio

We are using DriverManagerDataSourcefrom the Spring framework (version 2.5) to pool connections to Oracle. However, it seems that these connections don't have any timeout defined - yesterday, after emergency database restart, we had a thread hanging on a socket read inside the database connection. How can I set the timeout, to say 10 mins, so that it raises an exception next time?

我们使用DriverManagerDataSourceSpring 框架(2.5 版)来汇集与 Oracle 的连接。然而,这些连接似乎没有定义任何超时——昨天,在紧急数据库重启后,我们有一个线程挂在数据库连接内的套接字读取上。如何设置超时,比如 10 分钟,以便下次引发异常?

采纳答案by Grzenio

I ended up changing the bean in the Spring context in the following way:

我最终通过以下方式更改了 Spring 上下文中的 bean:

<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" autowire="no">
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
    <property name="connectionProperties">
        <props>
            <prop key="oracle.net.READ_TIMEOUT">60000</prop>
        </props>
    </property>
</bean>

I don't know if it works yet.

我不知道它是否还有效。

回答by gpeche

Oracle has a builtin connection pool: oracle.jdbc.pool.OracleDataSource. I recommend you use that directly. The reference for Oracle JDBC (10gR2, other versions will be similar) is in http://download.oracle.com/docs/cd/B19306_01/java.102/b14355/toc.htm. Points of interest:

Oracle 有一个内置的连接池:oracle.jdbc.pool.OracleDataSource. 我建议你直接使用它。Oracle JDBC(10gR2,其他版本类似)的参考位于http://download.oracle.com/docs/cd/B19306_01/java.102/b14355/toc.htm。兴趣点:

回答by Jon Freedman

If your Oracle driver implementation supports a timeout property you can pass it through to the underlying implementation via getConnectionProperties().put(key, timeout)

如果您的 Oracle 驱动程序实现支持超时属性,您可以通过以下方式将其传递给底层实现 getConnectionProperties().put(key, timeout)

I'm assuming you realise that DriverManagerDataSourceisn't a connection pool? From the docs:

我假设您意识到这DriverManagerDataSource不是连接池?从文档:

NOTE: This class is not an actual connection pool; it does not actually pool Connections.It just serves as simple replacement for a full-blown connection pool, implementing the same standard interface, but creating new Connections on every call. [..] If you need a "real" connection pool outside of a J2EE container, consider Apache's Jakarta Commons DBCPor C3P0.

注意:这个类不是一个实际的连接池;它实际上并不池连接。它只是作为成熟连接池的简单替代品,实现相同的标准接口,但在每次调用时创建新的连接。[..] 如果您需要 J2EE 容器之外的“真实”连接池,请考虑 Apache 的 Jakarta Commons DBCPC3P0

You may also be interested in OCI Connection Pooling?

您可能还对OCI 连接池感兴趣?