Home > Back-end >  Spring batch using mysql SQLSyntaxErrorException table batchJOB_INSTANCE does not exist and property
Spring batch using mysql SQLSyntaxErrorException table batchJOB_INSTANCE does not exist and property

Time:06-22

I'm using mysql 8.0 and spring batch 4.3.5with spring boot 2.x. I'm getting this exception while running my batch. exception is java.sql.SQLSyntaxErrorException: Table 'dsm-batch.batchJOB_INSTANCE' doesn't exist
This is full stack trace.

2022-06-22T04:35:16,377 DEBUG [schedulerPool-1] o.s.j.c.JdbcTemplate:Executing prepared SQL statement [SELECT JOB_INSTANCE_ID, JOB_NAME from batchJOB_INSTANCE where JOB_NAME = ? and JOB_KEY = ?]
2022-06-22T04:35:16,446 ERROR [schedulerPool-1] o.s.a.i.SimpleAsyncUncaughtExceptionHandler:Unexpected exception occurred invoking async method: public void com.tecnotree.dsm.scheduler.Scheduler.runJob() throws org.springframework.batch.core.repository.JobExecutionAlreadyRunningException,org.springframework.batch.core.repository.JobRestartException,org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException,org.springframework.batch.core.JobParametersInvalidException
org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [SELECT JOB_INSTANCE_ID, JOB_NAME from batchJOB_INSTANCE where JOB_NAME = ? and JOB_KEY = ?]; nested exception is java.sql.SQLSyntaxErrorException: Table 'dsm-batch.batchJOB_INSTANCE' doesn't exist
    at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:239)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:70)
    at org.springframework.jdbc.core.JdbcTemplate.translateException(JdbcTemplate.java:1541)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:667)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:713)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:744)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:757)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:815)
    at org.springframework.batch.core.repository.dao.JdbcJobInstanceDao.getJobInstance(JdbcJobInstanceDao.java:151)
    at org.springframework.batch.core.repository.support.SimpleJobRepository.getLastJobExecution(SimpleJobRepository.java:269)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:388)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215)
    at com.sun.proxy.$Proxy132.getLastJobExecution(Unknown Source)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:104)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$PassthruAdvice.invoke(SimpleBatchConfiguration.java:128)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215)
    at com.sun.proxy.$Proxy95.run(Unknown Source)
    at com.tecnotree.dsm.scheduler.Scheduler.runJob(Scheduler.java:36)
    at com.tecnotree.dsm.scheduler.Scheduler$$FastClassBySpringCGLIB$$cf5bb24e.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:793)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763)
    at org.springframework.aop.interceptor.AsyncExecutionInterceptor.lambda$invoke$0(AsyncExecutionInterceptor.java:115)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.sql.SQLSyntaxErrorException: Table 'dsm-batch.batchJOB_INSTANCE' doesn't exist
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953)
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeQuery(ClientPreparedStatement.java:1009)
    at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeQuery(ProxyPreparedStatement.java:52)
    at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeQuery(HikariProxyPreparedStatement.java)
    at org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:722)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:651)
    ... 42 more
2022-06-22T04:40:16,353 DEBUG [schedulerPool-2] c.t.d.s.Scheduler:running job... start time is:2022-06-22T04:40:16.353233200

I searched in the spring batch core jar schema file for mysql and this table really does not exist in that file. This is my configs.

spring.datasource.url=jdbc:mysql://localhost:3306/dsm-batch?useUnicode=yes
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.platform=mysql
spring.batch.schema=classpath:/org/springframework/batch/core/schema-mysql.sql
spring.batch.jdbc.table-prefix=batch
spring.batch.jdbc.initialize-schema = ALWAYS

any help would be appreciated.

CodePudding user response:

According to the error message: Table 'dsm-batch.batchJOB_INSTANCE' doesn't exist, the table prefix is dsm-batch, but it is set to a different value in the property spring.batch.jdbc.table-prefix=batch.

You should update this property accordingly:

spring.batch.jdbc.table-prefix=dsm-batch

CodePudding user response:

I put spring.batch.jdbc.table-prefix=batch in my properties file. When spring batch creates it's tables, it added batch_tablename E.G, BATCH_JOB_EXECUTION_CONTEXT. but when it queries, searches for batchtable without _ character. E.G, batchjob_execution_context. removing spring.batch.jdbc.table-prefix=batch solved my problem.

  • Related