2013-12-10 78 views
0

這裏是我的相關DAOHibernate事務塊問題

@Autowired 私人SessionFactory的SessionFactory的代碼;

public void getTabletbyNameAndSave(String []selectedMedicines) { 
    sessionFactory.getCurrentSession().beginTransaction(); 
    EPrescriber ePrescriber=new EPrescriber(); 
    List<SelectedTablets> selectedTabletsList=new ArrayList<SelectedTablets>(); 
    for (String item : selectedMedicines) { 

     Tablets tablets=null; 
     String hql="from Tablets t where t.category='"+item.trim()+"' "; 
     Query queryList = sessionFactory.getCurrentSession().createQuery(hql); 
     tablets=(Tablets)queryList.uniqueResult(); 

     SelectedTablets selectedTablets=new SelectedTablets(); 
     selectedTablets.setTablets(tablets); 
     selectedTablets.setePrescriberid(ePrescriber); 
     selectedTabletsList.add(selectedTablets); 
     sessionFactory.getCurrentSession().save(selectedTablets); 

    } 

    ePrescriber.setPatientName("Kasun"); 
    ePrescriber.setSelectedTablets(selectedTabletsList); 
    sessionFactory.getCurrentSession().save(ePrescriber); 
    sessionFactory.getCurrentSession().getTransaction().commit();  
} 

這是我的錯誤。看來我的事務塊沒有正確處理。請幫助在這個問題上給我建議。謝謝!

org.springframework.transaction.TransactionSystemException: Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction not successfully started 
    at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:660) 
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754) 
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723) 
    at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:393) 
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
    at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:621) 
    at com.priyan.patients.EPrescriberDAO$$EnhancerByCGLIB$$3ce885f5.getTabletbyNameAndSave(<generated>) 
    at com.priyan.patients.ContactsControllers.setTabletsNames(ContactsControllers.java:328) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176) 
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426) 
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414) 
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790) 
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719) 
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644) 
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:643) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:723) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) 
    at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:879) 
    at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:617) 
    at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1760) 
    at java.lang.Thread.run(Unknown Source) 
Caused by: org.hibernate.TransactionException: Transaction not successfully started 
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:131) 
    at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:656) 
    ... 33 more 

回答

1

我會嘗試手動分配交易到一個局部變量,以確保您正在運行在正確的交易最終提交。像下面這樣:

@Autowired private SessionFactory sessionFactory; 

public void getTabletbyNameAndSave(String []selectedMedicines) { 

    Session currentSession = sessionFactory.getCurrentSession(); 
    Transaction tx = currentSession.beginTransaction(); 

    EPrescriber ePrescriber=new EPrescriber(); 
    List<SelectedTablets> selectedTabletsList=new ArrayList<SelectedTablets>(); 
    for (String item : selectedMedicines) { 

     Tablets tablets=null; 
     String hql="from Tablets t where t.category='"+item.trim()+"' "; 
     Query queryList = currentSession.createQuery(hql); 
     tablets=(Tablets)queryList.uniqueResult(); 

     SelectedTablets selectedTablets=new SelectedTablets(); 
     selectedTablets.setTablets(tablets); 
     selectedTablets.setePrescriberid(ePrescriber); 
     selectedTabletsList.add(selectedTablets); 
     currentSession.save(selectedTablets); 

    } 

    ePrescriber.setPatientName("Kasun"); 
    ePrescriber.setSelectedTablets(selectedTabletsList); 
    currentSession.save(ePrescriber); 
    tx.commit();  
} 
+0

親愛的朋友drembert,我嘗試了你的代碼,但問題仍然是一樣的。 –

+1

我不確定你的環境設置是什麼,但是你確定你沒有運行一個陳舊的.class文件。我知道有時候這可能發生在Eclipse中,我問的唯一原因是因爲我相當確信您的問題是由於事務不匹配造成的。 – drembert

+0

其與SPring + hibernate應用程序 也作爲DB的postgresql –