我不确定我的机会,但我需要将一个JDBC事务与hibernate事务合并。只有当两个事务都成功执行时,才会提交事务。但看起来由于自动提交是错误的,JDBC事务正在锁定表,我最终得到了异常
12:47:52,605 WARN JDBCExceptionReporter:77 - SQL Error: 1205, SQLState: 41000
12:47:52,605 ERROR JDBCExceptionReporter:78 - Lock wait timeout exceeded; try restarting transaction
以下是方法
@Transactional
public void myMthod(int fieldId, int currentActiveOrgId, int formId)
{
Savepoint savePoint = null;
String savepointName = null;
try{
//Set auto commit false for sql statemnt
getDBConnection().setAutoCommit(false);
savepointName = "deleteDynamicField"+this.getClass().hashCode();
savePoint = getDBConnection().setSavepoint(savepointName);
DynamicQueryManager obj = DynamicQueryMgrObjFactory.getDynamicQueryMgrObj(currentActiveOrgId, formId);
//Resposible for sql statement
obj.deleteDynTblRec(fieldId);
//Resposible for hql transaction (part of the code generating exception)
dynDao.deleteDynamicField(fieldId);
DynamicQueryUtil.getDBConnection().commit();
}catch (Exception e) {
try {
logger.error("", e);
DynamicQueryUtil.getDBConnection().rollback(savePoint);
DynamicQueryUtil.getDBConnection().releaseSavepoint(savePoint);
}catch (Exception ex) {
logger.error("Failed to do rollback for savePoint "+savepointName+" ", ex);
}
}finally{
try{
DynamicQueryUtil.getDBConnection().commit();
DynamicQueryUtil.getDBConnection().setAutoCommit(true);
}catch (Exception e) {
logger.error("", e);
}
}
}
以下是完整的堆栈跟踪
org.hibernate.exception.GenericJDBCException: could not execute update query
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.hql.ast.exec.BasicExecutor.execute(BasicExecutor.java:84)
at org.hibernate.hql.ast.QueryTranslatorImpl.executeUpdate(QueryTranslatorImpl.java:396)
at org.hibernate.engine.query.HQLQueryPlan.performExecuteUpdate(HQLQueryPlan.java:259)
at org.hibernate.impl.SessionImpl.executeUpdate(SessionImpl.java:1141)
at org.hibernate.impl.QueryImpl.executeUpdate(QueryImpl.java:94)
at com.bredge.peer.platform.dynamicdata.dao.DynamicDataDaoImpl.deleteDynamicField(DynamicDataDaoImpl.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:196)
at $Proxy113.deleteDynamicField(Unknown Source)
at com.bredge.peer.platform.dynamicdata.service.DynamicDataServiceImpl.deleteDynamicField(DynamicDataServiceImpl.java:62)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy114.deleteDynamicField(Unknown Source)
at com.bredge.peer.platform.dynamicdata.controller.DyanmicDataController.deleteDynamicField(DyanmicDataController.java:109)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:174)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:421)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:409)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
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: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:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.sql.SQLException: Lock wait timeout exceeded; try restarting transaction
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3558)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3490)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2109)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2648)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2077)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2362)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2280)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2265)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:233)
at org.hibernate.hql.ast.exec.BasicExecutor.execute(BasicExecutor.java:75)
... 50 more
这里我想在hibernate事务中创建一个简单的JDBC事务,这样如果发生任何异常,它将回滚JDBC事务以及hibernate事务。是的由于应用程序的某些限制,我无法将sql部分的JDBC代码迁移到hibernate。 在此先感谢。堆栈溢出很棒。
答案 0 :(得分:0)
示例中的新事务从另一个数据库连接开始。嵌套事务只能在同一连接上进行,并非所有数据库都支持它们。您唯一的机会是从Hibernate会话中获取连接并在其上执行JDBC操作。
Hibernate为Hibernate会话封装了数据库连接,但是如果你实现了一个自定义的Hibernate会话工厂,你可以解决这个问题。