Java Oracle - 使用 spring 框架的连接池

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

Oracle - connection Pooling with spring framework

javaspringtomcatoracle10gibatis

提问by Priyank

We are trying to implement Oracle connection pooling with the help of Spring Framework. We are using DBCP connection pooling method. However the integration between DBCP and spring doesn't go down that well.

我们试图在 Spring Framework 的帮助下实现 Oracle 连接池。我们正在使用 DBCP 连接池方法。但是 DBCP 和 spring 之间的集成并没有那么好。

Problem that we face is that DBCP returns PoolableConnections Object while Oracle expects OracleConnection Objects. (Throws ClassCastException)

我们面临的问题是 DBCP 返回 PoolableConnections 对象,而 Oracle 期望 OracleConnection 对象。(抛出 ClassCastException)

It seems that this problem has been handled in Oracle 11g. However I am curious as to how others have implemented Oracle connection pooling using spring framework for Oracle 10g (Using TOMCAT).

这个问题好像在Oracle 11g中已经处理过了。但是,我很好奇其他人如何使用 Oracle 10g 的 spring 框架(使用 TOMCAT)实现 Oracle 连接池。

We use Ibatis as ORM framework.

我们使用 Ibatis 作为 ORM 框架。

I am sure there is a way. any help is appreciated.

我确定有办法。任何帮助表示赞赏。

采纳答案by Gandalf

I would use Oracles supplied solution, which in included in their ojdbc jars. The older way was with the class OracleConnectionPoolDataSourcebut now you can set a parameter on a regular OracleDataSourceand get connection pooling.

我将使用 Oracle 提供的解决方案,该解决方案包含在他们的 ojdbc jar 中。较旧的方法是使用OracleConnectionPoolDataSource类,但现在您可以在常规OracleDataSource上设置参数并获得连接池。

Here is how to do it in Spring:

这是在 Spring 中执行此操作的方法:

<bean id="datasource" class="oracle.jdbc.pool.OracleDataSource" destroy-method="close">
   <property name="connectionCachingEnabled" value="true" />
   <property name="URL" value="${jdbc.url}" />
   ...all your connection properties
   <property name="connectionCacheProperties">
      <props merge="default">
         <prop key="MinLimit>3</prop>
         <prop key="MaxLimit">20</prop>
      </props>
   </property>
</bean>

回答by raoulsson

I use C3PO to establish the connection. It has also the advantage to do connection pooling for you. Just define a datasourcebean of type e.g. com.mchange.v2.c3p0.ComboPooledDataSource (or similar) through your spring config files. Before I run into troubles with connection pooling, I even used one of spring (DriverManagerDataSource) that is not advised for production use, because it is not actually doing the connection pooling.

我使用 C3PO 建立连接。为你做连接池也有好处。只需通过 spring 配置文件定义一个类型为 com.mchange.v2.c3p0.ComboPooledDataSource(或类似的)的数据源bean。在遇到连接池问题之前,我什至使用了不建议用于生产的 spring (DriverManagerDataSource) 之一,因为它实际上并没有进行连接池。

Here is an example spring configuration.

这是一个示例弹簧配置。

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/>
    <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:XE"/>
    <property name="user" value="username"/>
    <property name="password" value="secret"/>
    <property name="minPoolSize" value="5"/>
    <property name="maxPoolSize" value="20"/>
    <property name="acquireIncrement" value="1"/>
    <property name="idleConnectionTestPeriod" value="100"/>
    <property name="maxStatements" value="0"/>
    <property name="checkoutTimeout" value="60000"/>

Spring then injects the dataSource bean into Hibernate and all is well. You will also need to have the c3pO jar file on your classpath...

然后 Spring 将 dataSource bean 注入 Hibernate,一切正常。您还需要在类路径上有 c3pO jar 文件...

回答by Pratik Garg

I was also facing the same problem as you are.. so i used Oracle Native connection pool.. it works smoothly..

我也遇到了和你一样的问题..所以我使用了 Oracle Native 连接池..它工作顺利..

here is the link for the details http://www.lambdaprobe.org/d/oracle.shtml

这是详细信息的链接 http://www.lambdaprobe.org/d/oracle.shtml

Thanks! Pratik

谢谢!普拉蒂克

回答by Hyman Leow

You should notimplementing your own pooling, if that's what you're using. Tomcat already does that for you, instead, define a data source in Tomcat, and have your ORM framework use it (when you define your Tomcat data source, you can specify the pool configurations there).

你应该没有实现自己的池,如果这就是你使用的是什么。Tomcat 已经为您做了这些,而是​​在 Tomcat 中定义一个数据源,并让您的 ORM 框架使用它(当您定义 Tomcat 数据源时,您可以在那里指定池配置)。

If you could post some code snippets, specifically, the relevant Spring context configurations, I can help provide you with how you'd do this.

如果您可以发布一些代码片段,特别是相关的 Spring 上下文配置,我可以帮助您提供如何执行此操作的信息。

Here's Tomcat documentation that shows you exactly how you do that:

这是 Tomcat 文档,它向您展示了如何做到这一点:

Incidentally, Tomcat uses DBCP also, and it's better to rely on JNDI, as it makes your code more portable (from one environment to another - e.g., dev to staging to production, or even across application servers - e.g., to WebSphere, WebLogic etc).

顺便说一句,Tomcat 也使用 DBCP,最好依赖 JNDI,因为它使您的代码更具可移植性(从一个环境到另一个环境 - 例如,从开发到过渡到生产,甚至跨应用程序服务器 - 例如,到 WebSphere、WebLogic 等)。

回答by user2704686

Instead of Using SimpleNativeJdbcExtractor use CommonsDbcpNativeJdbcExtractor to get the native connection. it works.

而不是使用 SimpleNativeJdbcExtractor 使用 CommonsDbcpNativeJdbcExtractor 来获取本机连接。有用。

When working with a simple connection pool that wraps Connections but not Statements, a SimpleNativeJdbcExtractor is often sufficient. However, some pools (like Jakarta's Commons DBCP) wrap all JDBC objects that they return: Therefore, you need to use a specific NativeJdbcExtractor (like CommonsDbcpNativeJdbcExtractor) with them.

当使用一个包含连接而不是语句的简单连接池时,SimpleNativeJdbcExtractor 通常就足够了。但是,一些池(如 Jakarta 的 Commons DBCP)包装了它们返回的所有 JDBC 对象:因此,您需要对它们使用特定的 NativeJdbcExtractor(如 CommonsDbcpNativeJdbcExtractor)。

Click here [http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/jdbc/support/nativejdbc/NativeJdbcExtractor.html]

点击这里 [http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/jdbc/support/nativejdbc/NativeJdbcExtractor.html]