简体   繁体   中英

Using c3p0 connection Pooling in a tomcat Spring based App

I have a Spring Based Web App running under tomcat 6. Now, I want to use c3p0 connection pooling instead of tomcat's default DBCP. So, from the c3p0 help doc, I have defined the data source in context.xml something like:

<Resource name="jdbc/sample" auth="Container"
     driverClassName="oracle.jdbc.driver.OracleDriver"
     url="jdbc:oracle:thin:@someServer:1551:xyz"
     username="userName"
     password="pwd"
     validationQuery="SELECT 1 FROM dual"
     testOnBorrow="true"
     testWhileIdle="true"
     factory="org.apache.naming.factory.BeanFactory" 
     type="com.mchange.v2.c3p0.ComboPooledDataSource" 
     maxPoolSize="20" 
     minPoolSize="5" 
     acquireIncrement="1" 
   />

Now, the documentation says, I should Include the following in web.xml :

<resource-ref>
  <res-ref-name>jdbc/sample</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
</resource-ref> 

I also have the following in applicationContext.xml :

<jee:jndi-lookup id="sampleDataSource" resource-ref="true"
    jndi-name="jdbc/sample" />

When I start the tomcat, I get

javax.naming.NameNotFoundException: Name jdbc is not bound in this Context

Without c3p0 and using default connection pooling in tomcat6 works fine.

Any help would be appreciated.

this thread is old, so the answer is for future use - i had the same issue (this is how i got to this thread).

i solved the problem after performing a small research. there are some definitions, that the used data source doesn't support. when you remove those definitions and rename others, the data source is created with no problem and without a need to the resourceLink mentioned above.

in the following link you can find the list of supported definitions. http://www.mchange.com/projects/c3p0/#tomcat-specific

the following link is to the data source's java doc. according to the methods that are listed there, you can configure the resource tag in the context.xml file. http://www.mchange.com/projects/c3p0/apidocs/com/mchange/v2/c3p0/ComboPooledDataSource.html

You need an entry in context.xml as well, eg:

<Context antiJARLocking="true" swallowOutput="true">

    <ResourceLink
     global="jdbc/sample"
     name="jdbc/sample"
     type="javax.sql.DataSource" />
</Context>

I'd agree that J2EE has far too many levels of indirection. Note that context.xml can reside either in the "conf" directory of Tomcat or the META-INF directory of the webapp (depending if you want the data source to be for all web apps or only a specific one). With the context.xml entries, you don't need the resource-ref in web.xml.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM