0

我正在使用hibernate與數據庫交互。插入,刪除,更新操作沒有問題,因爲它們以commit語句結束session.getTransaction.commit()使用休眠模式讀取數據不會與數據庫同步

雖然selecting data,listing records hibernate返回之前顯示的數據,並且不顯示所有新記錄或更新。

因此,在提出這個問題之前,我嘗試過(兩週前)導航到類似的問題,但在應用所有建議時沒有找到答案。

(A)啓用二級緩存(b)增加隔離級別

這對我變得奇怪,因爲當我想最近更新插入的記錄,我得到以下。

HTTP Status 500 - No row with the given identifier exists: [com.bd.model.TestType#15] 

type Exception report 
message No row with the given identifier exists: [com.bd.model.TestType#15] 

description The server encountered an internal error that prevented it from fulfilling this request. 

exception 

org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.bd.model.TestType#15] 
    org.hibernate.impl.SessionFactoryImpl$1.handleEntityNotFound(SessionFactoryImpl.java:377) 
    org.hibernate.proxy.AbstractLazyInitializer.checkTargetState(AbstractLazyInitializer.java:79) 
    org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:68) 
    org.hibernate.Hibernate.initialize(Hibernate.java:306) 
    com.bnr.clinic.services.TestTypeServices.getTestTypeById(TestTypeServices.java:79) 
    com.bnr.clinic.controller.TestTypeUpdateController.doPost(TestTypeUpdateController.java:85) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:646) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727) 
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.52 logs. 

這裏是我正在使用的選擇方法!

public TestType getTestTypeById(int idTestType) { 
    session = sf.getCurrentSession(); 
    session.beginTransaction(); 
    session.clear(); 
    TestType testTypes = (TestType) session.load(TestType.class, idTestType); 
    Hibernate.initialize(testTypes); 
    return testTypes; 
} 

我的Hibernate配置文件是這樣的:

<?xml version="1.0" encoding="UTF-8"?> 
    <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
    <hibernate-configuration> 
     <session-factory> 
<!-- Database connectivity --> 
      <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 
      <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
      <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mis</property> 
      <property name="hibernate.connection.username">root</property> 
      <property name="hibernate.connection.password">@ict#</property> 
      <property name="connection.pool_size">1</property> 

      <!-- Enable Hibernate's automatic session context management --> 

      <property name="current_session_context_class">thread</property> 
      <property name="connection.autocommit">true</property> 

<!-- Disabling timeout --> 
      <property name="connection.autoReconnect"> true</property> 
      <property name="connection.autoReconnectForPools">true</property> 
      <property name="c3p0.min_size">5</property> 
      <property name="c3p0.max_size">20</property> 
      <property name="c3p0.timeout">1800</property> 
      <property name="c3p0.max_statements">50</property> 
      <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> 
      <property name="connection.release_mode">auto</property> 

      <!-- Disable the second-level cache --> 

      <property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property> 
      <property name="hibernate.cache.use_second_level_cache">false</property> 
      <property name="hibernate.cache.use_query_cache">true</property> 
      <property name="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</property> 

      <!-- Echo all executed SQL to stdout --> 
      <property name="show_sql">true</property> 
      <property name="hbm2ddl.auto">update</property> 
      <mapping class="com.bd.model.Test" /> 
      <mapping class="com.bd.model.TestType" /> 


     </session-factory> 
    </hibernate-configuration> 

所以我很高興地問兩個問題:

有任何一點毛病我的方法還是我錯了我休眠配置?

什麼是阻止休眠與數據庫同步以獲取新插入的記錄?

謝謝。

回答

0

我意識到每一個事務的開始,都必須致力於確保hibernate與數據庫同步。如果這些READING(SELECT)事務沒有被提交,並且在新記錄加載到數據庫中時繼續獲得相同記錄,那麼Hibernate使用緩存。

我的配置是優良的唯一變化是代表的服務方法

public TestType getTestTypeById(int idTestType) { 
    Session session = sf.openSession(); // sf as a sessionFactory instance 
    Transaction tx = null; 
    TestType testType = null; 
    try { 
     tx = session.beginTransaction(); 
     testType = (TestType) session.get(TestType.class, idTestType); 
     tx.commit(); 
    } catch (HibernateException e) { 
     if (tx != null) 
      tx.rollback(); 
     e.printStackTrace(); 
    } finally { 
     session.close(); 
    } 

    return testType; 
}