JDBC数据库连接池 |
发布时间: 2012/8/23 16:51:43 |
数据库创建链接是比较消耗资源的,访问量非常高的情况会造成内存溢出等等负面影响。 大部分数据链接池解决了这个问题,一般的实现思路是这样得: •链接池在初始化时首先获取一定数量得链接并保存起来,程序请求链接时,直接由连接池返回一个,应用程序使用完后再归还给链接池。 常用的开源链接池:
使用需要导入两个包
dbcp将数据库配置相关的信息单独存放在.properties配置文件中,我们只需要将dbcp示例中的.properties文件考入工程中。 dbcpconfig.propertes : 02 driverClassName=com.mysql.jdbc.Driver 03 url=jdbc:mysql://localhost:3306/day17 04 username=root 05 password=hang 06 07 #<!-- 初始化连接 --> 08 initialSize=10 09 10 #最大连接数量 11 maxActive=50 12 13 #<!-- 最大空闲连接 --> 14 maxIdle=20 15 16 #<!-- 最小空闲连接 --> 17 minIdle=5 18 19 #<!-- 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒 --> 20 maxWait=60000 21 22 23 #JDBC驱动建立连接时附带的连接属性属性的格式必须为这样:[属性名=property;] 24 #注意:"user" 与 "password" 两个属性会被明确地传递,因此这里不需要包含他们。 25 connectionProperties=useUnicode=true;characterEncoding=utf8 26 27 #指定由连接池所创建的连接的自动提交(auto-commit)状态。 28 defaultAutoCommit=true 29 30 #driver default 指定由连接池所创建的连接的只读(read-only)状态。 31 #如果没有设置该值,则“setReadOnly”方法将不被调用。(某些驱动并不支持只读模式,如:Informix) 32 defaultReadOnly= 33 34 #driver default 指定由连接池所创建的连接的事务级别(TransactionIsolation)。 35 #可用值为下列之一:(详情可见javadoc。)NONE,READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE 36 defaultTransactionIsolation=READ_COMMITTED JdbcUitls.java 02 03 //DataSource是DBCP的数据库连接池 04 private static DataSource ds=null; 05 06 //既然是工具类,那就是拿来就能用的,不用new它 07 //这个静态代码块确保其内容只运行一次,这里在第一次调用的时候,获取一个工厂 08 static{ 09 try{ 10 //读取资源文件 11 Properties prop =new Properties(); 12 JdbcUtils.class.getClassLoader().getResourceAsStream("dbcpconfig.properties"); 13 14 //DBCP的连接池工厂 15 BasicDataSourceFactory factory=new BasicDataSourceFactory(); 16 //通过这个工厂,我们获得一个根据资源文件配置的数据库连接池 17 ds = factory.createDataSource(prop); 18 19 }catch (Exception e) { 20 throw new RuntimeException(e); 21 } 22 } 23 24 //返回一个数据库连接 25 public static Connection getConnection() throws SQLException{ 26 //从DataSource中获取一个空闲得连接并返回给调用它的方法 27 return ds.getConnection(); 28 } 29 30 }
这样我们在获取连接的时候可以直接通过这个静态类获得,在使用完成后,保持良好的习惯去关闭这个连接,这个连接实质上不会关闭,会被连接池收回,DBCP使用了动态代理来截断调用者对Connection.close()方法得直接操作。
c3p0与dbcp一样都是读取配置文件
配置文件 c3p0-config.xml : 02 <c3p0-config> 03 04 <default-config> 05 06 <property name="driverClass">com.mysql.jdbc.Driver</property> 07 <property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb</property> 08 <property name="user">root</property> 09 <property name="password">root</property> 10 11 <property name="initialPoolSize">10</property> 12 13 <property name="maxIdleTime">30</property> 14 <property name="maxPoolSize">100</property> 15 <property name="minPoolSize">10</property> 16 <property name="maxStatements">200</property> 17 18 </default-config> 19 20 <-- named-config可以采取多个配置 --> 21 <named-config name="mysql"> 22 <property name="acquireIncrement">50</property> 23 24 <property name="initialPoolSize">100</property> 25 <property name="minPoolSize">50</property> 26 <property name="maxPoolSize">1000</property><!-- intergalactoApp adopts a different approach to configuring statement caching --> 27 <property name="maxStatements">0</property> 28 <property name="maxStatementsPerConnection">5</property> 29 30 </named-config> 31 32 33 34 </c3p0-config>
示例: 02 03 04 private static ComboPooledDataSource ds; 05 06 07 static{ 08 09 try{ 10 //这个是加载配置文件 11 12 ds = new ComboPooledDataSource(); 13 14 15 //这个手动设置,不加载配置文件 16 17 /*ds.setDriverClass("com.mysql.jdbc.Driver"); 18 19 ds.setJdbcUrl("jdbc:mysql://localhost:3306/day16"); 20 21 ds.setUser("root"); 22 23 ds.setPassword("root"); 24 25 ds.setInitialPoolSize(20);*/ 26 27 }catch (Exception e) { 28 29 throw new ExceptionInInitializerError(e); 30 31 } 32 33 } 34 35 36 37 public static Connection getConnection() throws SQLException{ 38 39 return ds.getConnection(); 本文出自:亿恩科技【www.enkj.com】 |