druid连接oracle初始化错误,Hibernate配置Druid出错,无法初始化连接池
今天尝试了一下阿里的druid和hibernate结合,没想到一直报错。配置代码如下hibernate.cfg.xml/p>"-//Hibernate/Hibernate Configuration DTD//EN""http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">org.hibernate.dialect.M
今天尝试了一下阿里的druid和hibernate结合,没想到一直报错。
配置代码如下
hibernate.cfg.xml
/p>
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
org.hibernate.dialect.MySQL5Dialect
com.alibaba.druid.support.hibernate.DruidConnectionProvider
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/hib_demo
root
123456
true
1
1
20
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
INFO: HHH000206: hibernate.properties not found
七月 21, 2017 4:31:47 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager
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
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,发现原来是自己的配置文件写错了,应该改成下面这样
/p>
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
org.hibernate.dialect.MySQL5Dialect
com.alibaba.druid.support.hibernate.DruidConnectionProvider
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/hib_demo
root
123456
true
1
1
20
原因是:
如果使用
hibernate.connection.driver_class,hibernate.connection.url,hibernate.connection.username,hibernate.connection.password
进行配置,
则会初始化hibernate默认的连接池,而不会使用druid连接池,
所以要改成driverClassName、url、username、password。
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)