server.xml 文件中加入如下内容,具体设置可以根据情况变更:
<GlobalNamingResources>
<Resource auth="Container" description="DB Connection" driverClass="oracle.jdbc.driver.OracleDriver" maxPoolSize="100" minPoolSize="10" acquireIncrement="3" name="jdbc/dataSourceDB" user="itmc" password="itmc" factory="org.apache.naming.factory.BeanFactory" type="com.mchange.v2.c3p0.ComboPooledDataSource" jdbcUrl="jdbc:oracle:thin:@10.111.0.78:1521:orcl" />
</GlobalNamingResources>
context.xml文件中加入如下内容:
<ResourceLink name="jdbc/dataSourceDB" global="jdbc/dataSourceDB" type="javax.sql.DataSource"/>
将oracle和c3p0驱动包放入tomcat的lib文件夹下,一般是如下4个文件:
ojdbc14_g.jar
c3p0-oracle-thin-extras-0.9.1.2.jar
c3p0-0.9.1.2.jar
c3p0-0.9.1.2-jdk1.3.jar
这样一个连接池就配好了,在程序里可以通过如下方式获得连接:
InitialContext ic; try { ic = new InitialContext(); DataSource dataSource = (DataSource) ic.lookup("java:comp/env/jdbc/dataSourceDB");
Connection conn = dataSource.getConnection();
......... } catch (NamingException e) { // TODO Auto-generated catch block e.printStackTrace(); }