In a recent project, when deploying Web Engineering with Jenkins Hot, it was found that statically held threads in the project (storing the Scheduledexecutorservice timer task in a static map) resulted in a database access transaction closing exception, as follows: Org.springframework.transaction.CannotCreateTransactionException:Could not open JPA Entitymanager for transaction; Nested exception is java.lang.IllegalStateException:EntityManagerFactory is closed
Seeing this problem, the class that has been considered to be manipulating the database does not have a transaction declaration, so add @transactional to the class, but it does not take effect. And the program does not periodically call the Runnable class in the project, and then report the above error. Obviously there is only one thread, but there are multiple threads running, guess should be before the hot deployment, the timing thread stored in the static resource is not purged. Reboot Tomcat and find no error. Therefore, the location is Tomcat hot deployment, static resources for release.
Conjecture: The legacy thread is not released, is still running, the operation of the database, the exception that created the transaction failed, should be after the Web Engineering hot deployment, Spring's transaction management is invalidated, unable to create the transaction.
Workaround :
(1) Use annotations in Java EE5 @predestroy
(2) Use Disposablebean in spring or configure Destroy-method. (class implements the Disposablebean interface to implement resource deallocation in the Destroy () method)
Reference:
Deep understanding of Spring transaction Principles
A detailed description of spring transaction management
Thorough mastery of the use of @transactional in Spring
Jenkins Tomcat hot deployment, Task thread recurring boot workaround
Annotations @postconstruct and @predestroy and examples
[email protected] and @predestroy instances
Spring Bean Initializingbean and Disposablebean instances
Tomcat hot deployment, Web project thread not terminated