@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
希望你能提交你的修改,而不是我去改!
https://github.com/alibaba/druid/wiki/%E5%A6%82%E4%BD%95%E5%8F%82%E4%B8%8E
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。