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?
- <Resource name="jdbc/db1" auth="Container "
- type="Com.mchange.v2.c3p0.ComboPooledDataSource"
- factory="Org.apache.naming.factory.BeanFactory"
- driverclass="Oracle.jdbc.driver.OracleDriver"
- jdbcurl="JDBC:ORACLE:THIN:@127.0.0.1:1521:ORCL"
- user="Scott"
- password="123456"
- minpoolsize="Ten"
- maxpoolsize="
- maxidletime="1800"
- acquireincrement="2"
- maxstatements="0"
- initialpoolsize="
- idleconnectiontestperiod="
- acquireretryattempts="
- acquireretrydelay="+"
- testconnectiononcheckin="false"
- breakafteracquirefailure="false"
- testconnectiononcheckout="false"/>
Second, in Tomcat under Content.xml to configure the data source corresponding Jndi
[HTML]View PlainCopyprint?
- <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?
- <Bean id= "dataSource" class="Org.springframework.jndi.JndiObjectFactoryBean">
- <property name= "jndiname" value="java:comp/env/jdbc/db1" />
- </Bean>
Second, write code to get the data source directly through Jndi
[Java]View PlainCopyprint?
- try {
- Context CTX = new InitialContext ();
- Context Envcontext = (context) ctx.lookup ("java:/comp/env"); java:/comp/env as fixed path
- DataSource ds = (DataSource) envcontext.lookup ("jdbc/db1"); Data sources set in Tomcat
- Connection con = ds.getconnection ();
- System.out.println (con);
- } catch (Exception e) {
- //TODO auto-generated catch block
- E.printstacktrace ();
- }
Configuring the Tomcat global C3P0 connection pool