




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、.3301000falsetestfalse100nullfalse60精品.360151003rootpasswordselect id from test where id=1300精品.falsetruerootfalse精品.acquireincrement - 5 acquireretryattempts - 30 acquireretrydelay - 1000 autocommitonclose - false automatictesttable - null breakafteracquirefailure - true checkouttimeout - 0 connect
2、ioncustomizerclassname - null connectiontesterclassname - com.mchange.v2.c3p0.impl.defaultconnectiontester datasourcename - 1bqp8hc8ezqxpjc1hp9gny|704baa debugunreturnedconnectionstacktraces - false description - null driverclass - com.mysql.jdbc.driver factoryclasslocation - null forceignoreunresol
3、vedtransactions - false identitytoken - 1bqp8hc8ezqxpjc1hp9gny|704baa idleconnectiontestperiod - 60 initialpoolsize - 5 jdbcurl - jdbc:mysql:/localhost:3306/db_temp?useunicode=true&characterencoding=utf-8 lastacquisitionfailuredefaultuser - null maxadministrativetasktime - 0 maxconnectionage - 0 max
4、idletime - 60 maxidletimeexcessconnections - 0 maxpoolsize - 30 maxstatements - 0 maxstatementsperconnection - 0 minpoolsize - 5 numhelperthreads - 3 numthreadsawaitingcheckoutdefaultuser - 0 preferredtestquery - null properties - user=*password=* propertycycle - 0 testconnectiononcheckin - false te
5、stconnectiononcheckout - false unreturnedconnectiontimeout - 0 usestraditionalreflectiveproxies - false精品.package com.paipaiestore.database.c3p0;import java.util.hashmap;import java.util.iterator;import java.util.map;import java.util.set;import javax.sql.datasource;import com.mchange.v2.c3p0.datasou
6、rces;/* 数据连接池工厂* author samland* version 1.0, 2006-11*/public class dbpoolfactory private static dbpoolfactory dbpoolfactory = new dbpoolfactory();/* 连接池列表*/private static map poollist = new hashmap();private dbpoolfactory()public static dbpoolfactory getinstance()return dbpoolfactory;/* 向连接池列表添加一个新
7、的池化(pooled)数据源* param key* param configfile 配置文件名。为了简化参数输入与便于外部维护,可以把配置好的文件传入这里分析*/public void add(string key, string configfile) /read config from file, and build a dbpool/* 向连接池列表添加一个新的池化(pooled)数据源* param key* param driver 数据连接驱动,如com.mysql.jdbc.driver* param url 连接串, 如jdbc:mysql:/localhost/samla
8、nd* param username 数据库连接用户名,如samland* param password 数据库连接用户密码* throws exception*/public void add(string key, string driver, string url, string username, string password)throws exceptionif (key=null) throw new exception(dbpool key cannot be null) ;class.forname(driver);datasource ds_unpooled = datas
9、ources.unpooleddatasource(url, username, password);datasource ds_pooled = datasources.pooleddatasource( ds_unpooled );if (poollist.containskey(key)poollist.remove(key);poollist.put(key, ds_pooled);/* 根据key字符串获取连接池* param key* return dbpool 连接池* throws exception*/public dbpool getdbpool(string key) t
10、hrows exceptionif (key=null) throw new exception(dbpool key cannot be null) ;datasource ds = (datasource)poollist.get(key);dbpool dbpool = new dbpool();dbpool.setds_pooled(ds);return dbpool;/四、连接池的使用通过dbpool来获取数据库连接 (jdbc connection)。4.1 由dbpoolfactory工厂产生一个dbpool4.2 同时,dbpoolfactory工厂根据传入的参数key向这个d
11、bpool注入数据源4.3 从dbpool产生线程内的connection/package com.paipaiestore.database.c3p0;import java.sql.connection;import java.sql.sqlexception;import javax.sql.datasource;public class dbpool /* 数据库连接。同一个线程使用同一个连接。*/protected static threadlocal connection = new threadlocal();/* 数据源,需由dbpoolfactory工厂注入*/private
12、 datasource ds_pooled;/* 不能独立构造,必须从dbpoolfactory工厂产生*/protected dbpool()/* get 数据源* return*/public datasource getds_pooled() return ds_pooled;/* 注入数据源* param ds_pooled*/public void setds_pooled(datasource ds_pooled) this.ds_pooled = ds_pooled;/* 获得数据库连接* 同一个线程只返回相同的一个连接。* return connection* throws s
13、qlexception*/public connection getconnection() throws sqlexceptionconnection con = (connection)connection.get();if (con=null) con = ds_pooled.getconnection();connection.set(con);return con;/五、改进线程内数据库连接的使用为了适应程序已经使用开的jdbc事务管理,因此需要改进dbpool成为transaction类,而原来的dbpool则缩减为如下代码:/public class dbpool private
14、 datasource ds_pooled;protected dbpool()public datasource getds_pooled() return ds_pooled;public void setds_pooled(datasource ds_pooled) this.ds_pooled = ds_pooled;/package com.paipaiestore.database.c3p0;import java.sql.connection;import java.sql.resultset;import java.sql.sqlexception;import java.sq
15、l.statement;import javax.sql.datasource;public class transaction /* 数据库连接。同一个线程使用同一个连接。*/protected static threadlocal connection = new threadlocal();protected static boolean isinused = false;/* 开始一个事务* 如果当前线程的数据库连接不存在,则会新获得一个数据库连接* param key* throws exception*/public static void begintransaction(str
16、ing key) throws exceptionconnection conn = (connection)connection.get();if (conn=null) conn = createconnection(key);setconnection(conn);conn.settransactionisolation(connection.transaction_read_committed);conn.setautocommit(false);isinused = true;/* 提交一个事务* throws sqlexception*/public static void com
17、mittransaction() throws sqlexception connection conn = (connection)connection.get();mit();/* 回滚一个事务* throws sqlexception*/public static void rollbacktransaction() throws sqlexception connection conn = (connection)connection.get();conn.rollback();public static boolean getisinused() return isi
18、nused;private static void closeconnection() try connection conn = (connection)connection.get();if (conn!=null) connection.set(null);conn.close();catch(sqlexception sqlexception) private static void close(resultset rs)trystatement st = rs.getstatement();connection conn = st.getconnection();rs.close()
19、;st.close();conn.close();catch(exception exception) /* 获得数据库连接* 同一个线程只返回相同的一个连接。* return* throws sqlexception*/public static connection getconnection() throws sqlexception connection conn = null;conn = (connection)connection.get();if(conn = null)throw new nullpointerexception();elsereturn conn;priva
20、te static void setconnection(connection conn)connection.set(conn);/* 获得一个新的数据库连接* param key* return connection* throws exception*/private static connection createconnection(string key)throws exception dbpoolfactory df = dbpoolfactory.getinstance();datasource ds_pooled = df.getdbpool(key).getds_pooled() ;connection conn = ds_pooled.getconnection();df = null;return conn;/六、测试/package samland;import java.sql.connection;import java.sql.resultset;import java.sql.statement;import com.paipaiestore.database.c3p0.dbpoolfactory;import com.
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025-2030年中国铝颜料行业运行现状及发展趋势预测报告
- 2025-2030年中国铝冶炼市场竞争格局及前景趋势预测报告
- 2025-2030年中国钨行业未来投资预测发展战略规划分析报告
- 2025四川省建筑安全员《C证》考试题库
- 2025-2030年中国过氧化锌市场运营动态及发展前景分析报告
- 2025-2030年中国车蜡行业市场运行态势及发展盈利分析报告
- 2025-2030年中国贸易融资前景趋势及发展潜力分析报告
- 2025-2030年中国调味香料市场发展前景及投资战略研究报告
- 2025-2030年中国船舶制造行业发展状况及营销战略研究报告
- 2025-2030年中国纸基覆铜板市场十三五规划及投资风险评估报告
- 高二数学(含创意快闪特效)-【开学第一课】2023年高中秋季开学指南之爱上数学课
- 《学前儿童社会教育》学前儿童社会教育概述-pp课件
- 全国医学英语统考医学英语词汇表
- 【品牌建设研究国内外文献综述5000字】
- 国家电网公司电力安全工作规程(电力通信部分)(试行)
- 第八版-精神分裂症及其他精神病性障碍(中文)
- 小学一年级新生报名登记表
- 生态毒理学第三章毒物的分子效应与毒理学机制
- 智能财务共享在京东的应用研究
- 卫生和微生物基础知识培训-
- 2023年镇江市高等专科学校单招综合素质题库及答案解析
评论
0/150
提交评论