资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

ibatis中怎么加入c3p0连接池

本篇文章给大家分享的是有关ibatis中怎么加入c3p0连接池,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

成都创新互联是一家专业提供丽水企业网站建设,专注与成都网站建设、做网站、HTML5建站、小程序制作等业务。10年已为丽水众多企业、政府机构等服务。创新互联专业网站制作公司优惠进行中。

首先打开iBatis的源码包,找到com.ibatis.sqlmap.engine.datasource包,在里面新建个类,C3p0DataSourceFactory,并实现DataSourceFactory接口,里面的代码是Java代码
private DataSource dataSource;  

public DataSource getDataSource() {  
return dataSource;  
}  

public void initialize(Map map) {  
C3p0Configuration c3p0 = new C3p0Configuration(map);  
dataSource = c3p0.getDataSource();  

private DataSource dataSource;

public DataSource getDataSource() {
return dataSource;
}

public void initialize(Map map) {
C3p0Configuration c3p0 = new C3p0Configuration(map);
dataSource = c3p0.getDataSource();
}


在ibatis下加入c3p0连接池的下一步是到com.ibatis.common.jdbc包里,新建类C3p0Configuration,里面的代码是。。。太长了,原来在dbcp那部分里,还有单独加载配置文件的方法,我觉得如果连这个xml都读不了,那别的一样没法读。
Java代码
private static final Probe PROBE = ProbeFactory.getProbe();  
private DataSource dataSource;  

/** 
* Constructor to supply a map of properties 
*  
* @param properties 
*            - the map of configuration properties 
*/ 
public C3p0Configuration(Map properties) {  
try {  
dataSource = legacyC3p0Configuration(properties);  
} catch (Exception e) {  
throw new RuntimeException(  
"Error initializing C3p0DataSourceFactory.  Cause: " + e, e);  
}  
}  

/** 
* Getter for DataSource 
*  
* @return The DataSource 
*/ 
public DataSource getDataSource() {  
return dataSource;  
}  

private boolean notEmpty(String s) {  
return s != null && s.length() > 0;  
}  

private boolean notSelfString(String s, String compareStr) {  
return !s.equalsIgnoreCase(compareStr);  
}  

private DataSource legacyC3p0Configuration(Map map) {  

ComboPooledDataSource combo = null;  

try {  
if (map.containsKey("JDBC.Driver")) {  

combo = new ComboPooledDataSource();  

String driverClass = (String) map.get("JDBC.Driver");  
String jdbcUrl = (String) map.get("JDBC.ConnectionURL");  
String user = (String) map.get("JDBC.Username");  
String password = (String) map.get("JDBC.Password");  

Class.forName(driverClass);  
combo.setDriverClass(driverClass);  
combo.setJdbcUrl(jdbcUrl);  
combo.setUser(user);  
combo.setPassword(password);  

String acquireIncrement = (String) map  
.get("Pool.acquireIncrement");  
if (notEmpty(acquireIncrement)  
&& notSelfString(acquireIncrement, "acquireIncrement"))  
combo.setAcquireIncrement(Integer  
.parseInt(acquireIncrement));  

...................... 

private static final Probe PROBE = ProbeFactory.getProbe();
private DataSource dataSource;

/**
* Constructor to supply a map of properties
*
* @param properties
*            - the map of configuration properties
*/
public C3p0Configuration(Map properties) {
try {
dataSource = legacyC3p0Configuration(properties);
} catch (Exception e) {
throw new RuntimeException(
"Error initializing C3p0DataSourceFactory.  Cause: " + e, e);
}
}

/**
* Getter for DataSource
*
* @return The DataSource
*/
public DataSource getDataSource() {
return dataSource;
}

private boolean notEmpty(String s) {
return s != null && s.length() > 0;
}

private boolean notSelfString(String s, String compareStr) {
return !s.equalsIgnoreCase(compareStr);
}

private DataSource legacyC3p0Configuration(Map map) {

ComboPooledDataSource combo = null;

try {
if (map.containsKey("JDBC.Driver")) {

combo = new ComboPooledDataSource();

String driverClass = (String) map.get("JDBC.Driver");
String jdbcUrl = (String) map.get("JDBC.ConnectionURL");
String user = (String) map.get("JDBC.Username");
String password = (String) map.get("JDBC.Password");

Class.forName(driverClass);
combo.setDriverClass(driverClass);
combo.setJdbcUrl(jdbcUrl);
combo.setUser(user);
combo.setPassword(password);

String acquireIncrement = (String) map
.get("Pool.acquireIncrement");
if (notEmpty(acquireIncrement)
&& notSelfString(acquireIncrement, "acquireIncrement"))
combo.setAcquireIncrement(Integer
.parseInt(acquireIncrement));

......................


***在com.ibatis.sqlmap.engine.config包里,找到SqlMapConfiguration,在里面找到,加上C3P0就可以实现在ibatis下加入c3p0连接池了
Java代码
// DATA SOURCE ALIASES  
typeHandlerFactory.putTypeAlias("SIMPLE", SimpleDataSourceFactory.class.getName());  
typeHandlerFactory.putTypeAlias("DBCP", DbcpDataSourceFactory.class.getName());  
typeHandlerFactory.putTypeAlias("C3P0", C3p0DataSourceFactory.class.getName());  
typeHandlerFactory.putTypeAlias("JNDI", JndiDataSourceFactory.class.getName());

以上就是ibatis中怎么加入c3p0连接池,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。


本文标题:ibatis中怎么加入c3p0连接池
网页网址:http://cdkjz.cn/article/ghgopc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220