Hibernate插入数据时报org.hibernate.service.UnknownServiceException异常
来源:互联网 发布:淘宝优惠券怎么使用 编辑:程序博客网 时间:2024/05/29 08:58
异常的内容如下:
org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.engine.jdbc.connections.spi.ConnectionProvider]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:201)
at org.hibernate.internal.AbstractSessionImpl.getJdbcConnectionAccess(AbstractSessionImpl.java:341)
at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.<init>(JdbcCoordinatorImpl.java:114)
at org.hibernate.engine.transaction.internal.TransactionCoordinatorImpl.<init>(TransactionCoordinatorImpl.java:89)
at org.hibernate.internal.SessionImpl.<init>(SessionImpl.java:258)
at org.hibernate.internal.SessionFactoryImpl$SessionBuilderImpl.openSession(SessionFactoryImpl.java:1589)
at org.hibernate.context.internal.ThreadLocalSessionContext.buildOrObtainSession(ThreadLocalSessionContext.java:157)
at org.hibernate.context.internal.ThreadLocalSessionContext.currentSession(ThreadLocalSessionContext.java:109)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
at travel.bean.impl.UserBeanImpl.addUser(UserBeanImpl.java:50)
at travel.service.impl.UserServiceImpl.UsersReg(UserServiceImpl.java:31)
at travel.service.TestUsersReg.TestUserReg(TestUsersReg.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
查了很久的原因,发现是调用的两个方法,都要开启 sessionFactory .
我的解决办法时将前面一个调用的方法中的 sessionFactory 最后置为空,sessionFactory = null;
在下一个方法中就能重新调用啦,成功解决~
-------------------------------------------------------
以上是昨天的解决方案,今天早上测试时发现不关闭 SessionFactory 会使系统变卡,应该是在每个方法中都重新 new 了个,,
所以,我想到了将 SessionFactory 重用,
public class MyHibernateSessionFactory {
private final static Configuration configuration = new Configuration().configure();
private final static ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
private final static SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
private MyHibernateSessionFactory() {
}
public static Session getSession() {
return sessionFactory.openSession();
}
private static class SingletonHolder {
private final static MyHibernateSessionFactory INSTANCE = new MyHibernateSessionFactory();
}
public static MyHibernateSessionFactory getInstance() {
return SingletonHolder.INSTANCE;
}
}
在外部重新定义,然后在方法中调用
public Users SearchUser() {
Session session = MyHibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
try{
//do something
}catch(Exception ex){
//do something;
}finally{
tx = null;
session.close();
}
}
搞定,如果后续有问题我再跟新
- Hibernate插入数据时报org.hibernate.service.UnknownServiceException异常
- org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.stat.spi.Sta
- Hibernate插入数据时报Could not open Hibernate Session for transaction异常
- JPA保存数据时报持久化对象异常:PersistenceException: org.hibernate.PersistentObjectException
- 保存数据,发生异常 org.hibernate.TransientObjectException
- Hibernate org.hibernate.TypeMismatchException异常
- Hibernate 异常org.hibernate.LazyInitializationException
- Hibernate org.hibernate.TypeMismatchException异常 .
- hibernate 异常 org.hibernate.MappingException
- org.hibernate.AssertionFailure异常
- org.hibernate.LazyInitializationException异常
- 在整合hibernate时报异常
- Hibernate 4.3关于org.hibernate.service.jndi.JndiException: Error parsing JNDI name [foo]等异常
- hibernate异常Unable to create requested service [org.hibernate.engine.spi.CacheImple
- Hibernate 乐观锁 org.hibernate.StaleObjectStateException 异常
- Hibernate 乐观锁 org.hibernate.StaleObjectStateException 异常
- hibernate异常:org.hibernate.id.…
- hibernate异常分析:org.hibernate.ObjectNotFoundException
- 同步异步1:有线网络中的同步异步
- 发现的一些github优秀分类集合(待完善)
- 范型,迭代Map
- [Lintcode] Matrix Zigzag Traversal
- CPU和GPU的区别
- Hibernate插入数据时报org.hibernate.service.UnknownServiceException异常
- ios闭包循环引用精讲
- 变量的初始化
- C++ 自制Redis 数据库(十一)哈希类与数据库类
- 225. Implement Stack using Queues 用两个队列实现栈的基本操作。
- Linux笔记(5)——搜索命令
- 内部类基本知识点
- 文件操作杂记
- HDU 4300 Clairewd’s message