数据库连接池概念、种类、配置(DBCP\C3P0\JndI与Tomact配置连接池)

一、DBCP 连接:
DBCP 连接池是 Apache 软件基金组织下的一个开源连接池实现。

需要的 java 包
commons - dbcp - 1.2.1.jar //连接池的实现
commons - pool - 1.2.jar //连接池实现的依赖库
commons - collection.jar //连接池实现的依赖库

主要代码:
import org.apache.commons.dbcp.BasicDataSource;

// 数据库池

private static BasicDataSource dataSource = new BasicDataSource();

public static Connection getConn(){
Connection con = null ;
try {
   datasource.setDriverClassName("com.mysql.jdbc.Driver");
   datasource.setUrl("jdbc:mysql://localhost/mysql");
   datasource.setUsername("root");
   datasource.setPassword("root");
   datasource.setMaxActive(10);
   datasource.setMaxIdle(5);
   datasource.setMaxWait(0);
   con = datasource.getConnection();
  
} catch (Exception e) {
   // TODO: handl exception
   e.printStackTrace();
}
return con ;
}

二、C3PO 连接:
C3PO 连接池是一个优秀的连接池,推荐使用。

需要的 java 包
c3po0.902.jar

主要代码:
import com.mchange.v2.c3p0.ComboPooledDataSource;

// 数据库池

private static ComboPooledDataSource dataSource = new ComboPooledDataSource();

public static Connection getConn(){
Connection con = null ;
try {
   datasource.setDriverClass("com.mysql.jdbc.Driver");
   datasource.setJdbcUrl("jdbc:mysql://localhost/mysql");
   datasource.setUser("root");
   datasource.setPassword("root");
   datasource.setMaxActive(10);
   datasource.setMaxIdle(5);
   datasource.setMaxWait(0);
   con = datasource.getConnection();
  
} catch (Exception e) {
   // TODO: handl exception
   e.printStackTrace();
}
return con ;
}

三、JndI与 Tomact 连接池
此连接池不需要其他的 java 包

1、在 Tomact 的 conf 文件下的 context.xml 中加上如下:

<Resource name="jdbc/mysql"
    auth="Container"
    type="javax.sql.DataSource"
                  maxActive="100"
    maxIdle="30"
    maxWait="10000"
                  username="root"
    password="root"
    driverClassName="com.mysql.jdbc.Driver"
                url="jdbc:mysql://localhost:3306/test?autoReconnect=true"/>

说明:
type: 指数据源类型;
name: 为这个Resource 的名字 "jdbc/mysql" 它与 下一步 在 web.xml 的文件中;
   <res-ref-name>jdbc/mysql</res-ref-name>
********************** 中的 “ jdbc/mysql ” 名字可以任意取   但用到这个名字时,
********************** 一定要一样,有三个地方要用 :context.xml, web.xml, java 程序中的类中,
   和 index.jsp
   <sql:query var="rs" dataSource="jdbc/mysql">
    select bid, bname from books
    </sql:query>
   出现。

maxActive: 表示 dbcp 的最大连接数目,0 为不受限制;
maxIdle: 表示 dbcp 空闲时的数据库连接的最大数目;
maxWait: 表示 dbcp 中的数据库接处于空闲状态的最长时间,0 为不受制。
username: 数据库用户名;
password: 数据库登录密码;
driverClassName: 指数据库的 jdbc 驱动程序
url :中有个 test 它是 mysql 中的一个数据库的名字

2、web.xml configuration

<resource-ref>
    <description>DB Connection</description>
    <res-ref-name>jdbc/mysql</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

3、测试:
在 mysql 数据库中有个 test 数据库
test 中有表如下
         create table books(bid int , bname varchar(20))

html 测试:
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
         <%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>

<body>
    This is my JSP page. this is test JNDI datasource<br>
   <sql:query var="rs" dataSource="jdbc/mysql">
   select bid, bname from books
   </sql:query>

<c:forEach var="row" items="${rs.rows}">
   bid:${row.bid}<br>
       bname:${row.bname}
    </c:forEach>
   
</body>

java 程序测试:
     // 无需导入外部包
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public Connection getConn() {
Connection conn = null ;
try {
   InitialContext ctx = new InitialContext();
  
   DataSource dataSource = (DataSource) ctx.lookup("java:comp/env/jdbc/mysql");
  
    conn = dataSource.getConnection();
   String sql = "select * from books" ;
   PreparedStatement ps = conn.prepareStatement(sql);
   ResultSet rs = ps.executeQuery();
   while(rs.next()){
    System.out.println (rs.getInt(1));
   }
} catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  
} catch (NamingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
}
return conn;
}

---------- 说明:这里的 java:comp/env 是前缀,java 语言规范,后面是你在 web.xml 中
     <res-ref-name>jdbc/mysql</res-ref-name> 的名字,
   这个java 程序 不能直接测试, 因为 context.xml、 web.xml 都是Tomcat 中的,
   服务没开,无法读取 文件信息。
用时:可以直接将 context.xml 放到工程的 META-INF\ 目录下。

转!数据库连接池概念、种类、配置(DBCP\C3P0\JndI与Tomact配置连接池)的更多相关文章

  1. DBCP,C3P0与Tomcat jdbc pool 连接池的比较

    hibernate开发组推荐使用c3p0; spring开发组推荐使用dbcp(dbcp连接池有weblogic连接池同样的问题,就是强行关闭连接或数据库重启后,无法reconnect,告诉连接被重置 ...

  2. 001-http-总览、文件配置、常用http client、http连接池

    一.概述 http请求项目搭建:地址:https://github.com/bjlhx15/common-study.git 中的common-http 主要针对post请求中的,form表单[app ...

  3. 总结spring下配置dbcp,c3p0,proxool数据源链接池

    转载自 http://hiok.blog.sohu.com/66253191.html applicationContext-datasource-jdbc.xml <?xml version= ...

  4. 开源DBCP、C3P0、Proxool 、 BoneCP连接池的比较

    简介 项目主页 使用评价  DBCP DBCP是一个依赖Jakarta commons-pool对象池机制的数据库连接池.DBCP可以直接的在应用程序用使用 http://homepages.nild ...

  5. spring下配置dbcp,c3p0,proxool[转]

    不管通过何种持久化技术,都必须通过数据连接访问数据库,在Spring中,数据连接是通过数据源获得的.在以往的应用中,数据源一般是Web应用服务器提供的.在Spring中,你不但可以通过JNDI获取应用 ...

  6. Hibernate -- 配置c3p0连接池, 事务隔离级别, 管理session

    知识点1:配置c3p0连接池(了解) * 引入c3p0-0.9.1.jar * 在hibernate.cfg.xml文件中增加如下配置 <!-- C3P0连接池设定--> <!-- ...

  7. hibernate+mysql的连接池配置

    1:连接池的必知概念    首先,我们还是老套的讲讲连接池的基本概念,概念理解清楚了,我们也知道后面是怎么回事了. 以前我们程序连接数据库的时候,每一次连接数据库都要一个连接,用完后再释放.如果频繁的 ...

  8. Hibernate_14_数据连接池的使用

    在主配置文件Hibernate.cfg.xml中设置: <!-- 设置默认的事务隔离级别: 隔离级别 相应的整数表示 READ UNCOMMITED 1 READ COMMITED 2 REPE ...

  9. DBCP连接池原理分析及配置用法

    DBCP连接池介绍 ----------------------------- 目前 DBCP 有两个版本分别是 1.3 和 1.4. DBCP 1.3 版本需要运行于 JDK 1.4-1.5 ,支持 ...

随机推荐

  1. Improving the GPA 分类: 贪心 HDU 比赛 2015-08-08 16:12 11人阅读 评论(0) 收藏

    Improving the GPA Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...

  2. Binary Tree 分类: POJ 2015-06-12 20:34 17人阅读 评论(0) 收藏

    Binary Tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6355   Accepted: 2922 Descr ...

  3. easyUI的window包含一个iframe,在iframe中如何关闭window?

    easyUI的window包含一个iframe,在iframe中如何关闭window? parent.$('#win').window('close');

  4. java.util.zip对zip文件解压

    //通过构造方法,来创建一个新的ZIP输入流 ZipInputStream in = new ZipInputStream(new FileInputStream("G:/jquery.ca ...

  5. python学习笔记三 文件操作(基础篇)

    文件操作 打开文件 open(name[,mode[,buffering]])   open函数使用一个文件名作为强制参数,然后返回一个文件对象.[python 3.5 把file()删除掉]   w ...

  6. linux 开启wifi热点

    1,在网络连接管理中创建一个wifi连接,点击 Add,然后选Wi-Fi 2,设置wifi热点名字.wifi接连名字 3,设置 Mode 选 Ad-hoc,其它默认. 4,在 Wi-Fi Securi ...

  7. HDU 5688 Problem D map

    Problem D Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  8. SqlSever基础 len函数 返回一个字符串的长度

    镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...

  9. CentOS 7.0安装配置LAMP服务器(Apache+PHP+MariaDB)

    CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙. 1.关闭firewall: systemctl stop firewalld.service #停止fir ...

  10. V-rep学习笔记:机器人逆运动学数值解法(Cyclic Coordinate Descent Method)

    When performing inverse kinematics (IK) on a complicated bone chain, it can become too complex for a ...