连接池简述

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;

}
}




  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值