Oracle - 使用 spring 框架的连接池
我们正在尝试借助 Spring 框架来实现 Oracle 连接池。 我们使用的是DBCP连接池方法。 然而 DBCP 和 spring 之间的集成进展得不太顺利。
我们面临的问题是 DBCP 返回 PoolableConnections 对象,而 Oracle 需要 OracleConnection 对象。 (抛出ClassCastException)
看来这个问题在Oracle 11g中已经得到了处理。 不过我很好奇其他人如何使用 Oracle 10g 的 Spring 框架(使用 TOMCAT)实现 Oracle 连接池。
我们使用Ibatis作为ORM框架。
我确信有办法。 任何帮助表示赞赏。
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.
Problem that we face is that DBCP returns PoolableConnections Object while Oracle expects OracleConnection Objects. (Throws 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).
We use Ibatis as ORM framework.
I am sure there is a way. any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我会使用 Oracle 提供的解决方案,该解决方案包含在他们的 ojdbc jar 中。 旧的方法是使用 OracleConnectionPoolDataSource 类,但现在您可以在常规 OracleDataSource 上设置参数并获取连接池。
以下是在 Spring 中执行此操作的方法:
I would use Oracles supplied solution, which in included in their ojdbc jars. The older way was with the class OracleConnectionPoolDataSource but now you can set a parameter on a regular OracleDataSource and get connection pooling.
Here is how to do it in Spring:
我使用 C3PO 建立连接。 它还具有为您进行连接池的优势。 只需通过 Spring 配置文件定义一个类型为 com.mchange.v2.c3p0.ComboPooledDataSource (或类似)的数据源 bean。 在我遇到连接池问题之前,我什至使用了不建议用于生产用途的 spring (DriverManagerDataSource) 之一,因为它实际上并没有进行连接池。
这是一个弹簧配置示例。
然后 Spring 将 dataSource bean 注入 Hibernate 中,一切顺利。 您还需要在类路径上有 c3pO jar 文件...
I use C3PO to establish the connection. It has also the advantage to do connection pooling for you. Just define a datasource bean 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.
Here is an example spring configuration.
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...
如果您正在使用的话,您不应该实现自己的池。 Tomcat 已经为您做到了这一点,而是在 Tomcat 中定义一个数据源,并让您的 ORM 框架使用它(当您定义 Tomcat 数据源时,您可以在那里指定池配置)。
如果您可以发布一些代码片段,特别是相关的 Spring 上下文配置,我可以帮助您了解如何执行此操作。
以下 Tomcat 文档准确地向您展示了如何执行此操作:
顺便说一句,Tomcat也使用DBCP,最好依赖JNDI,因为它使你的代码更容易可移植(从一种环境到另一种环境 - 例如,开发到登台到生产,甚至跨应用程序服务器 - 例如,到 WebSphere、WebLogic 等)。
You should not implementing 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).
If you could post some code snippets, specifically, the relevant Spring context configurations, I can help provide you with how you'd do this.
Here's Tomcat documentation that shows you exactly how you do that:
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).
使用 CommonsDbcpNativeJdbcExtractor 来获取本机连接,而不是使用 SimpleNativeJdbcExtractor。 有用。
当使用包装连接但不包装语句的简单连接池时,SimpleNativeJdbcExtractor 通常就足够了。 但是,某些池(例如 Jakarta 的 Commons DBCP)包装了它们返回的所有 JDBC 对象:因此,您需要对它们使用特定的 NativeJdbcExtractor(例如 CommonsDbcpNativeJdbcExtractor)。
单击此处[http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/jdbc/support/nativejdbc/NativeJdbcExtractor.html]
Instead of Using SimpleNativeJdbcExtractor use CommonsDbcpNativeJdbcExtractor to get the native connection. it works.
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.
Click here [http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/jdbc/support/nativejdbc/NativeJdbcExtractor.html]
我也面临着和你一样的问题..所以我使用了Oracle Native连接池..它工作顺利..
这是详细信息的链接
http://www.lambdaprobe.org/d/oracle.shtml
谢谢!
普拉蒂克
I was also facing the same problem as you are.. so i used Oracle Native connection pool.. it works smoothly..
here is the link for the details
http://www.lambdaprobe.org/d/oracle.shtml
Thanks!
Pratik