views:

5328

answers:

5

Hi,

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. (Thorws Class cast exception)

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.

+2  A: 

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.

<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...

raoulsson
can you please provide some link for more details on this. A simple example for configuration would be a great pointer.
Priyank
hibernate also uses c3p0
Boris Pavlović
yes right, but you have to configure it, like so...
raoulsson
I see you are using Oracle xpress edition? Have you tried with a thin client without express edition? any pointers?
Priyank
I am not using only express. I also use the same setup for something like 11g...
raoulsson
Why use c3p0 when Oracle comes with connection pooling capabilities?
Gandalf
+1  A: 

Hi Priyank,

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

Pratik
+2  A: 

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).

Jack Leow
+3  A: 

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:

<bean id="datasource" class="oracle.jdbc.pool.OracleDataSource" destroy-method="close">
   <property name="connectionCachingEnabled" value="true" />
   <proerty 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>
Gandalf
A: 

One needs to use your own connection pooling with oracle 10g when using stupid Oracle specific features like XML DB...

George K