📄 datasourceconnectionprovider.java
字号:
package com.holpe.database;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.beanutils.BeanUtils;
import org.hibernate.HibernateException;
import org.hibernate.connection.ConnectionProvider;
import com.holpe.common.Globals;
import com.holpe.common.StringUtils;
/**
* 让Hibernate支持各种实现了DataSource接口的数据源
* @author liuxb
*/
public class DataSourceConnectionProvider implements ConnectionProvider {
private final static String BASE_KEY = "dscp.";
private final static String ENCODING_KEY = "dscp.encoding";
private final static String DATASOURCE_KEY = "dscp.datasource";
protected DataSource dataSource;
protected boolean encoding = false;
protected boolean useProxy = false;
/* (non-Javadoc)
* @see org.hibernate.connection.ConnectionProvider#configure(java.util.Properties)
*/
public void configure(Properties props) throws HibernateException {
initDataSource(props);
}
/* (non-Javadoc)
* @see org.hibernate.connection.ConnectionProvider#getConnection()
*/
public Connection getConnection() throws SQLException {
final Connection conn = dataSource.getConnection();
if(useProxy && conn!=null){
return (new _Connection(conn,encoding)).getConnection();
}
return conn;
}
/* (non-Javadoc)
* @see org.hibernate.connection.ConnectionProvider#closeConnection(java.sql.Connection)
*/
public void closeConnection(Connection conn) throws SQLException {
if(conn!=null && !conn.isClosed())
conn.close();
}
/* (non-Javadoc)
* @see org.hibernate.connection.ConnectionProvider#close()
*/
public void close() throws HibernateException {
if(dataSource != null)
try {
Method mClose = dataSource.getClass().getMethod("close", null);
mClose.invoke(dataSource, null);
} catch (Exception e) {
throw new HibernateException(e);
}
dataSource = null;
}
/* (non-Javadoc)
* @see org.hibernate.connection.ConnectionProvider#supportsAggressiveRelease()
*/
public boolean supportsAggressiveRelease() {
return false;
}
/**
* Initialize the datasource
* @param props
* @throws HibernateException
*/
protected synchronized void initDataSource(Properties props) throws HibernateException {
String dataSourceClass = null;
Properties new_props = new Properties();
Iterator keys = props.keySet().iterator();
while(keys.hasNext()){
String key = (String)keys.next();
//System.out.println(key+"="+props.getProperty(key));
if(ENCODING_KEY.equalsIgnoreCase(key)){
encoding = "true".equalsIgnoreCase(props.getProperty(key));
useProxy = true;
}
else if(DATASOURCE_KEY.equalsIgnoreCase(key)){
dataSourceClass = props.getProperty(key);
}
else if(key.startsWith(BASE_KEY)){
String value = props.getProperty(key);
value = StringUtils.replace(value, "{HOLPE}", Globals.WEBAPP_PATH);
new_props.setProperty(key.substring(BASE_KEY.length()), value);
}
}
if(dataSourceClass == null)
throw new HibernateException("Property 'dscp.datasource' no defined.");
try {
dataSource = (DataSource)Class.forName(dataSourceClass).newInstance();
BeanUtils.populate(dataSource, new_props);
} catch (Exception e) {
throw new HibernateException(e);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -