每次启动批处理作业时,它都会引发IllegalStateException并说它在JobRepository中检测到事务。我做了一些研究,并删除了代码中的所有@Transactional
注释。
我使用Grails Spring Batch插件,可以找到here,并且使用Grails 2.3.11和Java8。我的代码如下所示:
SimpleJobBatchConfig.groovy
beans {
xmlns batch:"http://www.springframework.org/schema/batch"
batch.job(id: 'simpleJob') {
batch.step(id: 'printStep') {
batch.tasklet(ref: 'printHelloWorld')
}
}
printHelloWorld(SimpleJobTasklet) { bean ->
bean.autowire = 'byName'
}
}
BatchTestController.groovy
class BatchelorController {
def batchTestService
def index() {
}
def launchSimpleJob() {
batchTestService.launchSimpleJob()
}
}
BatchTestService.groovy
class BatchTestService {
def springBatchService
def launchSimpleJob() {
springBatchService.launch("simpleJob")
}
}
SimpleJobTasklet.groovy
class SimpleJobTasklet implements Tasklet {
@Override
RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
println("Hello World!")
return RepeatStatus.FINISHED
}
}
最佳答案
默认情况下,Grails服务是事务性的。您可以使用@Transactional
自定义整个类或每个方法的设置,但如果没有注释,则与具有类作用域的Spring @Transactional
注释相同。
要使您的服务不可交易,请添加static transactional = false
,例如
class BatchTestService {
static transactional = false
def springBatchService
...
}
}
关于grails - JobRepository中检测到的现有事务-带有Grails插件的Spring Batch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32308793/