HSQLDB Serialization error with parallel JobSteps - java

I'm trying to get a complex flow of jobs done in Spring Batch using a combination of multithreaded Steps and parallel jobs.
Right now I've set up 3 jobs (1, 2, 3), the first (1) of which is running before the others (as expected) and completing withouth issues. The other two (2,3) are supposed to run parallel, having some parallel Steps of their own. All these jobs I'm trying to run are being encapsulated within JobSteps and then run within a master job (0).
The problem only occurs at jobs 2 & 3, where some JobStep fails, not always at the same point, not always the same JobStep. This is the stacktrace of such exception:
2019-01-07 17:35:57,513 ERROR: o.s.b.c.s.AbstractStep [SimpleAsyncTaskExecutor-10] Encountered an error executing step 2 in job0
org.springframework.dao.DataAccessResourceFailureException: Could not increment identity; nested exception is java.sql.SQLTransactionRollbackException: transaction rollback: serialization failure at org.springframework.jdbc.support.incrementer.AbstractIdentityColumnMaxValueIncrementer.getNextKey(AbstractIdentityColumnMaxValueIncrementer.java:113) ~[spring-jdbc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.jdbc.support.incrementer.AbstractDataFieldMaxValueIncrementer.nextLongValue(AbstractDataFieldMaxValueIncrementer.java:128) ~[spring-jdbc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.batch.core.repository.dao.JdbcJobExecutionDao.saveJobExecution(JdbcJobExecutionDao.java:151) ~[spring-batch-core-3.0.8.RELEASE.jar:3.0.8.RELEASE]
at org.springframework.batch.core.repository.support.SimpleJobRepository.createJobExecution(SimpleJobRepository.java:145) ~[spring-batch-core-3.0.8.RELEASE.jar:3.0.8.RELEASE]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_60]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_60]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_60]
at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_60]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) ~[spring-tx-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282) ~[spring-tx-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) ~[spring-tx-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.batch.core.repository.support.AbstractJobRepositoryFactoryBean$1.invoke(AbstractJobRepositoryFactoryBean.java:172) ~[spring-batch-core-3.0.8.RELEASE.jar:3.0.8.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at com.sun.proxy.$Proxy111.createJobExecution(Unknown Source) ~[?:?]
at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:125) ~[spring-batch-core-3.0.8.RELEASE.jar:3.0.8.RELEASE]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_60]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_60]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_60]
at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_60]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$PassthruAdvice.invoke(SimpleBatchConfiguration.java:127) ~[spring-batch-core-3.0.8.RELEASE.jar:3.0.8.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at com.sun.proxy.$Proxy181.run(Unknown Source) ~[?:?]
at org.springframework.batch.core.step.job.JobStep.doExecute(JobStep.java:117) ~[spring-batch-core-3.0.8.RELEASE.jar:3.0.8.RELEASE]
at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:200) [spring-batch-core-3.0.8.RELEASE.jar:3.0.8.RELEASE]
at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148) [spring-batch-core-3.0.8.RELEASE.jar:3.0.8.RELEASE]
at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:64) [spring-batch-core-3.0.8.RELEASE.jar:3.0.8.RELEASE]
at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:67) [spring-batch-core-3.0.8.RELEASE.jar:3.0.8.RELEASE]
at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:169) [spring-batch-core-3.0.8.RELEASE.jar:3.0.8.RELEASE]
at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:144) [spring-batch-core-3.0.8.RELEASE.jar:3.0.8.RELEASE]
at org.springframework.batch.core.job.flow.support.state.SplitState$1.call(SplitState.java:93) [spring-batch-core-3.0.8.RELEASE.jar:3.0.8.RELEASE]
at org.springframework.batch.core.job.flow.support.state.SplitState$1.call(SplitState.java:90) [spring-batch-core-3.0.8.RELEASE.jar:3.0.8.RELEASE]
at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_60]
at org.springframework.core.task.SimpleAsyncTaskExecutor$ConcurrencyThrottlingRunnable.run(SimpleAsyncTaskExecutor.java:271) [spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_60]
Caused by: java.sql.SQLTransactionRollbackException: transaction rollback: serialization failure at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source) ~[hsqldb-2.3.5.jar:2.3.5]
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source) ~[hsqldb-2.3.5.jar:2.3.5]
at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source) ~[hsqldb-2.3.5.jar:2.3.5]
at org.hsqldb.jdbc.JDBCStatement.executeUpdate(Unknown Source) ~[hsqldb-2.3.5.jar:2.3.5]
at org.springframework.jdbc.support.incrementer.AbstractIdentityColumnMaxValueIncrementer.getNextKey(AbstractIdentityColumnMaxValueIncrementer.java:110) ~[spring-jdbc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
... 42 more
Caused by: org.hsqldb.HsqlException: transaction rollback: serialization failure at org.hsqldb.error.Error.error(Unknown Source) ~[hsqldb-2.3.5.jar:2.3.5]
at org.hsqldb.error.Error.error(Unknown Source) ~[hsqldb-2.3.5.jar:2.3.5]
at org.hsqldb.Session.handleAbortTransaction(Unknown Source) ~[hsqldb-2.3.5.jar:2.3.5]
at org.hsqldb.Session.executeCompiledStatement(Unknown Source) ~[hsqldb-2.3.5.jar:2.3.5]
at org.hsqldb.Session.executeDirectStatement(Unknown Source) ~[hsqldb-2.3.5.jar:2.3.5]
at org.hsqldb.Session.execute(Unknown Source) ~[hsqldb-2.3.5.jar:2.3.5]
at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source) ~[hsqldb-2.3.5.jar:2.3.5]
at org.hsqldb.jdbc.JDBCStatement.executeUpdate(Unknown Source) ~[hsqldb-2.3.5.jar:2.3.5]
at org.springframework.jdbc.support.incrementer.AbstractIdentityColumnMaxValueIncrementer.getNextKey(AbstractIdentityColumnMaxValueIncrementer.java:110) ~[spring-jdbc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
... 42 more
I've done a bit of research and this kind of error normally shows up when the HSQLDB I'm using to store job information isn't properly set up for concurrency. However I'm already using the seemingly good config, with MVCC transaction mode:
#Configuration
public class HSqlDbConfig {
#Primary
#Bean("hsqldbDataSource")
public DataSource hsqldbDataSource() {
final SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
dataSource.setDriver(new org.hsqldb.jdbcDriver());
dataSource.setUrl("jdbc:hsqldb:mem:mydb;sql.enforce_strict_size=true;hsqldb.tx=mvcc");
dataSource.setUsername("sa");
dataSource.setPassword("");
return dataSource;
}
}
These are my code fragments I use for the configuration of these jobs. Beginning with the main job (0):
#Bean
public Job job0(JobBuilderFactory jobBuilderFactory) {
getJobParameters();
jobLauncher = (JobLauncher) ctx.getBean("jobLauncher");
Flow job1 = getJob1();
Flow job2 = getJob2();
Flow job3 = getJob3();
Flow splitFlow = getSplitFlow(job2, job3);
return jobBuilderFactory.get("Master Job")
.incrementer(new RunIdIncrementer())
.start(job1)
.next(splitFlow)
.end()
.build();
}
How I get the Flow for jobs 2 & 3:
private Flow getJob2() {
Job j2 = (Job) ctx.getBean("job2");
DefaultJobParametersExtractor extractor = new DefaultJobParametersExtractor();
Step step0 = getJobStep(j2, extractor);
return new FlowBuilder<Flow>("job2")
.start(step0)
.build();
}
private Flow getJob3() {
Job j3 = (Job) ctx.getBean("job3");
Job j3k = (Job) ctx.getBean("job3K");
Job j3l = (Job) ctx.getBean("job3L");
DefaultJobParametersExtractor extractor = new DefaultJobParametersExtractor();
Step step0 = getJobStep(j3, extractor);
Step step1 = getJobStep(j3k, params1);
Step step2 = getJobStep(j3k, params2);
Step step3 = getJobStep(j3l, params3);
Step step4 = getJobStep(j3l, params4);
Flow flow1 = new FlowBuilder<Flow>("flowJ3f1")
.start(step1)
.next(step2)
.build();
Flow flow2 = new FlowBuilder<Flow>("flowJ3f2")
.start(step3)
.next(step4)
.build();
return new FlowBuilder<Flow>("job3")
.start(step0)
.split(taskExecutor)
.add(flow1, flow2)
.build();
}
Both getJobStep() methods:
private Step getJobStep(Job job, JobParametersExtractor extractor) {
return steps.get(job.getName())
.job(job)
.launcher(jobLauncher)
.parametersExtractor(extractor)
.build();
}
private Step getJobStep(Job job, JobParameters jobParameters) {
SimpleJobParametersExtractor extractor = new SimpleJobParametersExtractor();
extractor.setJobParameters(jobParameters);
return steps.get(job.getName())
.job(job)
.launcher(jobLauncher)
.parametersExtractor(extractor)
.build();
}
The idea is to get this structure to work, as parallelization of all task possible is a requirement for this project, and it should be sturdy enough to add other parallel jobs apart from 2 & 3. Also, all jobs and steps have been tested without concurrency and they work as intended.
I can provide more code if needed. Right now I feel in a dead end so every bit of help is appreciated.
EDIT: As #MahmoudBenHassine suggested, I've configured a JobRepository, TransactionManager and JobLauncher (it solved my first issue) like so:
#Bean(name = "myTransactionManager")
public DataSourceTransactionManager transactionManager(#Qualifier("hsqldbDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
#Bean(name = "myJobRepository")
public JobRepository jobRepository(#Qualifier("hsqldbDataSource") DataSource dataSource,
#Qualifier("myTransactionManager") DataSourceTransactionManager dataSourceTransactionManager) throws Exception {
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
factory.setDataSource(dataSource);
factory.setTransactionManager(dataSourceTransactionManager);
factory.setIsolationLevelForCreate("ISOLATION_READ_COMMITTED");
factory.afterPropertiesSet();
return factory.getObject();
}
#Bean(name = "myJobLauncher")
public JobLauncher getJobLauncher(#Qualifier("myJobRepository") JobRepository jobRepository) throws Exception {
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setJobRepository(jobRepository);
jobLauncher.afterPropertiesSet();
return jobLauncher;
}
Then I run job 0 with the new JobLauncher and this exception occurs on the JobSteps from job3 (3K & 3L) EDIT: I've got a larger log extract to more context of what's happening:
2019-01-09 09:53:39,793 INFO: o.s.b.c.j.SimpleStepHandler [MainTaskExecutor11] Executing step: [3K]
2019-01-09 09:53:39,811 INFO: o.s.b.c.l.s.SimpleJobLauncher [MainTaskExecutor11] Job: [FlowJob: [name=3K]] launched with the following parameters: [{process=Job 3K BAR, pos_cod=BAR, per_event=EVENT, isRet=false, UNIQUE=-2347943936040182027}]
2019-01-09 09:53:39,821 INFO: o.s.b.c.j.SimpleStepHandler [MainTaskExecutor11] Executing step: [[3K] Job 3K]
2019-01-09 09:53:39,822 INFO: e.i.l.d.l.StepListener [MainTaskExecutor11] Executing Step: [3K] Job 3K
2019-01-09 09:53:41,120 INFO: e.i.l.d.l.StepListener [MainTaskExecutor11] Write Count: 0
2019-01-09 09:53:41,166 INFO: o.s.b.c.l.s.SimpleJobLauncher [MainTaskExecutor11] Job: [FlowJob: [name=3K]] completed with the following parameters: [{process=Job 3K BAR, pos_cod=BAR, per_event=EVENT, isRet=false, UNIQUE=-2347943936040182027}] and the following status: [COMPLETED]
2019-01-09 09:53:41,189 INFO: o.s.b.c.j.SimpleStepHandler [MainTaskExecutor11] Duplicate step [3K] detected in execution of job=[job 0]. If either step fails, both will be executed again on restart.
2019-01-09 09:53:41,191 INFO: o.s.b.c.j.SimpleStepHandler [MainTaskExecutor11] Executing step: [3K]
2019-01-09 09:53:41,201 ERROR: o.s.b.c.s.AbstractStep [MainTaskExecutor11] Encountered an error executing step 3K in job 0
org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException: A job instance already exists and is complete for parameters={process=Job 3K BAR, pos_cod=BAR, per_event=EVENT, isRet=false, UNIQUE=-2347943936040182027}. If you want to run this job again, change the parameters.
at org.springframework.batch.core.repository.support.SimpleJobRepository.createJobExecution(SimpleJobRepository.java:126) ~[spring-batch-core-3.0.8.RELEASE.jar:3.0.8.RELEASE]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_60]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_60]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_60]
at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_60]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) ~[spring-tx-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282) ~[spring-tx-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) ~[spring-tx-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.batch.core.repository.support.AbstractJobRepositoryFactoryBean$1.invoke(AbstractJobRepositoryFactoryBean.java:172) ~[spring-batch-core-3.0.8.RELEASE.jar:3.0.8.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at com.sun.proxy.$Proxy111.createJobExecution(Unknown Source) ~[?:?]
at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:125) ~[spring-batch-core-3.0.8.RELEASE.jar:3.0.8.RELEASE]
at org.springframework.batch.core.step.job.JobStep.doExecute(JobStep.java:117) ~[spring-batch-core-3.0.8.RELEASE.jar:3.0.8.RELEASE]
at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:200) [spring-batch-core-3.0.8.RELEASE.jar:3.0.8.RELEASE]
at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148) [spring-batch-core-3.0.8.RELEASE.jar:3.0.8.RELEASE]
at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:64) [spring-batch-core-3.0.8.RELEASE.jar:3.0.8.RELEASE]
at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:67) [spring-batch-core-3.0.8.RELEASE.jar:3.0.8.RELEASE]
at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:169) [spring-batch-core-3.0.8.RELEASE.jar:3.0.8.RELEASE]
at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:144) [spring-batch-core-3.0.8.RELEASE.jar:3.0.8.RELEASE]
at org.springframework.batch.core.job.flow.support.state.SplitState$1.call(SplitState.java:93) [spring-batch-core-3.0.8.RELEASE.jar:3.0.8.RELEASE]
at org.springframework.batch.core.job.flow.support.state.SplitState$1.call(SplitState.java:90) [spring-batch-core-3.0.8.RELEASE.jar:3.0.8.RELEASE]
at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_60]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_60]
Please note I use different JobParameters in getJob3() for each execution of job 3K and 3L (the same exception is thrown for that job too). The JobParameters I use are the following: EDIT: I've included a Random function in order to include a unique JobParameter:
Random randomizer = new Random(System.currentTimeMillis());
params1 = new JobParametersBuilder()
.addString(PROCESS, "Job 3K BAR" )
.addString("pos_cod", "BAR")
.addString("per_event", "EVENT")
.addString(IS_RET, FALSE)
.addLong("UNIQUE", randomizer.nextLong())
.toJobParameters();
params2 = new JobParametersBuilder()
.addString(PROCESS, "Job 3K 704" )
.addString("pos_cod", "704")
.addString("per_event", "EVENT")
.addString(IS_RET, FALSE)
.addLong("UNIQUE", randomizer.nextLong())
.toJobParameters();
params3 = new JobParametersBuilder()
.addString(PROCESS, "Job 3L BAR" )
.addString("pos_cod", "BAR")
.addString("per_event", "RET_EVENT")
.addString(IS_RET, FALSE)
.addLong("UNIQUE", randomizer.nextLong())
.toJobParameters();
params4 = new JobParametersBuilder()
.addString(PROCESS, "Job 3L 704" )
.addString("pos_cod", "704")
.addString("per_event", "RET_EVENT")
.addString(IS_RET, FALSE)
.addLong("UNIQUE", randomizer.nextLong())
.toJobParameters();
Having seen the duplicated job instance error keeps occurring as the JobLauncher is trying to launch the samen job (not its sibling with other JobParameters), I'm more inclined to think this is a problem with my JobRepository, but this is nothing more than speculation.

The Map based job repository is not intended for multi-threading. This job repository is by default configured with a ResourcelessTransactionManager.
Make sure to use the JDBC based job repository (even with an in-memory database like in your case) to properly support multi-threading and parallel processing along with a DataSourceTransactionManager.
As a side note, the SimpleDriverDataSource is not a pooled data source, I recommend to use one with a connection pool.

Related

Running transaction on different bean throws exception

I have asked this question and implemented the answer. It worked fine but now that I'm handling more data and it is not working as expected. Explaining:
I'm building integration with JMS. By the end of the integration process I must log the integration no matter what. To do so, I have build a #Service that saves a log in a new transaction. If I use the service, I'm getting an exception. If I execute the same logic of the service in the same bean it works fine. Let me show some code:
The listener
public class JMSListenerIntegration {
#Autowired
private IntegrationHandler integrationHandler;
#Autowired
private LogService logService;
#Autowired
private IntegrationLogRepository logRepository; // extends JpaRepository
#JmsListener(...)
public void onReceive(Message message) {
List<Dto> dtos = // Unmarshall the message
// A log for this integration already exists
// On receiving a message, it changes status from RUNNING to OK or NOK
IntegrationLog log = logRepository.findAlreadyExistingLog();
try {
// The integration handler persist 100+ records, each one in a new transaction
boolean success = integrationHandler.handle(dtos);
if (success)
log.setStatus("OK");
else
log.setStatus("NOK");
} catch (Exception e) {
log.setStatus("NOK");
} finally {
logService.save(log);
// this.saveLog(log); // THIS WORKS
}
}
#Transactional(propagation = Propagation.REQUIRES_NEW)
public void saveLog(IntegrationLog log) {
logRepository.saveAndFlush(log);
}
}
LogService
#Service
public class LogService {
#Autowired
private IntegrationLogRepository logRepository;
#Transactional(propagation = Propagation.REQUIRES_NEW)
public void save(IntegrationLog log) {
logRepository.saveAndFlush(log);
}
}
If I run the code using the saveLog() function defined on my JMSListenerIntegration it works fine. If, instead, I try to use my LogService class it does not work. I see on the log a hibernate query for selecting a log and then, after some good 20s, I get an error:
2019-01-02 20:21:35.649 WARN 21112 --- [ (self-tuning)'] onizationCallbackCoordinatorTrackingImpl : HHH000451: Transaction afterCompletion called by a background thread; delaying afterCompletion processing until the original thread can handle it. [status=4]
2019-01-02 20:21:35.652 WARN 21112 --- [enerContainer-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: null
2019-01-02 20:21:35.652 ERROR 21112 --- [enerContainer-1] o.h.engine.jdbc.spi.SqlExceptionHelper : The transaction is no longer active - status: 'Marked rollback. [Reason=weblogic.transaction.internal.TimedOutException: Transaction timed out after 31 seconds
BEA1-36996D3224E23057A4BD]'. No further JDBC access is allowed within this transaction.
2019-01-02 20:21:35.657 INFO 21112 --- [enerContainer-1] o.h.e.internal.DefaultLoadEventListener : HHH000327: Error performing load command : org.hibernate.exception.GenericJDBCException: could not load an entity: [com.example.models.IntegrationLog#6]
2019-01-02 20:21:35.692 WARN 21112 --- [enerContainer-1] o.s.j.l.DefaultMessageListenerContainer : Execution of JMS message listener failed, and no ErrorHandler has been set.
org.springframework.orm.jpa.JpaSystemException: could not load an entity: [com.example.models.IntegrationLog#6]; nested exception is org.hibernate.exception.GenericJDBCException: could not load an entity: [com.example.models.IntegrationLog#6]
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:333) ~[spring-orm-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:244) ~[spring-orm-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:525) ~[spring-orm-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59) ~[spring-tx-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:209) ~[spring-tx-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147) ~[spring-tx-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133) ~[spring-data-jpa-1.11.14.RELEASE.jar:na]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:57) ~[spring-data-commons-1.13.14.RELEASE.jar:na]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) ~[spring-aop-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at com.sun.proxy.$Proxy400.save(Unknown Source) ~[na:na]
at com.example.services.IntegrationLogService.save(IntegrationLogService.kt:20) ~[_wl_cls_gen.jar:na]
at com.example.services.IntegrationLogService$$FastClassBySpringCGLIB$$98a7fdd9.invoke(<generated>) ~[_wl_cls_gen.jar:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:736) ~[spring-aop-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) ~[spring-tx-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282) ~[spring-tx-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) ~[spring-tx-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:671) ~[spring-aop-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at com.example.services.IntegrationLogService$$EnhancerBySpringCGLIB$$9eee6d68.save(<generated>) ~[_wl_cls_gen.jar:na]
at com.example.integrations.listeners.MalhaJmsListener.onReceive(MalhaJmsListener.kt:88) ~[_wl_cls_gen.jar:na]
at com.example.integrations.listeners.MalhaJmsListener$$FastClassBySpringCGLIB$$1357be2d.invoke(<generated>) ~[_wl_cls_gen.jar:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:667) ~[spring-aop-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at com.example.integrations.listeners.MalhaJmsListener$$EnhancerBySpringCGLIB$$c9de82e4.onReceive(<generated>) ~[_wl_cls_gen.jar:na]
at com.example.integrations.listeners.ListenersManager$startupListeners$1.onMessage(ListenersManager.kt:42) ~[_wl_cls_gen.jar:na]
at org.springframework.jms.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:744) ~[spring-jms-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.jms.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:682) ~[spring-jms-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.jms.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:649) ~[spring-jms-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.doReceiveAndExecute(AbstractPollingMessageListenerContainer.java:317) [spring-jms-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.receiveAndExecute(AbstractPollingMessageListenerContainer.java:235) [spring-jms-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.invokeListener(DefaultMessageListenerContainer.java:1168) [spring-jms-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.executeOngoingLoop(DefaultMessageListenerContainer.java:1160) [spring-jms-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.run(DefaultMessageListenerContainer.java:1057) [spring-jms-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_192]
Caused by: org.hibernate.exception.GenericJDBCException: could not load an entity: [com.example.models.IntegrationLog#6]
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47) ~[hibernate-core-5.1.16.Final.jar:5.1.16.Final]
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111) ~[hibernate-core-5.1.16.Final.jar:5.1.16.Final]
at org.hibernate.loader.Loader.loadEntity(Loader.java:2213) ~[hibernate-core-5.1.16.Final.jar:5.1.16.Final]
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:60) ~[hibernate-core-5.1.16.Final.jar:5.1.16.Final]
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:50) ~[hibernate-core-5.1.16.Final.jar:5.1.16.Final]
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4083) ~[hibernate-core-5.1.16.Final.jar:5.1.16.Final]
at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:508) ~[hibernate-core-5.1.16.Final.jar:5.1.16.Final]
at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:478) ~[hibernate-core-5.1.16.Final.jar:5.1.16.Final]
at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:219) ~[hibernate-core-5.1.16.Final.jar:5.1.16.Final]
at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:278) ~[hibernate-core-5.1.16.Final.jar:5.1.16.Final]
at org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:121) ~[hibernate-core-5.1.16.Final.jar:5.1.16.Final]
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:89) ~[hibernate-core-5.1.16.Final.jar:5.1.16.Final]
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1142) ~[hibernate-core-5.1.16.Final.jar:5.1.16.Final]
at org.hibernate.internal.SessionImpl.access$2600(SessionImpl.java:167) ~[hibernate-core-5.1.16.Final.jar:5.1.16.Final]
at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.doLoad(SessionImpl.java:2762) ~[hibernate-core-5.1.16.Final.jar:5.1.16.Final]
at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.java:2741) ~[hibernate-core-5.1.16.Final.jar:5.1.16.Final]
at org.hibernate.internal.SessionImpl.get(SessionImpl.java:983) ~[hibernate-core-5.1.16.Final.jar:5.1.16.Final]
at org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:289) ~[hibernate-core-5.1.16.Final.jar:5.1.16.Final]
at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:169) ~[hibernate-core-5.1.16.Final.jar:5.1.16.Final]
at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:68) ~[hibernate-core-5.1.16.Final.jar:5.1.16.Final]
at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:843) ~[hibernate-core-5.1.16.Final.jar:5.1.16.Final]
at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:825) ~[hibernate-core-5.1.16.Final.jar:5.1.16.Final]
at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:830) ~[hibernate-core-5.1.16.Final.jar:5.1.16.Final]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:1161) ~[hibernate-entitymanager-5.1.0.Final.jar:5.1.0.Final]
at sun.reflect.GeneratedMethodAccessor252.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_192]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_192]
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:347) ~[spring-orm-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at com.sun.proxy.$Proxy386.merge(Unknown Source) ~[na:na]
at sun.reflect.GeneratedMethodAccessor252.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_192]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_192]
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:300) ~[spring-orm-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at com.sun.proxy.$Proxy386.merge(Unknown Source) ~[na:na]
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:511) ~[spring-data-jpa-1.11.14.RELEASE.jar:na]
at sun.reflect.GeneratedMethodAccessor254.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_192]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_192]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:515) ~[spring-data-commons-1.13.14.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:500) ~[spring-data-commons-1.13.14.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:477) ~[spring-data-commons-1.13.14.RELEASE.jar:na]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:56) ~[spring-data-commons-1.13.14.RELEASE.jar:na]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) ~[spring-tx-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282) ~[spring-tx-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) ~[spring-tx-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.18.RELEASE.jar:4.3.18.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) ~[spring-tx-4.3.18.RELEASE.jar:4.3.18.RELEASE]
... 35 common frames omitted
Caused by: java.sql.SQLException: The transaction is no longer active - status: 'Marked rollback. [Reason=weblogic.transaction.internal.TimedOutException: Transaction timed out after 30 seconds
BEA1-01E60652F53A3057A4BD]'. No further JDBC access is allowed within this transaction.
at weblogic.jdbc.wrapper.JTSConnection.checkIfRolledBack(JTSConnection.java:202) ~[weblogic.server.merged.jar:12.1.3.0.0]
at weblogic.jdbc.wrapper.JTSConnection.checkConnection(JTSConnection.java:220) ~[weblogic.server.merged.jar:12.1.3.0.0]
at weblogic.jdbc.wrapper.ResultSet.preInvocationHandler(ResultSet.java:95) ~[weblogic.server.merged.jar:12.1.3.0.0]
at weblogic.jdbc.wrapper.ResultSet_weblogic_jdbc_sqlserverbase_ddeb.next(Unknown Source) ~[weblogic.server.merged.jar:12.1.3.0.0]
at org.hibernate.loader.Loader.processResultSet(Loader.java:974) ~[hibernate-core-5.1.16.Final.jar:5.1.16.Final]
at org.hibernate.loader.Loader.doQuery(Loader.java:936) ~[hibernate-core-5.1.16.Final.jar:5.1.16.Final]
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:342) ~[hibernate-core-5.1.16.Final.jar:5.1.16.Final]
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:312) ~[hibernate-core-5.1.16.Final.jar:5.1.16.Final]
at org.hibernate.loader.Loader.loadEntity(Loader.java:2209) ~[hibernate-core-5.1.16.Final.jar:5.1.16.Final]
... 81 common frames omitted
Any ideas on why this is happening?
Thanks,
EDIT
Following the comments suggestion I have changed my LogService to look like:
#Service
public class LogService {
#Autowired
private IntegrationLogRepository logRepository;
#Transactional(propagation = Propagation.REQUIRES_NEW)
public void save(IntegrationLog plainLog) {
IntegrationLog log = logRepository.findOne(plainLog.getId());
log.setStatus(plainLog.getStatus());
log.setFoo(plainLog.getFoo());
// ...
logRepository.saveAndFlush(log);
}
}
And I still get the same error. And I start to think it is related to the previous operations done on integrationHandler.handle(dtos). In this method, I persist a lot of records. The logic looks like:
public boolean handle(List<Dto> dtos) {
boolean result = true;
for (Dto dto in dtos) {
try {
Foo foo = new Foo();
foo.setProp1(dto.getProp1());
foo.setProp2(dto.getProp2());
saveFoo(foo);
} catch (Exception e) {
logger.error(e.getMessage());
result = false;
}
}
return result;
}
// Now I know this is not running in its own transaction, right?
#Transactional(propagation = Propagation.REQUIRES_NEW)
public saveFoo(Foo foo) {
fooRepository.save(foo);
}
If I don't call the handle() method (so the persist operations are not performed) the LogService works just fine. I can only imagine the transaction for saving the Foo's are taking too long and the transaction for findOne is gettint timed out (can't find a reason why) giving the could not load an entity (I even tried calling findOne(6) which is a valid ID in my database). Maybe this is not the right way to persist the data...? Really lost here guys. Any help is appreciated :/

Srping/ Hibernate: spring.entity.Downloads cannot be cast to java.lang.Integer

I have recently started trying to learn java and finished a spring/hibernate course in aims to implement a solution to some problems at work in my spare time. Needless to say, I have hit a snag.
I am wondering if anyone can see anything obviously wrong with the code attached below that can cause the following error:
SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/sonya-local] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: com.sonya.spring.entity.Downloads cannot be cast to java.lang.Integer] with root cause
java.lang.ClassCastException: com.sonya.spring.entity.Downloads cannot be cast to java.lang.Integer
at org.hibernate.type.descriptor.java.IntegerTypeDescriptor.unwrap(IntegerTypeDescriptor.java:19)
at org.hibernate.type.descriptor.sql.IntegerTypeDescriptor$1.doBind(IntegerTypeDescriptor.java:46)
at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:74)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:277)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:272)
at org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:53)
at org.hibernate.hql.internal.ast.exec.BasicExecutor.doExecute(BasicExecutor.java:82)
at org.hibernate.hql.internal.ast.exec.BasicExecutor.execute(BasicExecutor.java:59)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.executeUpdate(QueryTranslatorImpl.java:429)
at org.hibernate.engine.query.spi.HQLQueryPlan.performExecuteUpdate(HQLQueryPlan.java:374)
at org.hibernate.internal.SessionImpl.executeUpdate(SessionImpl.java:1495)
at org.hibernate.query.internal.AbstractProducedQuery.doExecuteUpdate(AbstractProducedQuery.java:1507)
at org.hibernate.query.internal.AbstractProducedQuery.executeUpdate(AbstractProducedQuery.java:1485)
at com.sonya.spring.dao.DownloadsDAOImpl.archiveProduct(DownloadsDAOImpl.java:128)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.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:333)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:52)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:52)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy50.archiveProduct(Unknown Source)
at com.sonya.spring.service.DownloadsServiceImpl.archiveProduct(DownloadsServiceImpl.java:65)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
Basically I am looking to move data between tables after a small vetting period (1 hour) I have successfully added timestamps and a few other functions such as search, api calls etc so far.
The end goal of this is to attach a scheduler and have it run periodically throughout the day but for now I have added a url mapping to trigger it manually.
When hitting the URL I receive the above error:
My Code:
Controller code
#GetMapping("/archiveproducts")
public String archiveDownloads (){
List<Downloads> theDownloads = downloadsService.getDownloads();
for (Downloads archiveDownload : theDownloads) {
// display the download ... just for clarity
System.out.println(archiveDownload);
Date timer = new Date();
Date purge = archiveDownload.getTimestamp();
long hours = (timer.getTime() - purge.getTime()) / DateTimeConstants.MILLIS_PER_HOUR;
if (hours >= 1) {
downloadsService.archiveProduct(archiveDownload);
}
}
return "list-downloads";
}
DownloadsDAOimpl Code: (java:128)
public List<Downloads> archiveProduct(Downloads archiveDownload) {
Session currentSession = sessionFactory.getCurrentSession();
Query theCopyQuery =
currentSession.createQuery("insert into Archive(fieldOne, fieldTwo, fieldThree, fieldFour, fieldFive, , fieldSix, FieldSeven)"
+ "sfieldOne, fieldTwo, fieldThree, fieldFour, fieldFive, , fieldSix, FieldSeven from Downloads where id=:theId");
theCopyQuery.setParameter("theId", archiveDownload);
theCopyQuery.executeUpdate();
List<Downloads> archiveProducts = theCopyQuery.getResultList();
Query theDeleteQuery =
currentSession.createQuery("delete from Downloads where id=:theId");
theDeleteQuery.setParameter("theId", archiveDownload);
theDeleteQuery.executeUpdate();
// return the results
return archiveProducts;
}
DownloadsServiceImpl.java:65
#Override
#Transactional
public List<Downloads> archiveProduct(Downloads archiveDownload) {
return downloadsDAO.archiveProduct(archiveDownload);
}
I understand that it's telling me I cannot cast an entity as an integer. but I don't understand where this integer is coming from or how to fix/reslove it.. I have a used a similar code approach for one of the API's and it worked okay.
#PostMapping("/listdownloads")
public List<Downloads> addDownloads (#RequestBody List<Downloads> theDownloads){
for (Downloads tempDownload : theDownloads) {
// display the download ... just for clarity
tempDownload.setId(0);
tempDownload.setTimestamp(null);
System.out.println(tempDownload);
// save to the database
downloadsService.saveProduct(tempDownload);
}
Cheers,
Danny
I think the issue is with this line.
theDeleteQuery.setParameter("theId", archiveDownload);
It is expecting an integer as Id but you passing Downloads object as mentioned in eror message. you can try something like this.
theDeleteQuery.setParameter("theId", archiveDownload.getId());

InvalidDataAccessResourceUsage Exception in spring application test

Here is my model:-
DAO:-
public class User implements Serializable {
private int id;
private String name;
private String userIdentifier;
private String accountNumber;
private String baseURL;
private String proxy;
private int timeout;
private String configuration;
private boolean active;
// getters and setters for above variables
}
I am creating user in the test method and I have mandatory fields for the creation. Still I don't know why it is throwing error when I execute the test.
Test method:-
#RunWith(SpringRunner.class)
#SpringApplicationConfiguration(classes = TestApplication.class)
#WebAppConfiguration
public class UserRepositoryTest extends AbstractTransactionalJUnit4SpringContextTests {
private static final String PATH = "/users/";
#Before
public void setup() throws Exception {
this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
createUser("John");
headers = new HttpHeaders();
headers.add(CONTENT_TYPE, APPLICATION_JSON);
}
private void createUser(String name) {
user = new user();
user.setId(INTEGER_ONE);
user.setName(name);
user.setAccountNumber("1234");
user.setActive(TRUE);
user.setUserIdentifier("test");
user.setTimeout(3000);
user.setBaseUrl("https://localhost:8080");
}
#Test
public void testUser() throws Exception {
mockMvc.perform(post(PATH).content(new ObjectMapper().writeValueAsString(demandSource)).headers(headers))
.andDo(print())
.andExpect(status().isCreated());
}
I am getting below error when I execute the above test in my Spring application. What I am doing wrong here ?
Error:-
nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.ParserDQL.readTableName(Unknown Source)
at org.hsqldb.ParserDQL.readTableOrSubquery(Unknown Source)
at org.hsqldb.ParserDQL.XreadTableReference(Unknown Source)
at org.hsqldb.ParserDQL.XreadFromClause(Unknown Source)
at org.hsqldb.ParserDQL.XreadTableExpression(Unknown Source)
at org.hsqldb.ParserDQL.XreadQuerySpecification(Unknown Source)
at org.hsqldb.ParserDQL.XreadSimpleTable(Unknown Source)
at org.hsqldb.ParserDQL.XreadQueryPrimary(Unknown Source)
at org.hsqldb.ParserDQL.XreadQueryTerm(Unknown Source)
at org.hsqldb.ParserDQL.XreadQueryExpressionBody(Unknown Source)
at org.hsqldb.ParserDQL.XreadQueryExpression(Unknown Source)
at org.hsqldb.ParserDQL.compileCursorSpecification(Unknown Source)
at org.hsqldb.ParserCommand.compilePart(Unknown Source)
at org.hsqldb.ParserCommand.compileStatement(Unknown Source)
at org.hsqldb.Session.compileStatement(Unknown Source)
at org.hsqldb.StatementManager.compile(Unknown Source)
at org.hsqldb.Session.execute(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.<init>(Unknown Source)
at org.hsqldb.jdbc.JDBCConnection.prepareStatement(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tomcat.jdbc.pool.ProxyConnection.invoke(ProxyConnection.java:126)
at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:108)
at org.apache.tomcat.jdbc.pool.DisposableConnectionFacade.invoke(DisposableConnectionFacade.java:81)
at com.sun.proxy.$Proxy112.prepareStatement(Unknown Source)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:146)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:172)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:148)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1929)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1898)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1876)
at org.hibernate.loader.Loader.doQuery(Loader.java:919)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:336)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:306)
at org.hibernate.loader.Loader.loadEntity(Loader.java:2204)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:60)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:50)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3967)
at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:508)
at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:478)
at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:219)
at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:278)
at org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:121)
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:89)
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1129)
at org.hibernate.internal.SessionImpl.access$2600(SessionImpl.java:164)
at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.java:2696)
at org.hibernate.internal.SessionImpl.get(SessionImpl.java:980)
at org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:290)
at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:170)
at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:69)
at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:840)
at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:822)
at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:827)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:1161)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:298)
at com.sun.proxy.$Proxy137.merge(Unknown Source)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:509)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:503)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:488)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:460)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
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.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy163.save(Unknown Source)
application.yml
spring:
datasource:
url: jdbc:hsqldb:/home/user/unittest/hsql
username: sa
password:
driver-class-name: org.hsqldb.jdbcDriver
testOnBorrow: true
validationQuery: SELECT 1
jpa:
hibernate:
ddl-auto: createordrop
properties:
hibernate:
format_sql: true
show_sql: true
endpoints:
health:
sensitive: false
server:
tomcat:
basedir: target/tomcat
accesslog:
enabled: true
pattern: "%v %h %l %u %t "%r" %s %b "%{Referer}i" "%{User-Agent}i" %S %D"
How to solve this error ? Kindly help.
I solved this issue by changing the ddl-auto value from createordrop to create-drop in application.yml. It was my mistake. Thanks all for your time and suggestions.
ddl-auto property values are none, validate, update, create, create-drop. Refer here

Is there anything special about configuring the use of multiple buckets in a spring-data-couchbase Java application?

I'm working on a Java-based spring-data-couchbase application that connects to multiple buckets to do its work. We are seeing TimeoutExceptions being thrown when trying to retrieve documents from the buckets with a stack trace like this:
10-17-2016 14:28:10 (WebContainer : 9) ERROR [com.cars.ss.cp.resources.ProfileResource] - [RESOURCE] [ERROR] Service com.sun.proxy.$Proxy195.getConsumerProfileSummaryVersion throws org.springframework.dao.QueryTimeoutException.
org.springframework.dao.QueryTimeoutException: java.util.concurrent.TimeoutException; nested exception is java.lang.RuntimeException: java.util.concurrent.TimeoutException
at org.springframework.data.couchbase.core.CouchbaseExceptionTranslator.translateExceptionIfPossible(CouchbaseExceptionTranslator.java:122)
at org.springframework.data.couchbase.core.CouchbaseTemplate.execute(CouchbaseTemplate.java:500)
at org.springframework.data.couchbase.core.CouchbaseTemplate.exists(CouchbaseTemplate.java:464)
at org.springframework.data.couchbase.repository.support.SimpleCouchbaseRepository.exists(SimpleCouchbaseRepository.java:110)
at sun.reflect.GeneratedMethodAccessor241.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55)
at java.lang.reflect.Method.invoke(Method.java:613)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:503)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:488)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:460)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.couchbase.repository.support.ViewPostProcessor$ViewInterceptor.invoke(ViewPostProcessor.java:87)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
at com.sun.proxy.$Proxy148.exists(Unknown Source)
at sun.reflect.GeneratedMethodAccessor240.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55)
at java.lang.reflect.Method.invoke(Method.java:613)
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.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
at com.sun.proxy.$Proxy174.exists(Unknown Source)
at com.cars.ss.cp.service.impl.ProfilePhotoService.findByConsumerId(ProfilePhotoService.java:43)
at com.cars.ss.cp.dao.impl.ConsumerAccountDAO.findConsumerAccountByPartyId(ConsumerAccountDAO.java:77)
at sun.reflect.GeneratedMethodAccessor287.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55)
at java.lang.reflect.Method.invoke(Method.java:613)
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.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
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.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
at com.sun.proxy.$Proxy186.findConsumerAccountByPartyId(Unknown Source)
at com.cars.ss.cp.service.impl.ProfileService.getBOConsumerProfileVersion(ProfileService.java:235)
at com.cars.ss.cp.service.impl.ProfileService.getProfileSummaryByPersonPartyId(ProfileService.java:489)
at com.cars.ss.cp.service.impl.ProfileService.getProfileSummaryByConsumerId(ProfileService.java:479)
at sun.reflect.GeneratedMethodAccessor286.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55)
at java.lang.reflect.Method.invoke(Method.java:613)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at com.sun.proxy.$Proxy195.getProfileSummaryByConsumerId(Unknown Source)
at com.cars.ss.cp.resources.ProfileResource.getSummaryReadByConsumerId(ProfileResource.java:362)
at com.cars.ss.cp.resources.ProfileResource.getConsumerProfileSummaryVersion(ProfileResource.java:320)
at com.cars.ss.cp.resources.ProfileResource.getConsumerProfile(ProfileResource.java:252)
at sun.reflect.GeneratedMethodAccessor261.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55)
at java.lang.reflect.Method.invoke(Method.java:613)
at org.apache.wink.server.internal.handlers.InvokeMethodHandler.handleRequest(InvokeMethodHandler.java:63)
...
at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1225)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:775)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:457)
at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:178)
at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1032)
at com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServletWrapper.java:87)
at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:908)
at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1662)
at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:195)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:459)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:526)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:312)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:283)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminators(NewConnectionInitialReadCallback.java:214)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:113)
at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
at com.ibm.io.async.AsyncChannelFuture$1.run(AsyncChannelFuture.java:205)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1815)
Caused by: java.lang.RuntimeException: java.util.concurrent.TimeoutException
at com.couchbase.client.java.util.Blocking.blockForSingle(Blocking.java:75)
at com.couchbase.client.java.CouchbaseBucket.exists(CouchbaseBucket.java:159)
at com.couchbase.client.java.CouchbaseBucket.exists(CouchbaseBucket.java:154)
at org.springframework.data.couchbase.core.CouchbaseTemplate$5.doInBucket(CouchbaseTemplate.java:467)
at org.springframework.data.couchbase.core.CouchbaseTemplate$5.doInBucket(CouchbaseTemplate.java:464)
at org.springframework.data.couchbase.core.CouchbaseTemplate.execute(CouchbaseTemplate.java:497)
... 120 more
Caused by: java.util.concurrent.TimeoutException
... 126 more
I configured my Couchbase configuration class with multiple buckets like this:
#EnableCouchbaseRepositories(basePackages = { "com.cars.ss.cp.nosql.repository" })
#Configuration
public class CouchbaseConfiguration extends AbstractCouchbaseConfiguration {
private static Logger logger = Logger.getLogger(CouchbaseConfiguration.class);
private static final long TIMEOUT_IN_SECONDS = 10;
#Autowired
#Qualifier("propertyService")
private PropertyService propertyService;
#Bean(name = { "defaultBucket" })
public Bucket defaultBucket() throws Exception {
return couchbaseCluster().openBucket(getBucketName());
}
#Override
protected List<String> getBootstrapHosts() {
String rawPropertyValue = this.propertyService.getPropertyValue(COUCHBASE_HOSTS);
if (StringUtils.isNotBlank(rawPropertyValue)) {
StringTokenizer st = new StringTokenizer(rawPropertyValue, ",");
List<String> returnValue = new ArrayList<>(st.countTokens());
while (st.hasMoreTokens()) {
returnValue.add(st.nextToken().trim());
}
return returnValue;
}
logger.error("Unable to process the couchbaseHosts property. Property value: " + rawPropertyValue);
return Collections.emptyList();
}
#Override
protected String getBucketName() {
return this.propertyService.getPropertyValue(COUCHBASE_BUCKET);
}
#Override
protected String getBucketPassword() {
return null;
}
#Override
protected CouchbaseEnvironment getEnvironment() {
return DefaultCouchbaseEnvironment.builder().connectTimeout(TimeUnit.SECONDS.toMillis(TIMEOUT_IN_SECONDS)).kvTimeout(TimeUnit.SECONDS.toMillis(TIMEOUT_IN_SECONDS)).build();
}
#Bean
public Bucket consumerProfileBucket() throws Exception {
return couchbaseCluster().openBucket(this.propertyService.getPropertyValue(CONSUMER_PROFILE_BUCKET), this.propertyService.getPropertyValue(CONSUMER_PROFILE_BUCKET_PASSWORD));
}
#Bean
public CouchbaseTemplate consumerProfileTemplate() throws Exception {
CouchbaseTemplate template = new CouchbaseTemplate(couchbaseClusterInfo(), consumerProfileBucket(), mappingCouchbaseConverter(), translationService());
template.setDefaultConsistency(getDefaultConsistency());
return template;
}
#Override
protected void configureRepositoryOperationsMapping(RepositoryOperationsMapping baseMapping) {
try {
baseMapping.mapEntity(OwnedVehicle.class, consumerProfileTemplate());
baseMapping.mapEntity(ProfilePhoto.class, consumerProfileTemplate());
} catch (Exception e) {
logger.error("Errors encountered during configuration. Exception: " + e);
}
}
}
I originally didn't have the "getEnvironment" method overridden. Based on some recommendations that I saw online, I overrode that method and increased both the connection timeout and key-value timeout to 10 seconds to see if that might help, but it did not. When I increased the timeout from the defaults to 10 seconds, the timeout of the document retrieval became exactly 10 seconds. The TimeoutException is occurring against a document that is trying to be retrieved using the document's key.
The application has the following specifications:
* Java version: 1.7
* spring-data-couchbase version: 2.1.1.RELEASE
* java-client version: 2.2.5
* IBM WebSphere version: 8.5
Is there something obvious that I am missing in the way I have things configured? Are there any other ideas out there of things that I can look at to repair this problem?
10/24/2016 - Added a thread analysis image from the javacore:
thread analysis image from javacore

dao is always null in serviceimpl class

Below is my code. I am using remoting to get reference to the service. once i get the service reference and try to get event.dao call i get a NPE in EventServiceImpl class. When I use xml based config it works but i want it to be annotation based.
EventDao.class
public interface EventDao {
void saveEvent(Event employee);
List<Event> findAllEvents();
void deleteEmployeeBySsn(String ssn);
Event findBySsn(String ssn);
void updateEvent(Event employee);
}
EventImplDao.class
#Repository
public class EventDaoImpl extends AbstractDao implements EventDao{
public void saveEvent(Event employee) {
persist(employee);
}
#SuppressWarnings("unchecked")
public List<Event> findAllEvents() {
Criteria criteria = getSession().createCriteria(Event.class);
return (List<Event>) criteria.list();
}
EventServiceImpl
#Service
#Transactional
public class EventServiceImpl implements EventService{
private EventDao eventDao;
public void setEventDao(EventDao eventDao) {
this.eventDao = eventDao;
}
public void saveEvent(Event employee) {
eventDao.saveEvent(employee);
}
public List<Event> findAllEvents() {
return eventDao.findAllEvents();
}
EventService
#ServiceInterface
public interface EventService {
void saveEvent(Event employee);
List<Event> findAllEvents();
void deleteEmployeeBySsn(String ssn);
Event findBySsn(String ssn);
void updateEvent(Event employee);
}
appconfig
#Configuration
#ComponentScan(basePackages = "com.xxx.spring")
public class AppConfig {
#Bean
public com.xxx.spring.dao.EventDao EventDao() {
return new EventDaoImpl();
}
#Bean
public com.xxxx.spring.service.EventService eventService() {
return new EventServiceImpl();
}
hibernateconfig class
#Configuration
#EnableTransactionManagement
#ComponentScan({ "com.xxxxx.configuration.hib" })
public class HibernateConfiguration {
#Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(new String[] { "com.xxxxxx.spring.component" });
sessionFactory.setHibernateProperties(hibProperties());
return sessionFactory;
}
#Bean
public DataSource dataSource() {
FileUtils ft = new FileUtils();
ft.getProperties();
DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
driverManagerDataSource.setDriverClassName(ft.getProperties().getProperty("driver"));
driverManagerDataSource.setUrl(ft.getProperties().getProperty("url"));
driverManagerDataSource.setUsername(ft.getProperties().getProperty("username"));
driverManagerDataSource.setPassword(ft.getProperties().getProperty("password"));
return driverManagerDataSource;
}
private Properties hibProperties() {
Properties properties = new Properties();
properties.put(Environment.DIALECT, "org.hibernate.dialect.SQLServer2012Dialect");
properties.put(Environment.SHOW_SQL, false);
return properties;
}
#Bean
#Autowired
public HibernateTransactionManager transactionManager(SessionFactory s) {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(s);
return txManager;
}
}
Stacktrace:
HTTP Status 500 - Request processing failed; nested exception is java.lang.NullPointerException
type Exception report
message Request processing failed; nested exception is java.lang.NullPointerException
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:152)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
root cause
java.lang.NullPointerException
com.xxxx.spring.service.Impl.EventServiceImpl.findAllEvents(EventServiceImpl.java:28)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
com.sun.proxy.$Proxy24.findAllEvents(Unknown Source)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
org.springframework.remoting.support.RemoteInvocationTraceInterceptor.invoke(RemoteInvocationTraceInterceptor.java:78)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
com.sun.proxy.$Proxy25.findAllEvents(Unknown Source)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.remoting.support.RemoteInvocation.invoke(RemoteInvocation.java:212)
org.springframework.remoting.support.DefaultRemoteInvocationExecutor.invoke(DefaultRemoteInvocationExecutor.java:39)
org.springframework.remoting.support.RemoteInvocationBasedExporter.invoke(RemoteInvocationBasedExporter.java:78)
org.springframework.remoting.rmi.RmiBasedExporter.invoke(RmiBasedExporter.java:73)
org.springframework.remoting.rmi.RmiInvocationWrapper.invoke(RmiInvocationWrapper.java:74)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
sun.rmi.transport.Transport$1.run(Unknown Source)
sun.rmi.transport.Transport$1.run(Unknown Source)
java.security.AccessController.doPrivileged(Native Method)
sun.rmi.transport.Transport.serviceCall(Unknown Source)
sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source)
sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$256(Unknown Source)
java.security.AccessController.doPrivileged(Native Method)
sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.lang.Thread.run(Unknown Source)
org.springframework.remoting.support.RemoteInvocationUtils.fillInClientStackTraceIfPossible(RemoteInvocationUtils.java:45)
org.springframework.remoting.rmi.RmiClientInterceptor.doInvoke(RmiClientInterceptor.java:353)
org.springframework.remoting.rmi.RmiClientInterceptor.invoke(RmiClientInterceptor.java:260)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
com.sun.proxy.$Proxy30.findAllEvents(Unknown Source)
com.xxxx.controller.WebController.initiateEventsPage(WebController.java:35)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:152)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.57 logs.
new error now. I think caused becaue eventDaoIml has #Repository attached to it.
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.xxxxx.spring.dao.EventDao com.xxx.spring.service.Impl.EventServiceImpl.eventDao; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.xxxi.spring.dao.EventDao] is defined: expected single matching bean but found 2: eventDaoImpl,EventDao
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
... 26 more
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [comxxx.i.spring.dao.EventDao] is defined: expected single matching bean but found 2: eventDaoImpl,EventDao
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:970)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
... 28 more
Try to use annotation in your daowith #Autowired
#Autowired
private EventDao eventDao;

Categories