Hibernate事务阻塞问题

时间:2013-12-10 17:04:28

标签: spring hibernate transactions one-to-many

以下是相关 DAO

的代码

@Autowired     private 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 个答案:

答案 0 :(得分: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();       
}