我正在使用GORM工具将域对象与数据库映射,下面是代码:
public BatchJobMetaData getBatchJobMetaDataById(Integer batchJobMetaDataId){
return BatchJobMetaData.get(batchJobMetaDataId)
}
我得到了"启发式完成:结果状态被回滚;"异常
完全异常轨道:
Caused by: org.springframework.transaction.HeuristicCompletionException: Heuristic completion: outcome state is rolled back; nested exception is java.lang.NullPointerException
at org.codehaus.groovy.grails.transaction.ChainedTransactionManager.commit(ChainedTransactionManager.java:174)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:478)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:272)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:644)
at com.cscglobal.dbstools.brandreports.service.PortfolioStrategyService$$EnhancerBySpringCGLIB$$2dc9d6ea.getPortfolioStrategInput(<generated>)
at com.cscglobal.dbstools.brandreports.service.PortfolioStrategyService$getPortfolioStrategInput$0.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at com.cscglobal.dbstools.batch.brandreports.portfoliostrategy.PortfolioStrategyListItemReader.beforeStep(PortfolioStrategyListItemReader.groovy:31)
... 20 more
Caused by: java.lang.NullPointerException
at org.springframework.orm.hibernate4.SpringSessionSynchronization.getCurrentSession(SpringSessionSynchronization.java:51)
at org.springframework.orm.hibernate4.SpringSessionSynchronization.beforeCommit(SpringSessionSynchronization.java:85)
at org.springframework.transaction.support.TransactionSynchronizationUtils.triggerBeforeCommit(TransactionSynchronizationUtils.java:95)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.triggerBeforeCommit(AbstractPlatformTransactionManager.java:928)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:740)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:726)
at org.codehaus.groovy.grails.transaction.MultiTransactionStatus.commit(MultiTransactionStatus.java:73)
at org.codehaus.groovy.grails.transaction.ChainedTransactionManager.commit(ChainedTransactionManager.java:147)
我已经尝试使用withSession来解决问题,但可以解决问题:
public BatchJobMetaData getBatchJobMetaDataById(Integer batchJobMetaDataId){
BatchJobMetaData batchJobMetaData = new BatchJobMetaData();
BatchJobMetaData.withSession{
batchJobMetaData = BatchJobMetaData.get(batchJobMetaDataId)
}
return batchJobMetaData
}
我也用过这个:
@Transactional(propagation = Propagation.REQUIRES_NEW)
public BatchJobMetaData getBatchJobMetaDataById(Integer batchJobMetaDataId){
return BatchJobMetaData.get(batchJobMetaDataId)
}
我也用过这个:
@Transactional(propagation = Propagation.REQUIRED)
public BatchJobMetaData getBatchJobMetaDataById(Integer batchJobMetaDataId){
return BatchJobMetaData.get(batchJobMetaDataId)
}
但仍有例外发生。你能帮我处理这个例外吗:
启发式完成和回滚可能是由回滚事务的MySQL查询失败引起的。有关详细信息,请参阅日志。有时,您可以看到查询在一台服务器上工作,而在另一台服务器中失败。在这种情况下,尝试比较MYSQL模式并删除一些语法变化模式,如">";FULL_GROUP_BY">。
要在服务器启动时设置SQL模式,请使用命令行上的--sql-mode="modes"
选项,或使用选项文件(如my.cnf
(中的sql-mode="modes"
。
要在运行时更改SQL模式,请使用set语句设置全局或会话SQL_mode系统变量:
SET GLOBAL sql_mode = 'modes';
SET SESSION sql_mode = 'modes';
# Adding a mode to sql_mode:
SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
# Removing a mode from sql_mode:
SET GLOBAL sql_mode=(SELECT CONCAT(@@sql_mode,',ONLY_FULL_GROUP_BY'));
最后,我得到了答案:
使用withTransaction就是解决方案。
public BatchJobMetaData getBatchJobMetaDataById(Integer batchJobMetaDataId){
BatchJobMetaData batchJobMetaData = new BatchJobMetaData()
BatchJobMetaData.withTransaction { status ->
batchJobMetaData = BatchJobMetaData.get(batchJobMetaDataId)
}
return batchJobMetaData
}