iBatis 配置自定义C3P0连接池

[转][url]http://www.blogjava.net/usherlight/archive/2010/02/01/311493.html[/url]

IBatis2中提供了3种DataSource的配置:JNDI, Apache DBCP, IBatis自带的SimpleDataSource。但在IBatis3中只提供了两种DataSource: UNPOOLED, POOLED。
如果要实现自定义的DataSource,就需要通过扩展DataSourceFactory。本文就演示一下这个过程。
准备工作:Connection Pool的选择,通过搜索发现目前比较流行的免费数据库连接池主要有3种:Apache DBCP, C3P0, Proxool。
看了一下,Proxool的最新版本是0.9.1(2008-08-23), C3P0的最新版本是0.9.1.2(2007-05-21), DBCP最新版本是1.2.2(2007-04-04)
好像这3个项目都已经挺长时间没有更新了。但是总体评价上C3P0无论从稳定上还是效率上都要好一点。
(具体这3个项目谁更优秀,并不是本文的重点,本文主要是介绍一下如何在IBatis3中自定义数据源)
大致步骤:
1、实现org.apache.ibatis.datasource.DataSourceFactory接口,主要是2个方法
a、public DataSource getDataSource() 如何具体地得到一个数据源
b、public void setProperties(Properties properties) 如何设置数据源的参数属性
2、实现javax.sql.DataSource,这个就是提供给DataSourceFactory的实例
3、在IBatis3中引用新加入的数据源

1. 从代码中可以看出,IBatis3与IBatis2不同,不再通过一个Configuration类来进行数据源属性的设置,而是使用反射机制直接调用数据源的方法来设置参数。
这就要求配置文件中的参数名称必须与数据源类中的方法名匹配.

public class C3p0DataSourceFactory implements DataSourceFactory {

private DataSource dataSource;

public C3p0DataSourceFactory() {
dataSource = new C3p0DataSource();
}

public DataSource getDataSource() {
return dataSource;
}

public void setProperties(Properties properties) {
Properties driverProperties = new Properties();
MetaObject metaDataSource = MetaObject.forObject(dataSource);
for (Object key : properties.keySet()) {
String propertyName = (String) key;
if (propertyName.startsWith(DRIVER_PROPERTY_PREFIX)) {
String value = properties.getProperty(propertyName);
driverProperties.setProperty(propertyName
.substring(DRIVER_PROPERTY_PREFIX_LENGTH), value);
} else if (metaDataSource.hasSetter(propertyName)) {
String value = (String) properties.get(propertyName);
Object convertedValue = convertValue(metaDataSource,
propertyName, value);
metaDataSource.setValue(propertyName, convertedValue);
} else {
throw new DataSourceException("Unkown DataSource property: "
+ propertyName);
}
}
if (driverProperties.size() > 0) {
metaDataSource.setValue("driverProperties", driverProperties);
}
}

@SuppressWarnings("unchecked")
private Object convertValue(MetaObject metaDataSource, String propertyName,
String value) {
Object convertedValue = value;
Class targetType = metaDataSource.getSetterType(propertyName);
if (targetType == Integer.class || targetType == int.class) {
convertedValue = Integer.valueOf(value);
} else if (targetType == Long.class || targetType == long.class) {
convertedValue = Long.valueOf(value);
} else if (targetType == Boolean.class || targetType == boolean.class) {
convertedValue = Boolean.valueOf(value);
}
return convertedValue;
}

private static final String DRIVER_PROPERTY_PREFIX = "driver.";
private static final int DRIVER_PROPERTY_PREFIX_LENGTH = DRIVER_PROPERTY_PREFIX.length();

}


2. 数据源类,其中的一堆setter就是用于设置属性的

public class C3p0DataSource implements DataSource {

private ComboPooledDataSource dataSource;
public C3p0DataSource() {
this.dataSource = new ComboPooledDataSource();
}

public Connection getConnection() throws SQLException {
return dataSource.getConnection();
}

public Connection getConnection(String username, String password)
throws SQLException {
return dataSource.getConnection(username, password);
}

public PrintWriter getLogWriter() throws SQLException {
return dataSource.getLogWriter();
}

public int getLoginTimeout() throws SQLException {
return dataSource.getLoginTimeout();
}

public void setLogWriter(PrintWriter out) throws SQLException {
dataSource.setLogWriter(out);
}

public void setLoginTimeout(int seconds) throws SQLException {
dataSource.setLoginTimeout(seconds);
}


public synchronized void setDriver(String driver) {
try {
dataSource.setDriverClass(driver);
} catch (Exception e) {
}
}

public void setUrl(String url) {
dataSource.setJdbcUrl(url);
}

public void setUsername(String username) {
dataSource.setUser(username);
}

public void setPassword(String password) {
dataSource.setPassword(password);
}

public void setInitialPoolSize(int initialPoolSize) {
dataSource.setInitialPoolSize(initialPoolSize);
}

public void setMaxPoolSize(int maxPoolSize) {
dataSource.setMaxPoolSize(maxPoolSize);
}

public void setMinPoolSize(int minPoolSize) {
dataSource.setMinPoolSize(minPoolSize);
}

public void setPreferredTestQuery(String preferredTestQuery) {
dataSource.setPreferredTestQuery(preferredTestQuery);
}

public void setPoolPingQuery(String poolPingQuery) {
dataSource.setPreferredTestQuery(poolPingQuery);
}
}


3. 在配置文件Configuration.xml中,可以先定义数据源的别称,然后就象POOLED和UNPOOLED一样使用别称来引用数据源。

<Configuration>
...
<typeAlias>
<typeAlias type="com.test.datasource.C3p0DataSourceFactory" alias="C3P0"/>
</typeAlias>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="C3P0">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="poolPingQuery" value="${pingquery}"/>
</dataSource>
</environment>
</environments>
...
<Configuration>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值