我正在使用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)
}

但是仍然发生异常。您能否帮我解决这个异常(exception)情况:

最佳答案

最后,我得到了答案:

解决方案是使用withTransction。

public BatchJobMetaData getBatchJobMetaDataById(Integer batchJobMetaDataId){
        BatchJobMetaData batchJobMetaData = new BatchJobMetaData()
        BatchJobMetaData.withTransaction { status ->
            batchJobMetaData = BatchJobMetaData.get(batchJobMetaDataId)
         }
        return batchJobMetaData
    }

09-09 17:15