Appendix D: Configuring c3p0 DataSources in Tomcat
You can easily configure Apache's Tomcat web application server to use c3p0 pooled DataSources. Below is a Tomcat 5.0 sample config to get you started. It's a fragment of Tomcat's conf/server.xml file, which should be modified to suit and placed inside a <Context> element.
For Tomcat 5.5, try something like the following (thanks to Carl F. Hall for the sample!):
The rest is standard J2EE stuff: You'll need to declare your DataSource reference in your web.xml file:
And you can access your DataSource from code within your web application like this:
That's it!
拷贝jdbc.jar和c3p0.jar到defaul/lib/下
Appendix E: JBoss-specific notes
To use c3p0 with JBoss:
- Place c3p0's jar file in the lib directory of your jboss server instance (e.g. ${JBOSS_HOME}/server/default/lib)
- Modify the file below, and save it as c3p0-service.xml in the deploy directory of your jboss server (e.g. ${JBOSS_HOME}/server/default/deploy). Note that parameters must be capitalized in this file, but otherwise they are defined as described above.
Please note: As of c3p0-0.9.1, the class name of the jboss configuration mbean has changed to com.mchange.v2.c3p0.jboss.C3P0PooledDataSource (from com.mchange.v2.c3p0.mbean.C3P0PooledDataSource), in order to distinguish what is really jboss-specific functionality from c3p0's more general JMX support.
The old jboss config mbeans are deprecated, but will still work. However, support for new configuration parameters will only be added under the new name. Updating requires a one-word change to your c3p0-service.xml, change "mbean" to "jboss" where your old file says 'code="com.mchange.v2.c3p0.mbean.C3P0PooledDataSource"'. Just do it!
<!DOCTYPE server>
<server>
<mbean code="com.mchange.v2.c3p0.mbean.C3P0PooledDataSource"
name="jboss:service=C3P0PooledDataSource">
<attribute name="JndiName">java:PooledDS</attribute>
<attribute name="JdbcUrl">jdbc:oracle:thin:@218.85.135.184:1522:BTS</attribute>
<attribute name="DriverClass">oracle.jdbc.driver.OracleDriver</attribute>
<attribute name="User">best_bts</attribute>
<attribute name="Password">best_bts</attribute>
<!-- Uncomment and set any of the optional parameters below -->
<!-- See c3p0's docs for more info. -->
<!-- <attribute name="AcquireIncrement">3</attribute> -->
<!-- <attribute name="AcquireRetryAttempts">30</attribute> -->
<!-- <attribute name="AcquireRetryDelay">1000</attribute> -->
<!-- <attribute name="AutoCommitOnClose">false</attribute> -->
<!-- <attribute name="AutomaticTestTable"></attribute> -->
<!-- <attribute name="BreakAfterAcquireFailure">false</attribute> -->
<!-- <attribute name="CheckoutTimeout">0</attribute> -->
<!-- <attribute name="ConnectionTesterClassName">0</attribute> -->
<!-- <attribute name="Description">A pooled c3p0 DataSource</attribute> -->
<!-- <attribute name="FactoryClassLocation"></attribute> -->
<!-- <attribute name="ForceIgnoreUnresolvedTransactions">true</attribute> -->
<!-- <attribute name="IdleConnectionTestPeriod">-1</attribute> -->
<!-- <attribute name="InitialPoolSize">3</attribute> -->
<!-- <attribute name="MaxIdleTime">0</attribute> -->
<!-- <attribute name="MaxPoolSize">15</attribute> -->
<!-- <attribute name="MaxStatements">0</attribute> -->
<!-- <attribute name="MaxStatementsPerConnection">0</attribute> -->
<!-- <attribute name="MinPoolSize">0</attribute> -->
<!-- <attribute name="NumHelperThreads">3</attribute> -->
<!-- <attribute name="PreferredTestQuery"></attribute> -->
<!-- <attribute name="TestConnectionOnCheckin">false</attribute> -->
<!-- <attribute name="TestConnectionOnCheckout">false</attribute> -->
<!-- <attribute name="UsesTraditionalReflectiveProxies">false</attribute> -->
<depends>jboss:service=Naming</depends>
</mbean>
</server>