Configuring the Tomcat global C3P0 connection pool

Source: Internet
Author: User

Because multiple applications in a project access the same database and are deployed under the same tomcat, it is not necessary to configure connection pooling information for each application, which may result in uneven distribution of resources in the database, so this situation can be fully configured with a Tomcat global connection pool, all of which are accessed by the application involved. Make the database resources fully utilized.

I use the C3P0 connection pool, although the C3P0 has not done the relevant performance test, but the network review is good, using 0.9.1 version, Tomcat uses 7.0.32

The configuration is divided into several steps:

First, configure the data source in Tomcat's Server.xml

Locate the Globalnamingresources node and add the following in it (see the C3P0 official note for specific parameter meanings):

[HTML]View PlainCopyprint?
  1. <Resource name="jdbc/db1" auth="Container "
  2. type="Com.mchange.v2.c3p0.ComboPooledDataSource"
  3. factory="Org.apache.naming.factory.BeanFactory"
  4. driverclass="Oracle.jdbc.driver.OracleDriver"
  5. jdbcurl="JDBC:ORACLE:THIN:@127.0.0.1:1521:ORCL"
  6. user="Scott"
  7. password="123456"
  8. minpoolsize="Ten"
  9. maxpoolsize="
  10. maxidletime="1800"
  11. acquireincrement="2"
  12. maxstatements="0"
  13. initialpoolsize="
  14. idleconnectiontestperiod="
  15. acquireretryattempts="
  16. acquireretrydelay="+"
  17. testconnectiononcheckin="false"
  18. breakafteracquirefailure="false"
  19. testconnectiononcheckout="false"/>



Second, in Tomcat under Content.xml to configure the data source corresponding Jndi

[HTML]View PlainCopyprint?
    1. <resourcelink name="jdbc/db1" global="jdbc/db1" type="Javax.sql.DataSource" />

Third, put C3P0 jar package, database driver package into Tomcat under the Lib directory

Application access Method:

One, based on spring management, you only need to configure the data source in spring to

[HTML]View PlainCopyprint?
    1. <Bean id= "dataSource" class="Org.springframework.jndi.JndiObjectFactoryBean">
    2. <property name= "jndiname" value="java:comp/env/jdbc/db1" />
    3. </Bean>


Second, write code to get the data source directly through Jndi

[Java]View PlainCopyprint?
    1. try {
    2. Context CTX = new InitialContext ();
    3. Context Envcontext = (context) ctx.lookup ("java:/comp/env"); java:/comp/env as fixed path
    4. DataSource ds = (DataSource) envcontext.lookup ("jdbc/db1"); Data sources set in Tomcat
    5. Connection con = ds.getconnection ();
    6. System.out.println (con);
    7. } catch (Exception e) {
    8. //TODO auto-generated catch block
    9. E.printstacktrace ();
    10. }

Configuring the Tomcat global C3P0 connection pool

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.