Announcement Announcement Module
Collapse
No announcement yet.
Spring Jobs in transactional context Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Jobs in transactional context

    Hi,

    I am trying to run the job in the transactional method. The method is as below :
    Code:
    @Transactional
    private void runJob(CbValidationJob jb) {
    		System.out.println("In For rubJob()");
    
    		JobParametersBuilder parameters = new JobParametersBuilder()
    				.addString("inputResource", jb.getInputResource())
    				.addString("user", jb.getUserId()).addString("bu", jb.getBu())
    				.addString("batch_type", jb.getCardType())
    				.addString("client_ip", jb.getClientIp())
    				.addDate("date", new Date());
    
    		try {
    			JobExecution jobExecution = jobLauncher.run(job,
    					parameters.toJobParameters());
    			logger.info("Job ID : " + jobExecution.getId()
    					+ "Started with file :" + jb.getInputResource());
    			jb.setJobStatus("SHEDULED");
    			jb.setJobNo(jobExecution.getId());
    			cbValidationJobDao.merge(jb);
    
    		} catch (Exception e) {
    			logger.info(e.getMessage());
    			e.printStackTrace();
    		}
    	}
    The application transactional attributes are handled by
    Code:
    <tx:annotation-driven mode="aspectj"
    		transaction-manager="transactionManager" />
    The error is



    Code:
    2013-02-12 14:47:20,076 [scheduler-1] INFO  shedular - Existing transaction detected in JobRepository. Please fix this and try again (e.g. remove @Transactional annotations from client).
    java.lang.IllegalStateException: Existing transaction detected in JobRepository. Please fix this and try again (e.g. remove @Transactional annotations from client).
    	at org.springframework.batch.core.repository.support.AbstractJobRepositoryFactoryBean$1.invoke(AbstractJobRepositoryFactoryBean.java:164)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    	at $Proxy83.createJobExecution(Unknown Source)
    	at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:111)
    	at in.mahadiscom.batch.ValidationJobSchedular.runJob(ValidationJobSchedular.java:75)
    	at in.mahadiscom.batch.ValidationJobSchedular.scheduleJob(ValidationJobSchedular.java:55)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.lang.reflect.Method.invoke(Method.java:601)
    	at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:64)
    	at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:53)
    	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    	at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:351)
    	at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:178)
    	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:178)
    	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
    	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    	at java.lang.Thread.run(Thread.java:722)
    The jobrepository is configured using :

    Code:
    <bean id="jobRepository" init-method=""
    		class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
    		<property name="dataSource" ref="dataSource" />
    		<property name="transactionManager" ref="transactionManager" />
    		<property name="isolationLevelForCreate" value="ISOLATION_DEFAULT" />
    		<property name="databaseType" value="sybase" />
    		<property name="tablePrefix" value="billing.BATCH_" />
    		<property name="lobHandler" ref="lobHandler" />
    	</bean>
    If the @Transactional attribute is removed the Job runs , but not able to update database status in the database.

    Any alternative configuration is required.

    Thanks

    Narendra Dhande

  • #2
    The short answer is...don't run a job from a @Transactional method. Spring Batch handles the transactional semantics for you internally and executing a job from a @Transactional method breaks this. If you want to execute something associated with the job, why not use something like the JobExecutionListener?

    Comment


    • #3
      Good point. I will test it and post the reply.
      Thanks

      Comment

      Working...
      X