今天尝试了一下阿里的druid和hibernate结合,没想到一直报错。
配置代码如下
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <property name="hibernate.connection.provider_class">com.alibaba.druid.support.hibernate.DruidConnectionProvider</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hib_demo</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123456</property> <property name="hibernate.show_sql">true</property> <!-- druid --> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize">1</property> <property name="minIdle">1</property> <property name="maxActive">20</property> <!-- 映射文件 --> <mapping resource="Dept.hbm.xml"/> </session-factory> </hibernate-configuration>
TestApp.java
package com.test; import com.entity.Dept; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.junit.Test; public class App { @Test public void testHibernate(){ SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); sessionFactory.openSession().beginTransaction(); Session session = sessionFactory.openSession(); Dept dept = session.get(Dept.class,14); System.out.println(dept.getName()); } }
然后运行测试类的时候就发现连接池无法初始化,一直卡着不动,只好强行shutdown。出错日志如下
七月 21, 2017 4:31:46 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@136432db: startup date [Fri Jul 21 16:31:46 CST 2017]; root of context hierarchy
七月 21, 2017 4:31:46 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [bean.xml]
七月 21, 2017 4:31:46 下午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.10.Final}
七月 21, 2017 4:31:46 下午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
七月 21, 2017 4:31:47 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
七月 21, 2017 4:31:48 下午 org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator initiateService
INFO: HHH000130: Instantiating explicit connection provider: com.alibaba.druid.support.hibernate.DruidConnectionProvider
七月 21, 2017 4:31:48 下午 org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator initiateService
WARN: HHH000342: Could not obtain connection to query metadata : null
七月 21, 2017 4:31:48 下午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
七月 21, 2017 4:31:48 下午 org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl makeLobCreatorBuilder
INFO: HHH000422: Disabling contextual LOB creation as connection was null
Process finished with exit code 1
解决方案:
查了一下druid的wiki,发现原来是自己的配置文件写错了,应该改成下面这样
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="connection.provider_class">com.alibaba.druid.support.hibernate.DruidConnectionProvider</property>
<property name="driverClassName">com.mysql.jdbc.Driver</property>
<property name="url">jdbc:mysql://localhost:3306/hib_demo</property>
<property name="username">root</property>
<property name="password">123456</property>
<property name="hibernate.show_sql">true</property>
<!-- druid -->
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize">1</property>
<property name="minIdle">1</property>
<property name="maxActive">20</property>
<!-- 映射文件 -->
<mapping resource="Dept.hbm.xml"/>
</session-factory>
</hibernate-configuration>
原因是:
如果使用
hibernate.connection.driver_class,hibernate.connection.url,hibernate.connection.username,hibernate.connection.password
进行配置,
则会初始化hibernate默认的连接池,而不会使用druid连接池,
所以要改成driverClassName、url、username、password。
具体细节请移步 druid wiki