Tomcat hot deployment, the Web project thread is not terminated, tomcatweb
In a recent project, when you use jenkins to deploy a web project, you will find the static threads in the project (storing ScheduledExecutorService scheduled tasks in static Map ), an exception occurs when the database access transaction is closed from time to time, as shown in the following code: org. springframework. transaction. cannotCreateTransactionException: cocould not open JPA EntityManager for transaction; nested exception is java. lang. illegalStateException: EntityManagerFactory is closed
Seeing this problem, I always think that the class that operates the database has no transaction declaration, So adding @ Transactional to the class does not take effect. In addition, the program calls the Runnable class in the project from time to time, and then reports the preceding error. It is clear that there is only one thread, but multiple threads run. It is assumed that the scheduled threads stored in static resources were not cleared during hot deployment. Restart Tomcat and no error will be reported. Therefore, it is positioned as Tomcat hot deployment, and static resources are released.
Conjecture: The Legacy thread has not been released and is still running. In this case, an exception occurs when a transaction fails to be created when the web project is hot deployed. The Spring transaction management has expired, A transaction cannot be created.
Solution:
(1) Use the annotation @ PreDestroy in Java EE5
(2) Use DisposableBean in Spring or configure destroy-method. (Class implements the DisposableBean interface and releases resources in the destroy () method)
Refer:
Deep understanding of Spring transaction principles
Spring transaction management
Thoroughly understand the use of @ transactional in Spring
Solution for hot deployment of jenkins tomcat and repeated startup of task threads
Annotation @ PostConstruct and @ PreDestroy details and examples
Spring @ PostConstruct and @ PreDestroy instances
Spring Bean InitializingBean and DisposableBean instances