Hibernate整合Druid数据库连接池遇到的问题整合

1.cannot be cast to org.hibernate.engine.jdbc.connections.spi.ConnectionProvider

这是因为你的hibernate核心包版本太低,阿里巴巴数据库连接池兼容的hibernate至少4.2以上,我使用的是hibernate-core-4.3.11.Final.jar ,如果你的版本以前是4.0以下的建议不要使用5.0以上的包,因为5.0及以上,hibernate做了大量更改 ,比如sessionfactory初始化不再像以前一样,增加了许多的依赖包,例如:classmate-1.3.3.jar,hibernate-jpa-2.1-api-1.0.0.Draft-16.jar ,hibernate-commons-annotations-5.0.1.Final.jar(这个包很重要,这个包的版本应该跟随hibernate的核心包版本变动,不然会报反射生成错误)

2.当你处理好hibernate的包之后,开始调整druid的包,我使用的是druid-1.0.26.jar,这个包也很容易出现与hibernate兼容性的问题,5.0以上,需要自己去设配更高版本的

3.所有依赖包调整完以后,开始配置hibernate.cfg.xml,这里需要注意数据源的配置,跟c3p0以及DBCP配置不一样

 <property name="hibernate.connection.provider_class">  
            com.alibaba.druid.support.hibernate.DruidConnectionProvider  
        </property>  
    <property name="driverClassName">com.mysql.jdbc.Driver</property>  
        <property name="url">  
        jdbc:mysql://127.0.0.1:3306/hibernate?useSSL=true
        </property>  
        <property name="username">admin</property>  
        <property name="password">123456</property>  
     <!-- 配置初始化大小、最小、最大 -->  
        <property name="initialSize">5</property>  
        <property name="minIdle">5</property>  
        <property name="maxActive">100</property>  
       
        <!-- 配置获取连接等待超时的时间 -->  
        <property name="maxWait">60000</property>  
       
        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->  
        <property name="timeBetweenEvictionRunsMillis">60000</property>  
       
        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->  
        <property name="minEvictableIdleTimeMillis">300000</property>  
       
         
        <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->  
        <property name="poolPreparedStatements">true</property>  
        <property name="maxPoolPreparedStatementPerConnectionSize">20</property>  
       
        <!-- 配置监控统计拦截的filters,去掉后监控界面sql无法统计 -->  
        <property name="filters">stat</property>  
        <!-- JDBC connection pool (use the built-in) -->  
      <!--   <property name="connection.pool_size">5</property> -->  
          
        <!-- SQL dialect -->  
        <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>  
  
        <!-- Enable Hibernate's automatic session context management -->  
        <property name="current_session_context_class">thread</property>  
  
        <!-- Disable the second-level cache  -->  
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>  
  
        <!-- Echo all executed SQL to stdout -->  
        <property name="show_sql">false</property>  
  
        <!-- Drop and re-create the database schema on startup -->  
        <property name="hbm2ddl.auto">update</property>  
        <mapping resource="com/synpower/entity/ModeDetailModbus.hbm.xml" />

4.创建sessionfactory根据hibernate 版本不同 创建方法也不同

首先说我的4.3版本的创建方法

   try{
            Configuration conf = new Configuration().configure();
            StandardServiceRegistry standardServiceRegistry = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
            factory = conf .buildSessionFactory(standardServiceRegistry);
        }catch(Exception e){
            System.out.println("init factory error"+e.getMessage());
        }

其他版本可以自行百度 ,参考地址:http://yanln.iteye.com/blog/2249939

posted @ 2018-02-25 11:10  我为自己找借口  阅读(3998)  评论(0编辑  收藏  举报