源码看DBCPConnectionProvider与BasicDataSource的渊源

package com.syscatech.util.db.hibernate;import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Properties;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.cfg.Environment;
import org.hibernate.connection.ConnectionProvider;
import org.hibernate.connection.ConnectionProviderFactory;


public class DBCPConnectionProvider implements ConnectionProvider {

private static final Log log = LogFactory.getLog(DBCPConnectionProvider.class);
private static final String PREFIX = "hibernate.dbcp.";
private static BasicDataSource ds;

// Old Environment property for backward-compatibility (property removed in Hibernate3)
private static final String DBCP_PS_MAXACTIVE = "hibernate.dbcp.ps.maxActive";

// Property doesn't exists in Hibernate2
private static final String AUTOCOMMIT = "hibernate.connection.autocommit";

public void configure(Properties props) throws HibernateException {
try {
log.debug("Configure DBCPConnectionProvider");

// DBCP properties used to create the BasicDataSource
Properties dbcpProperties = new Properties();

// DriverClass & url
String jdbcDriverClass = props.getProperty(Environment.DRIVER);
String jdbcUrl = props.getProperty(Environment.URL);
dbcpProperties.put("driverClassName", jdbcDriverClass);
dbcpProperties.put("url", jdbcUrl);

// Username / password
String username = props.getProperty(Environment.USER);
String password = props.getProperty(Environment.PASS);
dbcpProperties.put("username", username);
dbcpProperties.put("password", password);

// Isolation level
String isolationLevel = props.getProperty(Environment.ISOLATION);
if ((isolationLevel != null) && (isolationLevel.trim().length() > 0)) {
dbcpProperties.put("defaultTransactionIsolation", isolationLevel);
}

// Turn off autocommit (unless autocommit property is set)
String autocommit = props.getProperty(AUTOCOMMIT);
if ((autocommit != null) && (autocommit.trim().length() > 0)) {
dbcpProperties.put("defaultAutoCommit", autocommit);
} else {
dbcpProperties.put("defaultAutoCommit", String.valueOf(Boolean.FALSE));
}

// Pool size
String poolSize = props.getProperty(Environment.POOL_SIZE);
if ((poolSize != null) && (poolSize.trim().length() > 0)
&& (Integer.parseInt(poolSize) > 0)) {
dbcpProperties.put("maxActive", poolSize);
}

// Copy all "driver" properties into "connectionProperties"
Properties driverProps = ConnectionProviderFactory.getConnectionProperties(props);
if (driverProps.size() > 0) {
StringBuffer connectionProperties = new StringBuffer();
for (Iterator iter = driverProps.keySet().iterator(); iter.hasNext();) {
String key = (String) iter.next();
String value = driverProps.getProperty(key);
connectionProperties.append(key).append('=').append(value);
if (iter.hasNext()) {
connectionProperties.append(';');
}
}
dbcpProperties.put("connectionProperties", connectionProperties.toString());
}

// Copy all DBCP properties removing the prefix
for (Iterator iter = props.keySet().iterator() ; iter.hasNext() ;) {
String key = String.valueOf(iter.next());
if (key.startsWith(PREFIX)) {
String property = key.substring(PREFIX.length());
String value = props.getProperty(key);
dbcpProperties.put(property, value);
}
}

// Backward-compatibility
if (props.getProperty(DBCP_PS_MAXACTIVE) != null) {
dbcpProperties.put("poolPreparedStatements", String.valueOf(Boolean.TRUE));
dbcpProperties.put("maxOpenPreparedStatements", props.getProperty(DBCP_PS_MAXACTIVE));
}

// Some debug info
if (log.isDebugEnabled()) {
log.debug("Creating a DBCP BasicDataSource with the following DBCP factory properties:");
StringWriter sw = new StringWriter();
dbcpProperties.list(new PrintWriter(sw, true));
log.debug(sw.toString());
}

// Let the factory create the pool
[color=red][b] ds = (BasicDataSource) BasicDataSourceFactory.createDataSource(dbcpProperties); [/b][/color]
// The BasicDataSource has lazy initialization
// borrowing a connection will start the DataSource
// and make sure it is configured correctly.
Connection conn = ds.getConnection();
conn.close();

// Log pool statistics before continuing.
logStatistics();
}
catch (Exception e) {
String message = "Could not create a DBCP pool";
log.fatal(message, e);
if (ds != null) {
try {
ds.close();
}
catch (Exception e2) {
// ignore
}
ds = null;
}
throw new HibernateException(message, e);
}
log.debug("Configure DBCPConnectionProvider complete");
}

public static BasicDataSource getDataSource(){

return ds;
}

public Connection getConnection() throws SQLException {
Connection conn = null;
try {
conn = ds.getConnection();
}
finally {
logStatistics();
}
return conn;
}

public void closeConnection(Connection conn) throws SQLException {
try {
conn.close();
}
finally {
logStatistics();
}
}

public void close() throws HibernateException {
log.debug("Close DBCPConnectionProvider");
logStatistics();
try {
if (ds != null) {
ds.close();
ds = null;
}
else {
log.warn("Cannot close DBCP pool (not initialized)");
}
}
catch (Exception e) {
throw new HibernateException("Could not close DBCP pool", e);
}
log.debug("Close DBCPConnectionProvider complete");
}

protected void logStatistics() {
if (log.isInfoEnabled()) {
log.info("active: " + ds.getNumActive() + " (max: " + ds.getMaxActive() + ") "
+ "idle: " + ds.getNumIdle() + "(max: " + ds.getMaxIdle() + ")");
}
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值