Could not open JPA EntityManager for transaction; nested exception is java.lang.IllegalStateException
The error comes from JpaTransactionManager line 403:
TransactionSynchronizationManager.bindResource(getDataSource(), conHolder);
The error means that the transaction manager is trying to bind the datasource (not the entity manager) to the thread, but the datasource is already there and this is unexpected.
Note that the transaction manager had not started yet to bind the Entity Manager to the thread, that would happen next at JpaTransactionManager line 416:
There are two possible explanations:
Somebody (another transaction manager?) is adding the datasource to the thread before the transaction manager and this is unexpected.
Or no one is adding the datasource to the transaction manager, is just that at the end of the task execution no one cleans the thread before returning it to the pool, maybe due an error or an unhandled exception.
One question, does this also happen for only one execution thread, or only when there are several?
To find out what the problem is, these are some steps:
run with a minimal number of threads that cause the problem
put a breakpoint in
TransactionSynchronizationManager.bindResource()
to see who adds the connection to the thread. The breakpoint can be a conditional breakpoint with a condition on the thread name: "jobLauncherTaskExecutor-1".equals(Thread.currentThread().getName())put also a breakpoint in
TransactionSynchronizationManager.unbindResource()
, to see if the datasource is unbound from the thread. when the breakpoints hit, scroll down the stacktrace and see which classes are causing this.
Could not open JPA EntityManager for transaction in spring
I had the same problem. The solution is to use default-autowire="byName" instead of "byType" in your xml configuration. If you have "byType" Spring automatically autowire also the property "jtaDataSource" in the LocalContainerEntityManagerFactoryBean with your definied datasource bean.
Related Topics
In Firebase After Uploading Image How to Get Url
Get Current Week Start and End Date in Java - (Monday to Sunday)
How to Convert Date Which I Got from Firebase Server as Am Getting Error Date
How to Put a Scanner Input into an Array... for Example a Couple of Numbers
How to Replace a Placeholder in a String With a Simpledateformat Pattern
How to Mock Classes With Constructor Injection
How to View Apache Parquet File in Windows
Jpa: Update Only Specific Fields
Spring Boot: Cannot Access Rest Controller on Localhost (404)
How to Configure Hikaricp in My Spring Boot App in My Application.Properties Files
What Is the Most Elegant Way to Check If All Values in a Boolean Array Are True
Wait Until All Threads Finish Their Work in Java
Spring Rest - Create Zip File and Send It to the Client
Spring Rest Post Json Requestbody Content Type Not Supported