1.tomcat自带的连接池
E:/apache-tomcat-8.0.23/conf/context.xml中配置如下代码
<?xml version='1.0' encoding='utf-8'?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- The contents of this file will be loaded for each web application -->
<Context>
<!-- Default set of monitored resources. If one of these changes, the -->
<!-- web application will be reloaded. -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
-->
<Resource
<!-- 指定资源池的Resource的JNDI的名字,就是给连接池起的名字,在连接数据库的时候会用到这个名字 -->
name="jdbc/yimai"
<!-- 管理权限,指定管理Resource的Manager,可以是Container或Application -->
auth="Container"
<!--指出Resource所属的类名,是什么类型的数据源-->
type="javax.sql.DataSource"
<!-- 连接池最大激活的连接数,设为0表示无限制-->
maxActive="100"
<!-- 连接池中最多可空闲的连接数 -->
maxIdle="30"
<!-- 为连接最大的等待时间,单位毫秒,如果超过此时间将接到异常。设为-1表示无限制-->
maxWait="10000"
<!-- 数据库用户名 -->
username="yimai"
<!-- 数据库密码 -->
password="666"
<!-- 数据库驱动类 -->
driverClassName="oracle.jdbc.driver.OracleDriver"
<!-- 数据库连接url-->
url="jdbc:oracle:thin:@localhost:1521:orcl" />
</Context>
在dao中调用数据源文件
public class BaseDAO {
/**
* 获取数据库连接
*
* @return conn
* @throws Exception
*/
public Connection getConn() throws Exception {
Context context;
Connection conn = null;
try {
context = new InitialContext();
// 获取服务器的context.xml配置的路径资源
DataSource ds = (DataSource) context
.lookup("java:comp/env/jdbc/yimai");
conn = ds.getConnection();
} catch (Exception e) {
throw new Exception(e.getMessage());
}
return conn;
}
2.c3p0连接池
spring对c3p0的支持是非常好的
首先配置c3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="driverClass">oracle.jdbc.driver.OracleDriver</property>//数据库连接驱动
<property name="jdbcUrl">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property>//
<property name="user">scott</property>
<property name="password">tiger</property>
<property name="initialPoolSize">5</property>//初始化连接数
<property name="maxPoolSize">5</property>//最大连接数
<property name="minPoolSize">1</property>最小连接数
<property name="acquireIncrement">2</property>连接池连接耗尽每次增加2个
</default-config>
</c3p0-config>
jdbcUtil工具类你加载配置文件获取数据库资源
//缺省下 加载src下面的c3p0-config.xml
private static ComboPooledDataSource dataSource = new ComboPooledDataSource();
public static ComboPooledDataSource getDataSource() {
return dataSource;
}
}