1. 程式人生 > >Hibernate插入資料時報org.hibernate.service.UnknownServiceException異常

Hibernate插入資料時報org.hibernate.service.UnknownServiceException異常

異常的內容如下:

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.engine.jdbc.connections.spi.ConnectionPr

關於hibernate查詢資料時報錯java.lang.StringIndexOutOfBoundsException: String index out of range: 0

今天學習struts2框架,重寫登入頁面,在登入時碰到這個問題,久久不能解決,後來用度娘一查,果斷出結果。 原文地址: 答案如下: 報錯:java.lang.StringIndexOutOfBoundsException: String index out of

Hibernate插入資料時取欄位的預設值,更新時只更新修改過的屬性值

         使用Hibernate向資料庫插入或更新資料的時候 ,如果想要取到資料庫欄位的預設值,可以在類的Hibernate對映檔案的Class標籤中加上dynamic-insert和dynamic-update。如果資料庫欄位是可以為空且有預設值 的情況下,只要在

Hibernate插入資料到資料庫中時讓欄位預設值生效

資料庫中一個欄位的預設值設為0,當用hibernate插入資料時,沒有對該欄位進行操作,結果該欄位居然不是0,而是空。後來google了一下,發現應該在.hbm.xml檔案中新增一些引數定義(示例中的紅色部分),具體如下: <hibernate-mapping> 

配置ssh框架啟動tomcat服務器報異常Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]

clas 系統默認 lba nco not -1 path xml配置 mysql用戶 在Spring中配置jdbc時,引用的是dbcp.jar包,在db.properties配置文件中,使用了之前的properties配置文件的用戶名username(MySql用戶名)

Hibernate **關於hibernate4.3版本之後org.hibernate.service.ServiceRegistryBuilder被棄用**

讓我 IE 發現 code .com registry 都是 開啟事務 tran 之前一直都是使用hibernate4.2.21的我,有一天突然沒有使用本地的jar包而是讓IDEA自動下載最新版本的hibernate5.2.2之後,發現有幾個經常使用的方法報錯了. /

Hibernate使用Gson時報錯:org.hibernate.proxy.HibernateProxy. Forgot to register a type adapter?

Hibernate使用Gson時報錯:org.hibernate.proxy.HibernateProxy. Forgot to register a type adapter? 主要是使用Hibernate查詢的實體類有多對一的關係,Hibernate預設延遲載入,不會查詢關聯的實體一方,只有

Hibernate入門 批量插入資料

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

關於hibernate插入資料報read-only mode只讀模式錯誤

專案中有時候會遇到插入資料到表中去報錯只讀模式 org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (F

hibernate 報錯Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]

第一篇解決hibernate報錯文章,希望能幫助到同樣有這個錯誤的童鞋們~ 報錯 org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.en

JPA中hibernate資料無法插入卻不報錯(JPA儲存boolean值)

2017-03-26 20:17:840 | DEBUG | org.springframework.aop.framework.CglibAopProxy.accept() - Found 'equals' method: public boolean java.lang.Object.equals(jav

Hibernate 學習心得1: 多對多關係中,中間表無法插入資料

Hibernate 學習心得之一 多對多關係中,中間表無法插入資料。最近學習 spring4+hibernate4,學習中遇到了很多坑。在這裡我來說說我遇到的坑,這裡就不介紹如何spring如何整合hibernate。目前學習過程中,我遇到的兩個問題1.為何在hibernat

hibernate迴圈插入資料

       在使用hibernate持久化資料時,經常會預見迴圈插入資料的問題。一個有趣的現象是,在迴圈插入資料的時候,只有第一條資料時真正插入到資料庫的,剩餘的都在對這條資料進行更新操作。       例如如下程式碼: <pre class="java" name

SSH框架問題——hbm.xml配置出現的org.hibernate.MappingException: Could not determine type for:(貌似叫資料型別轉換錯誤)問題

不知道大家有沒有碰到過這樣一個報錯: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDaoImp': Injection of reso

使用Hibernate向Oracle中插入資料

Hibernate是ORMapping的實現,通過一個類實現資料庫操作例項:hibernate.cfg.xml:Hibernate環境配置檔案 程式程式碼<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE hibe

java.lang.UnsupportedClassVersionError: org/hibernate/SessionFactory : Unsupported major.minor version 52.0

ner jdk1.7 actor supported 新的 min hiberna 導入 ava 問題: 在學習hibernate的過程中,涉及導入jar包的問題,寫該文的時候是從官網下載的最新的hibernate的jar包hibernate-release-5.2.10.

org.hibernate.HibernateException: Wrong column type

property 入門 錯誤 lar per connect set conn 定義 這個問題一般出現在我們使用定長的字符串作為主鍵(其它字段也可能)的時候,如數據庫中的ID為char(16)。雖然很多資料上都說不推薦這樣做,但實際上我們在做很多小case的

org.hibernate.LazyInitializationException

poi error solver epo splay char imp 延遲 kth org.hibernate.LazyInitializationException: could not initialize proxy - no Session at or

org.hibernate.exception.GenericJDBCException: could not execute statement

intern err class sed urn ldr get protocol servlet 異常信息: Exception: org.hibernate.exception.GenericJDBCException: could not execute statem

Hibernate裏面表間有連接,查詢一個表出org.hibernate.HibernateException: HHH000142: Javassist Enhancement failed: 表聯系的類名 異常

延遲加載 配置文件 poj led 希望 java main.c exceptio 加載 這個問題我學Hibernate的時候遇到了,各種方法都試過,兩天才查出了,不過學的不是很深。感覺是因為表間聯系才出的問題。 最近又有人來問我這個問題,他是其他的都能查出來,但是排序查詢