DruidConnectionProvider失效在hibernate3.6.10
来源:互联网 发布:!在c语言中是什么意思 编辑:程序博客网 时间:2024/04/28 12:09
问题:
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
0 0
- DruidConnectionProvider失效在hibernate3.6.10
- hibernate3在javaSE中的应用
- JavaScript在头部失效
- Hibernate3
- hibernate3
- Hibernate3
- Hibernate3
- hibernate3
- Hibernate3
- hibernate3.2在spring中的配置
- hibernate3 二级缓存在项目中的使用
- $.getJSON在IE下失效
- session和cookie在失效
- UIActionSheet在IOS8上失效
- ScrollView在RelativeLayout失效问题
- swiper在IE8下失效
- dos在windows命令失效
- webuploader在ie8上传失效
- Thread和Runnable的区别
- Android百度地图使用
- mysql alter语句的用法
- Android手机通过USB线连接PC共享上网
- Struts1基本运作流程
- DruidConnectionProvider失效在hibernate3.6.10
- 【框架-MFC】MFC 所有Combox失效,无法下拉
- Hibernate的缓存
- 常用正则表达式
- 108. 虚数
- IOS设备之armv6,armv7,armv7s,arm64
- struts2 tags - s:property 显示变量
- 3月底工作安排
- 关于使用bean:write标签显示html内容