开发者社区> 问答> 正文

DruidConnectionProvider失效在hibernate3.6.10?报错

@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&characterEncoding=utf8&autoReconnectForPools=true&failOverReadOnly=false&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


展开
收起
爱吃鱼的程序员 2020-06-22 17:33:05 521 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    希望你能提交你的修改,而不是我去改!

    https://github.com/alibaba/druid/wiki/%E5%A6%82%E4%BD%95%E5%8F%82%E4%B8%8E

    2020-06-22 17:33:20
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载