Springboot Hikari Centos 首次连数据库很慢

前言:

springboot项目默认使用了hikari作为数据库连接池,在开发机器上一切正常.
但是把程序部署到Centos7 x64上之后发现,每次首页登录的时候都要停顿几十秒.
于是,在程序启动后, 迅速执行ps -ef|grep java获取到程序的pid,然后在首页点击了几下登录, 再然后执行jstack <pid> > 123.txt获取程序堆栈信息.
以下只是一部分信息,从以下信息中可以看到好几个线程都是阻塞状态, 都在等待锁, 而锁住的线程指向了SecureRandom

"Thread-8" #36 prio=5 os_prio=0 tid=0x00007fab159e5800 nid=0x4b2b runnable [0x00007faad91cb000]
   java.lang.Thread.State: RUNNABLE
	at java.io.FileInputStream.readBytes(Native Method)
	at java.io.FileInputStream.read(FileInputStream.java:255)
	at sun.security.provider.SeedGenerator$URLSeedGenerator.getSeedBytes(SeedGenerator.java:539)
	at sun.security.provider.SeedGenerator.generateSeed(SeedGenerator.java:144)
	at sun.security.provider.SecureRandom$SeederHolder.<clinit>(SecureRandom.java:203)
	at sun.security.provider.SecureRandom.engineNextBytes(SecureRandom.java:221)
	"- locked <0x00000000d82b9b80> (a sun.security.provider.SecureRandom) 问题所在"
	at java.security.SecureRandom.nextBytes(SecureRandom.java:468)
	at oracle.security.o5logon.O5Logon.a(Unknown Source)
	at oracle.security.o5logon.O5Logon.<clinit>(Unknown Source)
	at oracle.jdbc.driver.T4CTTIoauthenticate.<init>(T4CTTIoauthenticate.java:582)
	at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:401)
	at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:553)
	at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:254)
	at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
	at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:528)
	at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:136)
	at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:369)
	at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:198)
	at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:467)
	at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:541)
	at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115)
	at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112)
	"- locked <0x00000000eeb7c898> (a com.zaxxer.hikari.HikariDataSource) "
	at org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:157)


"http-nio-8080-exec-1" #23 daemon prio=5 os_prio=0 tid=0x00007fab1593b000 nid=0x4b1f waiting for monitor entry [0x00007faaf4313000]
   java.lang.Thread.State: BLOCKED (on object monitor)
	at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:107)
	"- waiting to lock <0x00000000eeb7c898> (a com.zaxxer.hikari.HikariDataSource) "
	at org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:157)

"http-nio-8080-exec-2" #24 daemon prio=5 os_prio=0 tid=0x00007fab15995800 nid=0x4b20 waiting for monitor entry [0x00007faaf4212000]
   java.lang.Thread.State: BLOCKED (on object monitor)
	at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:107)
	"- waiting to lock <0x00000000eeb7c898> (a com.zaxxer.hikari.HikariDataSource)"
	at org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:157)

从以上可以看到,问题出在SecureRandom处, 网上查询后, 发现很多Java程序在Linux下都需要把获取随机数的方式从 /dev/random改为/dev/urandom .

参考https://blog.csdn.net/weixin_40581980/article/details/81630501后, 我在启动脚本中加入了参数 java -Djava.security.egd=file:/dev/urandom -jar my.jar, 重启后,发现问题解决.

posted @ 2019-12-12 16:34  火光闪耀  阅读(3055)  评论(0编辑  收藏  举报