即使server.tomcat.max-threads = 5,Springboot也会增加线程 [英] Springboot increasing threads even if server.tomcat.max-threads=5

查看:2151
本文介绍了即使server.tomcat.max-threads = 5,Springboot也会增加线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有一个springboot应用程序,即使服务器空闲,线程也继续增加.随着时间的流逝,这将导致机器崩溃.导致很高的内存和CPU使用率.

I have a springboot application right now that the threads keeps on increasing even if the server is idle. This causes the machine to crash over time. Causing very high memory and CPU usage.

我尝试设置server.tomcat.max-threads = 5,但是我很不幸,线程不断增加.

I tried setting server.tomcat.max-threads=5 but I am very unlucky and the threads keeps on rising.

顺便说一下,这些是我的组件: -休息模板 -确定HTTP -光明CP -Springboot和Spring框架(@RestController和@Service) -HttpInterceptor(请求和传出) -休眠/JPA

By the way, these are my components: - Rest Template - OK HTTP - Hikari CP - Springboot and Spring Framework (@RestController and @Service) - HttpInterceptor (Request and Outgoing) - Hibernate / JPA

我还在使用htop和我创建的JNI计数线程. JNI PID/TID可以获取htop的轻量级进程ID.因此,此JNI可以虚拟地映射LWPID和Java线程.

I am also counting the threads using htop and the JNI I have created. The JNI PID/TID can get htop's light-weight-process IDs. So this JNI can virtually map that LWPID and the java thread.

任何帮助将不胜感激.

谢谢,
阿尔塔尼斯·泽拉图尔(Artanis Zeratul)

Thanks,
Artanis Zeratul

推荐答案

我的问题现在已经解决.
这些是我观察到的事情,也是造成问题的根本原因.

首先,我为我进行的每个查询创建了Hikari线程池和本地实体管理器工厂Bean,这些查询在应用程序的生命周期中应该只执行一次,或者对于每个数据源连接只执行一次.这是线程不断产生的主要原因.线程池/LocalEntityManagerFactoryBean的创建与此代码段有关:

My problem solved now.
These were the things I have observed and were the root causes of the problem.

Firstly, I have created Hikari Thread Pool and Local Entity Manager Factory Bean for every query I made which should only be done once in the application's lifetime or once for every datasource connection. This was the major cause of threads keeps spawning up. Creation of Thread Pool/LocalEntityManagerFactoryBean pertains to this code snippet:

HikariDataSource connectionPoolDatasource = new HikariDataSource(connectionPoolConfig);

localContainerEntityManagerFactoryBean.setDataSource(connectionPoolDatasource);
localContainerEntityManagerFactoryBean.setJpaProperties(jpaProperties);
localContainerEntityManagerFactoryBean.afterPropertiesSet();

参考: https://groups.google.com /forum/#!topic/hikari-cp/IsVEn-D6oFA
参见Brett Wooldridge对钟仁明的回应.

最后,每次查询或连接到数据库时,我都没有调用Connection.close().
这也导致线程没有被终止或关闭.
以及保持数据库连接打开.
参考:在纯JPA设置中获取数据库连接

Reference: https://groups.google.com/forum/#!topic/hikari-cp/IsVEn-D6oFA
See Brett Wooldridge response to Jen-Ming Chung.

Lastly, I didn't call Connection.close() everytime I have made query or connection to the DB.
Which also leads to threads not being terminated or closed.
As well as leaving DB connections open.
Reference: Getting Database connection in pure JPA setup



干杯!



Cheers!

这篇关于即使server.tomcat.max-threads = 5,Springboot也会增加线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆