对于Proxool,也许大家都熟悉,它是一种稳定、高效的连接池,在web应用中最为常见,特别是在采用了Struts、Spring等框架的web应用,因为它大大提高了反复对数据库操作的效率。
但在纯Java应用项目中,也许大家用的比较少吧,而网上关于它的配置的说明也非常少。现在,我向大家介绍一种在Spring中使用Proxool的方式(注:非web工程),请大家多多指教。
我们都知道,spring提供的对JDBC操作的类初始化时都要传入一DataSource,如NamedParameterJdbcTemplate、SimpleJdbcTemplate等。但是,由于Proxool根本就没有提供一个获取DataSource的方法,所以,我们必须自己开发一数据源,大家都可以取研究研究,我闲着没事,自己写了一个,具体代码如下:
写一个测试类EntityManager,具体代码如下:
然后,在Spring配置文件中,作如下配置:
最后进行测试,得出结果:
1
希望大家看看,指出其中的不足或给出更好的处理方式,毕竟知识共享嘛!呵呵!
但在纯Java应用项目中,也许大家用的比较少吧,而网上关于它的配置的说明也非常少。现在,我向大家介绍一种在Spring中使用Proxool的方式(注:非web工程),请大家多多指教。
我们都知道,spring提供的对JDBC操作的类初始化时都要传入一DataSource,如NamedParameterJdbcTemplate、SimpleJdbcTemplate等。但是,由于Proxool根本就没有提供一个获取DataSource的方法,所以,我们必须自己开发一数据源,大家都可以取研究研究,我闲着没事,自己写了一个,具体代码如下:
package com.jdbc.dataSource;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.logicalcobwebs.proxool.ProxoolException;
import org.logicalcobwebs.proxool.configuration.JAXPConfigurator;
import org.springframework.jdbc.datasource.AbstractDataSource;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
/**
* 供java 应用(非web工程)使用的数据源
* AbstractDataSource是spring提供的DataSource抽象类
* 注:所以的proxool只能被注册一次,注册多次就会出现异常,并且注册时必须
* 要获得注入的proxool配置文件,所以,具体注册过程写在set方法中,并且,
* 该bean必须是spring中所描述的单例模式。
*
* @author t j a
*
*/
public class ProxoolDataSource extends AbstractDataSource {
private String proxoolConfigFileName;
public ProxoolDataSource(){ }
public ProxoolDataSource(String configFileName) {
this.proxoolConfigFileName = configFileName;
}
/**
* 获得数据库连接
*/
public Connection getConnection() throws SQLException {
try {
//1:注册驱动类,这次这个驱动已经不是Oracle的驱动了,是Proxool专用的驱动
Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
} catch (Exception e) {
logger.error(e.getMessage());
throw new SQLException(e.getMessage());
}
Connection conn = DriverManager.getConnection("proxool.connection-afterTransform");
return conn;
}
/**
* 获得数据库连接
*/
public Connection getConnection(String username, String password)
throws SQLException {
return getConnection();
}
public String getProxoolConfigFileName() {
return proxoolConfigFileName;
}
public void setProxoolConfigFileName(String proxoolConfigFileName) {
this.proxoolConfigFileName = proxoolConfigFileName;
String configFilePath = getConfigFileName(this.proxoolConfigFileName);
try {
//注册proxool
JAXPConfigurator.configure(configFilePath, false);
} catch (ProxoolException e) {
logger.error("注册Proxool时出现异常,具体异常信息:"+e.getMessage());
e.printStackTrace();
}
}
/**
* 获得Proxool的配置文件全路径文件名
*
* @param fileName
* @return
*/
private String getConfigFileName(String fileName) {
Assert.notNull(fileName);
StringBuffer configFileDir = new StringBuffer(ClassUtils.getDefaultClassLoader().getResource("").getPath());
configFileDir.append("../src/config");
configFileDir.append("/");
configFileDir.append(fileName);
return StringUtils.cleanPath(configFileDir.toString());
}
}
写一个测试类EntityManager,具体代码如下:
package com.jdbc;
import javax.sql.DataSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
public class EntityManager {
/*
* 数据源
*/
private DataSource dataSource;
public int getCountForAgreement() {
String sql = "select count(*) from tma_fms_agreement a where a.facilityCode =:facilityCode";
SqlParameterSource source = new MapSqlParameterSource("facilityCode","1268273906088MH51G6B");
NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(dataSource);
return template.queryForInt(sql, source);
}
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
}
然后,在Spring配置文件中,作如下配置:
<!-- 数据源配置 -->
<bean id="proxoolDataSource" class="com.jdbc.dataSource.ProxoolDataSource">
<property name="proxoolConfigFileName" value="proxool.xml" />
</bean>
<!-- spring jdbc 使用的bean -->
<bean id="entityManager" class="com.jdbc.EntityManager">
<property name="dataSource" ref="proxoolDataSource"/>
</bean>
最后进行测试,得出结果:
1
希望大家看看,指出其中的不足或给出更好的处理方式,毕竟知识共享嘛!呵呵!