1

Launching same spring batch multiple times in parallel threads produces this error:

.spring batch Duplicate entry 'xxxxxxx' for key 'JOB_INST_UN'

Here is how the jobs are called:

JobParameters jobParameters = new JobParametersBuilder() .addLong("time",System.currentTimeMillis()).toJobParameters(); 
JobLauncher jobLauncher = (JobLauncher) ApplicationContextUtils.getApplicationContext().getBean("jobLauncher"); 
Job job = (Job) ApplicationContextUtils.getApplicationContext().getBean("writeTeamsToDB"); 
try { 
    JobExecution execution = jobLauncher.run(job, jobParameters); 
    System.out.println("Exit Status : " + execution.getStatus());
    logger.info("thread completed.."); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

I get below error:

2016-06-01 11:15:17,385 DEBUG [org.springframework.batch.support.transaction.ResourcelessTransactionManager] - <Rolling back resourceless transaction on [org.springframework.batch.support.transaction.ResourcelessTransactionManager$ResourcelessTransaction@2846ce4f]>
org.springframework.dao.DuplicateKeyException: PreparedStatementCallback; SQL [INSERT into BATCH_JOB_INSTANCE(JOB_INSTANCE_ID, JOB_NAME, JOB_KEY, VERSION) values (?, ?, ?, ?)]; 
    Duplicate entry 'writeTeamsToDB-38eb86e9d3cf1b87034c74e54d25a658' for key 'JOB_INST_UN'; 
    nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'writeTeamsToDB-38eb86e9d3cf1b87034c74e54d25a658' for key 'JOB_INST_UN'
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:239)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:645)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:866)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:927)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:932)
at org.springframework.batch.core.repository.dao.JdbcJobInstanceDao.createJobInstance(JdbcJobInstanceDao.java:115)
at org.springframework.batch.core.repository.support.SimpleJobRepository.createJobExecution(SimpleJobRepository.java:135)
at sun.reflect.GeneratedMethodAccessor11.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.batch.core.repository.support.AbstractJobRepositoryFactoryBean$1.invoke(AbstractJobRepositoryFactoryBean.java:172)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
at com.sun.proxy.$Proxy6.createJobExecution(Unknown Source)
at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:125)
at com.ritsasoft.fantwiz.helper.FantwizWriteTeamsHelper.run(FantwizWriteTeamsHelper.java:39)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
2016-06-01 11:15:17,388 INFO [com.ritsasoft.fantwiz.helper.FantwizWriteTeamsHelper] - <launching prediction writer.....>
2016-06-01 11:15:17,388 INFO [com.ritsasoft.fantwiz.helper.FantwizWriteTeamsHelper] - <thread for new job started..>
2016-06-01 11:15:17,388 DEBUG [org.springframework.batch.support.transaction.ResourcelessTransactionManager] - <Creating new transaction with name [org.springframework.batch.core.repository.support.SimpleJobRepository.getLastJobExecution]: PROPAGATION_REQUIRES_NEW,ISOLATION_REPEATABLE_READ>
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'writeTeamsToDB-38eb86e9d3cf1b87034c74e54d25a658' for key 'JOB_INST_UN'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1015)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1734)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2019)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1937)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1922)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:102)
at org.springframework.jdbc.core.JdbcTemplate$2.doInPreparedStatement(JdbcTemplate.java:873)
at org.springframework.jdbc.core.JdbcTemplate$2.doInPreparedStatement(JdbcTemplate.java:866)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:629)
... 24 more

What do you think I could improve / check to make it work? Is it possible for me to define the unique key myself to avoid it?

Son of the Wai-Pan
  • 12,371
  • 16
  • 46
  • 55
  • What could we do to help you? Have you read http://stackoverflow.com/help/how-to-ask ? Try to build an [MCVE](http://stackoverflow.com/help/mcve). – J. Chomel Jun 01 '16 at 06:36
  • can I please get some help on this. thanks. – jkakumanu09 Jun 01 '16 at 06:57
  • thanks for reply J.Chomel. yes , i have searched on google but didn't find any solution. i use the below to launch my job, – jkakumanu09 Jun 01 '16 at 07:39
  • JobParameters jobParameters = new JobParametersBuilder() .addLong("time",System.currentTimeMillis()).toJobParameters(); JobLauncher jobLauncher = (JobLauncher) ApplicationContextUtils.getApplicationContext().getBean("jobLauncher"); Job job = (Job) ApplicationContextUtils.getApplicationContext().getBean("writeTeamsToDB"); try { JobExecution execution = jobLauncher.run(job, jobParameters); System.out.println("Exit Status : " + execution.getStatus()); logger.info("thread completed.."); } catch (Exception e) { e.printStackTrace(); } – jkakumanu09 Jun 01 '16 at 07:50
  • J.Chomel, i just added my code as a comment. I am not able to edit the question to post the code, as it is saying that the question has lot of code. sorry for that. – jkakumanu09 Jun 01 '16 at 07:52
  • J.chomel, thanks for editing my question. I will make sure I follow this format next time. Thank you. – jkakumanu09 Jun 01 '16 at 15:26
  • can I please get some help on this issue. Thanks. – jkakumanu09 Jun 01 '16 at 15:26
  • can I please get some help on this issue. Thanks. – jkakumanu09 Jul 01 '16 at 06:56

1 Answers1

-1

Well, since you seem a little lost with below error,

..DuplicateKeyException: PreparedStatementCallback; SQL [INSERT into BATCH_JOB_INSTANCE(JOB_INSTANCE_ID, JOB_NAME, JOB_KEY, VERSION) values (?, ?, ?, ?)];

I would suggest you simply remove the PK JOB_INST_UN from BATCH_JOB_INSTANCE.

Otherwise, the best person to help you might be you know... Because your question isn't accurate enough. You tell us nothing about what you tried to fix it.

Have you read or tries anything of the following?

... Mentioning what you tried should gather attention to your question via links.

Community
  • 1
  • 1
J. Chomel
  • 8,193
  • 15
  • 41
  • 69