DruidConnectionProvider失效在hibernate3.6.10

路小磊 发布于 2013/08/14 18:32
阅读 1K+
收藏 1

@wenshao 你好,想跟你请教个问题:

org.hibernate.service.jdbc.connections.spi.ConnectionProvider

已经迁移至

org.hibernate.connection.ConnectionProvider;

org.hibernate.service.spi.Configurable和org.hibernate.service.spi.Stoppable找不到了。所以会报错。

还有我看c3p0的ConnectionProvider中有事务隔离级别这个设定,DruidConnectionProvider中未提供。

以下是我重写后的

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;


import org.hibernate.HibernateException;
import org.hibernate.cfg.Environment;
import org.hibernate.connection.ConnectionProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

/**
 * Druid连接池的Hibernate辅助类
 * 
 * @author xiaoleilu
 * @see ConnectionProvider
 */
public class DruidConnectionProvider implements ConnectionProvider {
	private static final Logger log = LoggerFactory.getLogger(DruidConnectionProvider.class);

	private DruidDataSource ds; // 数据源
	private Integer isolation; // 事务隔离级别
	
	public DruidConnectionProvider() {
		ds = new DruidDataSource();
	}

	/**
	 * 获得数据库连接对象,在获得时设定其隔离级别(如果存在)<br>
	 * {@inheritDoc}
	 */
	public Connection getConnection() throws SQLException {
		final Connection conn = ds.getConnection();
		if (isolation != null) {
			conn.setTransactionIsolation(isolation.intValue());
		}
		return conn;
	}

	/**
	 * 关闭给定的数据库连接<br>
	 * 数据库连接池给定的策略应该是将此连接放回池中<br>
	 * {@inheritDoc}
	 */
	public void closeConnection(Connection conn) throws SQLException {
		conn.close();
	}

	/**
	 * {@inheritDoc}
	 */
	public void configure(Properties props) throws HibernateException {
		//隔离级别的设定
		String isolationStr = props.getProperty(Environment.ISOLATION);
		if(isolationStr != null) {
			this.isolation = Integer.parseInt(isolationStr);
			log.info("JDBC isolation level: " + Environment.isolationLevelToString(isolation));
		}
		
		Properties connProps = new Properties();
		Set<Entry<Object,Object>> entrySet = props.entrySet();
		for (Entry<Object, Object> entry : entrySet) {
			String key = entry.getKey().toString();
			if(key.startsWith(Environment.CONNECTION_PREFIX)) {
				connProps.put(key.substring(Environment.CONNECTION_PREFIX.length() + 1), entry.getValue());
			}
		}
		
		try {
			DruidDataSourceFactory.config(this.ds, connProps);
		} catch (SQLException e) {
			log.error("Exception when config the Druid from Hibernate!", e);
		}
	}

	/**
	 * 关闭数据库连接池 {@inheritDoc}
	 */
	public void close() {
		ds.close();
	}

	/**
	 * {@inheritDoc}
	 */
	public boolean supportsAggressiveRelease() {
		return false;
	}
}

Hibernate.cfg.xml的配置如下:

<!-- Database connection settings -->
		<property name="connection.provider_class">com.XX.XX.XX.XX.DruidConnectionProvider</property>

		<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="connection.url">jdbc:mysql://localhost/exam?useUnicode=true&amp;characterEncoding=utf8&amp;autoReconnectForPools=true&amp;failOverReadOnly=false&amp;useCompression=true</property>
		<property name="connection.username">test</property>
		<property name="connection.password">test</property>
		
		<!-- Druid连接池参数配置 -->
		<property name="connection.initialSize">1</property>
		<property name="connection.minIdle">1</property>
		<property name="connection.maxActive">20</property>
		<property name="connection.maxActive">20</property>
		<property name="connection.maxWait">60000</property>
		<property name="connection.timeBetweenEvictionRunsMillis">60000</property>
		<property name="connection.minEvictableIdleTimeMillis">60000</property>
		<property name="connection.validationQuery">SELECT 1</property>
		<property name="connection.testWhileIdle">true</property>
		<property name="connection.testOnBorrow">false</property>
		<property name="connection.testOnReturn">false</property>
		<property name="connection.poolPreparedStatements">false</property>
		<property name="connection.defaultAutoCommit">true</property>
		<property name="connection.filters">stat</property>
希望温少可以考虑支持新版的Hibernate


加载中
返回顶部
顶部