我使用以下代码开始并提交事务。
final static Object lock = new Object();
@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public String addRequest(String actionCode) throws Exception {
synchronized (this.getClass()) {
Session session = dao.getCurrentSession();
try {
System.out.println("start");
Transaction tr = session.getTransaction();
tr.begin();
certRequest
.setRequestNbr(generateRequestNumber(certInsuranceRequestAddRq
.getAccountInfo().getAccountNumberId()));
reqId = Utils.getUniqueId();
certRequest.setRequestId(reqId);
ItemIdInfo itemIdInfo = new ItemIdInfo();
itemIdInfo.setInsurerId(certRequest.getRequestId());
certRequest.setItemIdInfo(itemIdInfo);
Serializable it = session.save(certRequest);
if (it != null) {
addAccountRel();
System.out.println("\n \n Transaction value is as " + it);
if (!tr.wasCommitted())
tr.commit();
System.out.println("\n \n Refresh value is "
+ generateRequestNumber(certInsuranceRequestAddRq
.getAccountInfo().getAccountNumberId()));
}
} catch (HibernateException e) {
e.printStackTrace();
} finally {
session.close();
}
System.out.println("end1");
}
return reqId;
}
但是我得到以下例外。
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.aspectj.AbstractTransactionAspect.ajc$afterReturning$org_springframework_transaction_aspectj_AbstractTransactionAspect$3$2a73e96c(AbstractTransactionAspect.aj:82)
at com.csc.exceed.uow.RequestAddUOW.addRequest(RequestAddUOW.java:405)
at com.csc.exceed.uow.RequestAddUOW.invokeService(RequestAddUOW.java:82)
at resources.rules.uow.Rule_Add_Request_0.defaultConsequence(Rule_Add_Request_0.java:7)
at resources.rules.uow.Rule_Add_Request_0DefaultConsequenceInvoker.evaluate(Rule_Add_Request_0DefaultConsequenceInvoker.java:29)
at org.drools.common.DefaultAgenda.fireActivation(DefaultAgenda.java:917)
at org.drools.common.DefaultAgenda.fireNextItem(DefaultAgenda.java:856)
at org.drools.common.DefaultAgenda.fireAllRules(DefaultAgenda.java:1071)
at org.drools.common.AbstractWorkingMemory.fireAllRules(AbstractWorkingMemory.java:785)
at org.drools.common.AbstractWorkingMemory.fireAllRules(AbstractWorkingMemory.java:761)
at org.drools.impl.StatefulKnowledgeSessionImpl.fireAllRules(StatefulKnowledgeSessionImpl.java:226)
at com.csc.exceed.infrastructure.BusinessProcessManager.executeRules(BusinessProcessManager.java:35)
at com.csc.exceed.infrastructure.BusinessProcessManager.executeUOWSequence(BusinessProcessManager.java:72)
at com.csc.exceed.controller.RequestHandler.service(RequestHandler.java:169)
at com.csc.exceed.controller.ControllerServlet.doPost(ControllerServlet.java:54)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
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:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)
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)
... 32 more
error msz is Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction not successfully started
我也试过了 事务tr = session.beginTransaction();
代替"交易tr = session.getTransaction(); tr.begin();"但那也行不通。请提出建议!
答案 0 :(得分:0)
编写事务同步代码很难,一旦开始将应用程序部署到多个服务器,就会失败。要解决此问题,请使用适当的isolation level进行交易
首先删除所有交易和会话管理代码以及同步的内容,因为您不需要它。接下来只需使用@Transactional(isolation=SERIALIZABLE)
注释您的方法即可锁定数据库级别,无论您拥有多少个应用程序服务器,都可以使用。
@Transactional(isolation=SERIALIZABLE, rollbackFor = Exception.class)
public String addRequest(String actionCode) throws Exception {
Session session = dao.getCurrentSession();
System.out.println("start");
certRequest
.setRequestNbr(generateRequestNumber(certInsuranceRequestAddRq
.getAccountInfo().getAccountNumberId()));
reqId = Utils.getUniqueId();
certRequest.setRequestId(reqId);
ItemIdInfo itemIdInfo = new ItemIdInfo();
itemIdInfo.setInsurerId(certRequest.getRequestId());
certRequest.setItemIdInfo(itemIdInfo);
Serializable it = session.save(certRequest);
if (it != null) {
addAccountRel();
System.out.println("\n \n Transaction value is as " + it);
System.out.println("\n \n Refresh value is "
+ generateRequestNumber(certInsuranceRequestAddRq
.getAccountInfo().getAccountNumberId()));
}
System.out.println("end1");
return reqId;
}
对我而言,您的代码看起来也有缺陷,因为它看起来像reqId
和certRequest
是实例变量,我没有看到它们传递给方法。出于某种原因,我发现你生成requestNumber
两次,一次在外面,一次在if
语句中,这也是错误的。
答案 1 :(得分:0)
您正在尝试创建一个新的事务,以为您似乎在说Spring来处理事务。无论你或它应该这样做。
从帖子上的评论/链接中,我了解到您有一些脏读,这些不允许您达到您想要的效果。您的Isolation.SERIALIZABLE
会帮助您。
无论M. Deinum发布的是正确的。