Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7170754
  • 博文数量: 3857
  • 博客积分: 6409
  • 博客等级: 准将
  • 技术积分: 15948
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-02 16:48
个人简介

迷彩 潜伏 隐蔽 伪装

文章分类

全部博文(3857)

文章存档

2017年(5)

2016年(63)

2015年(927)

2014年(677)

2013年(807)

2012年(1241)

2011年(67)

2010年(7)

2009年(36)

2008年(28)

分类: Java

2013-08-23 10:09:48

原文地址:常见的数据库连接池 作者:scoield


所有的数据库连接池实现都要指定sun定义的连接池接口-javax.sql.DataSource

1.DBCP

DBCP连接池是tomcat默认支持的连接池,在tomcat的lib目录下可看见tomcat-dbcp.jar;
如果没有的话,须执行引入commons-dbcp-1.2.2.jarcommons-pool.jar 早期版本可能还需引入commons-collection.jar
简单入门实例:

点击(此处)折叠或打开

  1. public class DBCP {
  2.     public static void getConnection() throws Exception {
  3.         // 1. 获取连接池对象

  4.         BasicDataSource source = new BasicDataSource();
  5.         // 2. 设置连接池对象

  6.         source.setDriverClassName("com.mysql.jdbc.Driver");
  7.         source.setUrl("jdbc:mysql:///dbutils");
  8.         source.setUsername("root");
  9.         source.setPassword("root");
  10.         source.setDefaultAutoCommit(true);
  11.         
  12.         source.setDefaultTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
  13.         source.setInitialSize(10);
  14.         source.setMaxActive(5);
  15.         source.setMaxIdle(10);
  16.         source.setMinIdle(0);
  17.         source.setMaxWait(5000);
  18.         
  19.         Connection conn = source.getConnection();
  20.         String sql = "insert into test(name) values(?)";
  21.         PreparedStatement state = conn.prepareStatement(sql);
  22.         state.setString(1, "scoield");
  23.         state.executeUpdate();
  24.         state.close();
  25.         System.out.println("插入数据成功");
  26.         
  27.     }
  28.     public static void main(String[] args) throws Exception {
  29.         getConnection();

  30.     }
  31. }


上面的代码发现设置参数的方法过多,得一个个的设置,过于繁琐!

配置文件方式代码实现:
dbcp.properties

点击(此处)折叠或打开

  1. username=root
  2. password=root
  3. url=jdbc\:mysql\:///dbutils
  4. driverClassName=com.mysql.jdbc.Driver
  5. defaultAutoCommit=true
  6. defaultTransactionIsolation=REPEATABLE_READ
  7. initialSize=10
  8. maxActive=5
  9. maxIdle=5
  10. minIdle=0
  11. maxWait=5000

java代码:

点击(此处)折叠或打开

  1. public class DBCP {
  2.     public static void getConnection2() throws Exception{
  3.         Properties prop = new Properties();
  4.         InputStream url = DBCP.class.getClassLoader().getResourceAsStream("dbcp.properties");
  5.         prop.load(url);
  6.         BasicDataSource source = (BasicDataSource) BasicDataSourceFactory.createDataSource(prop);
  7.      Connection conn = source.getConnection();
  8.      String sql = "insert into test(name) values(?)";
  9.         PreparedStatement state = conn.prepareStatement(sql);
  10.         state.setString(1, "scoield1");
  11.         state.executeUpdate();
  12.         state.close();
  13.         System.out.println("插入数据成功");
  14.     }
  15.     public static void main(String[] args) throws Exception {
  16.         getConnection2();

  17.     }
  18. }

2.C3P0
在Hibernate和Spring中默认支持该数据库连接池
需要引入:c3p0-0.9.1.2.jar包,如果报错再引入mchange-commons-0.2.jar

1. 在类路径下编写一个c3p0-config.xml文件


点击(此处)折叠或打开

  1. <c3p0-config>
  2.   <!--
  3.   <default-config>
  4.     <property name="driverClass">com.mysql.jdbc.Driver</property>
  5.     <property name="jdbcUrl">jdbc:mysql:///dbutils</property>
  6.     <property name="user">root</property>
  7.     <property name="password">root</property>
  8.     
  9.     <property name="checkoutTimeout">30000</property>
  10.     <property name="idleConnectionTestPeriod">30</property>
  11.     <property name="initialPoolSize">10</property>
  12.     <property name="maxIdleTime">30</property>
  13.     <property name="maxPoolSize">100</property>
  14.     <property name="minPoolSize">10</property>
  15.     <property name="maxStatements">200</property>
  16.   </default-config>
  17.    -->
  18.    <named-config name="mysql">
  19.         <property name="driverClass">com.mysql.jdbc.Driver</property>
  20.         <property name="jdbcUrl">jdbc:mysql:///dbutils</property>
  21.         <property name="user">root</property>
  22.         <property name="password">root</property>
  23.         
  24.         <property name="acquireIncrement">5</property>
  25.         <property name="initialPoolSize">6</property>
  26.         <property name="minPoolSize">5</property>
  27.         <property name="maxPoolSize">10</property>
  28.     </named-config>
  29. </c3p0-config>

2.获取默认的配置:

点击(此处)折叠或打开

  1. public static void getConnection1() throws Exception {
  2.         ComboPooledDataSource source = new ComboPooledDataSource();
  3.         Connection conn = source.getConnection();
  4.         String sql = "insert into users (name,address) values (?,?)";
  5.         PreparedStatement state = conn.prepareStatement(sql);
  6.         state.setString(1, "c3p0");
  7.         state.setString(2, "c3p0");
  8.         state.executeUpdate();
  9.         source.close();
  10.         System.out.println("OK");
  11.     }

3.获取指定名的配置:

点击(此处)折叠或打开

  1. public static void getConnection2() throws Exception {
  2.         ComboPooledDataSource source = new ComboPooledDataSource("mysql");
  3.         Connection conn = source.getConnection();
  4.         String sql = "insert into users (name,address) values (?,?)";
  5.         PreparedStatement state = conn.prepareStatement(sql);
  6.         state.setString(1, "c3p02");
  7.         state.setString(2, "c3p02");
  8.         state.executeUpdate();
  9.         source.close();
  10.         System.out.println("OK");
  11.     }

总结:连接池技术可以快速的获取数据库连接的重量级资源但是操作数据库依旧比较繁琐……
  解决方案---DBUtils!



阅读(675) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~