2015-09-25 33 views
1

我試圖更新我的應用程序使用的session-per-request模式,所以我可以移動到較新版本的GWT(我的實體不保存正確過去2.4 - GWT >2.4 RequestFactory not saving child object changesRequestfactory休眠的session-per-要求

我實現了一個請求過濾器,它似乎工作正常 - 我可以將數據拉到客戶端沒有問題,但是,當我嘗試保存一個實體時,由於它沒有找到活動事務而出錯:

org.hibernate.HibernateException: saveOrUpdate is not valid without active transaction 

我把大部分關於如何實現這個模式的信息從https://developer.jboss.org/wiki/OpenSessionInView中拿出來,下面是我的過濾器:

public class HibernateSessionRequestFilter implements Filter { 

private static Log log = LogFactory.getLog(HibernateSessionRequestFilter.class); 

private SessionFactory sf; 

public void doFilter(ServletRequest request, 
        ServletResponse response, 
        FilterChain chain) 
     throws IOException, ServletException { 

    try { 
     System.out.println("Starting a database transaction"); 
     sf.getCurrentSession().beginTransaction(); 

     // Call the next filter (continue request processing) 
     chain.doFilter(request, response); 

     // Commit and cleanup 
     System.out.println("Committing the database transaction"); 
     sf.getCurrentSession().getTransaction().commit(); 

    } catch (StaleObjectStateException staleEx) { 
     log.error("This interceptor does not implement optimistic concurrency control!"); 
     log.error("Your application will not work until you add compensation actions!"); 
     // Rollback, close everything, possibly compensate for any permanent changes 
     // during the conversation, and finally restart business conversation. Maybe 
     // give the user of the application a chance to merge some of his work with 
     // fresh data... what you do here depends on your applications design. 
     throw staleEx; 
    } catch (Throwable ex) { 
     // Rollback only 
     ex.printStackTrace(); 
     try { 
      if (sf.getCurrentSession().getTransaction().isActive()) { 
       System.out.println("Trying to rollback database transaction after exception"); 
       sf.getCurrentSession().getTransaction().rollback(); 
      } 
     } catch (Throwable rbEx) { 
      log.error("Could not rollback transaction after exception!", rbEx); 
     } 

     // Let others handle it... maybe another interceptor for exceptions? 
     throw new ServletException(ex); 
    } 
} 

public void init(FilterConfig filterConfig) throws ServletException { 
    System.out.println("Initializing filter..."); 
    System.out.println("Obtaining SessionFactory from static HibernateUtil singleton"); 
    sf = HibernateUtil.getSessionFactory(); 
} 

public void destroy() {} 

} 

我的web.xml:

<servlet> 
    <servlet-name>requestFactoryServlet</servlet-name> 
    <servlet-class>com.example.server.util.ExampleRequestFactoryServlet</servlet-class> 
    </servlet> 

    <servlet-mapping> 
    <servlet-name>requestFactoryServlet</servlet-name> 
    <url-pattern>/gwtRequest</url-pattern> 
    </servlet-mapping> 

    <filter> 
     <filter-name>HibernateFilter</filter-name> 
     <filter-class>com.example.server.util.HibernateSessionRequestFilter</filter-class> 
    </filter> 

    <filter-mapping> 
     <filter-name>HibernateFilter</filter-name> 
     <url-pattern>/gwtRequest</url-pattern> 
    </filter-mapping> 

保存是非常簡單的:

// client 
private void saveScale(ScaleProxy scale) { 
    scaleRequest.save(scale) 
    .fire(new Receiver<Void>() { 
     @Override 
     public void onSuccess(Void response) { 
      Window.alert("Scale saved."); 
     }   
    }); 
} 

// server 
public static void save(Scale scale) { 
    Session session = HibernateUtil.getSessionFactory().getCurrentSession(); 

    session.saveOrUpdate(scale); 
} 

任何其他信息,我是否可以提供?我很欣賞任何想法或見解!

回答

1

您需要每個請求一個會話,但不是一個單獨的事務。在每種服務方法中打開和關閉事務,以防錯誤時的正確行爲。

+0

我想我現在回到原點。保存家長時,對孩子的更改不會持續。我做了一些額外的日誌記錄,並驗證了我在所有請求中使用了相同的Hibernate會話。我會繼續挖掘,謝謝。 – CyDharttha

+0

找到我的問題。在我上面詳述的更新之前,我一直在做'Hibernate.initialize(parent.getChildren());'查詢實體時。在我的舊代碼中,除非我這樣做,否則孩子不會初始化並在發送給客戶端時連接到父項。現在,在會話過濾器就位的情況下,這已不再需要,並且導致子實體的更新不會持久(會話沒有正確連接/關聯到父項)。謝謝您的幫助! – CyDharttha