问题描述
在Tomcat 8中设置数据库连接时,由于某种原因,Tomcat没有遵循我在context.xml中配置的内容,导致我的连接用完,导致应用程序服务器端出现资源争用(BLOCKED) /等待线程)。池初始化后,我总是有8个连接(在mariadb / mysql中显示进程列表)。我的配置规定最少10个连接,最多100个连接。
When setting up the database connections in Tomcat 8, for some reason Tomcat is not following what I configured in the context.xml, with as result that I run out of connections, leading to resource contentions at the application server side (BLOCKED/WAITING threads). I always have 8 connections (show processlist in mariadb/mysql) after the pool initializes. My configuration states a minimum of 10 connections and a maximum of 100 connections.
我测试了不同的配置,但这根本没有任何区别,这至少是奇怪的。使用context.xml,否则根本无法连接到数据库。
I tested different configurations, but this does not make any difference at all, which is strange at least. The context.xml is used else it would not be able to connect to the database at all.
这里发生了什么?为什么只有8个连接?
What is happening here? Why only 8 connections?
软件版本:
- MySQL JDBC驱动程序:最新版本(5.1.35)
- Java 1.8.0_05
Software versions:- MySQL JDBC driver: Latest (5.1.35)- Java 1.8.0_05
我还观察到我以前的设置:Tomcat 7,Java 1.7,较旧的MySQL JDBC驱动程序,MySQL而不是MariaDB。所以问题似乎与版本没有直接关系。
I also observed with with my previous setup: Tomcat 7, Java 1.7, older MySQL JDBC drivers, MySQL instead of MariaDB. So the problem does not seem directly version related.
显示进程列表输出(显示8个进程):
Show processlist output (Showing the 8 processes):
| Id | User | Host | db | Command | Time | State | Info | Progress |
+-------+---------------+---------------+-------------------+---------+------+-----------+-----------------+----------+
| 71153 | root | localhost | vnitdatacollector | Query | 0 | init | show processlist| 0.000 |
| 73473 | vnit_datacoll | virt005:58585 | vnitdatacollector | Sleep | 0 | | NULL | 0.000 |
| 73474 | vnit_datacoll | virt005:58586 | vnitdatacollector | Sleep | 0 | | NULL | 0.000 |
| 73482 | vnit_datacoll | virt005:58606 | vnitdatacollector | Query | 0 | update | INSERT INTO ... | 0.000 |
| 73483 | vnit_datacoll | virt005:58607 | vnitdatacollector | Sleep | 0 | | NULL | 0.000 |
| 73485 | vnit_datacoll | virt005:58618 | vnitdatacollector | Query | 0 | query end | INSERT INTO ... | 0.000 |
| 73487 | vnit_datacoll | virt005:58624 | vnitdatacollector | Sleep | 0 | | NULL | 0.000 |
| 73488 | vnit_datacoll | virt005:58634 | vnitdatacollector | Sleep | 0 | | NULL | 0.000 |
| 73489 | vnit_datacoll | virt005:58637 | vnitdatacollector | Query | 7 | update | INSERT INTO ... | 0.000 |
+-------+---------------+---------------+-------------------+---------+------+-----------+-----------------+----------+
我有以下context.xml配置:
I have the following context.xml configuration:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource
name="jdbc/dbaccess"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="100"
minIdle="10"
maxWait="1000"
initialSize="10"
minEvictableIdleTimeMillis="5000"
testOnBorrow="true"
validationQuery="SELECT 1"
timeBetweenEvictionRunsMillis="5000"
testWhileIdle="true"
removeAbandoned="true"
removeAbandonedTimeout="60"
logAbandoned="true"
username="some_user"
password="{the password}"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://dbserver:3306/dbaccess?useFastDateParsing=false&jdbcCompliantTruncation=false"
/>
<Resource
name="mail/emailconnection"
auth="Container"
type="javax.mail.Session"
mail.smtp.host="some.stmp.server"
/>
</Context>
以下线程问题会在(短)期后出现:
The following thread issue then shows up after a (short) period:
"Thread-495" #517 daemon prio=5 os_prio=0 tid=0x00007f678c040800 nid=0x1642 waiting on condition [0x00007f67848f4000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000000f21933f0> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
at org.apache.tomcat.dbcp.pool2.impl.LinkedBlockingDeque.takeFirst(LinkedBlockingDeque.java:582)
at org.apache.tomcat.dbcp.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:439)
at org.apache.tomcat.dbcp.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:360)
at org.apache.tomcat.dbcp.dbcp2.PoolingDataSource.getConnection(PoolingDataSource.java:118)
at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1412)
at com.hipersonik.util.ServiceLocator.getConnection(ServiceLocator.java:32)
推荐答案
来自:
事实证明,DBCP包只是忽略了一系列设置。将以下行添加到 context.xml
资源配置中,可以在数据库中获得更好的响应:
It turns out that DBCP package just ignores a series of settings. Adding the following line to the context.xml
resource configuration, gets better response in the database:
<Resource
....
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
....
/>
在mysql中显示进程列表,然后立即显示所需的行为。
Show processlist in mysql then immediately shows the desired behaviour.
这篇关于为什么我的Tomcat只打开8个JDBC连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!