上下文数据源设置不会被拾取
Context datasource settings don't get picked up
目标
我正在创建一个 REST web 服务(使用 RESTEasy),我认为如果我不管理连接池会很好,但我让 Tomcat 这样做。所以我在 META-INF/context.xml
中添加了一个资源
问题
我看到在 MySQL 服务器上建立了很多连接,但我已经配置了总共只允许 2 个连接。我不知道为什么,但我的设置没有获取有关连接数等的信息。
Context.xml
<Context>
<!-- DataSource management from tomcat (reference created in web.xml) -->
<Resource name="jdbc/Taalmaatjes-db" auth="Container" type="javax.sql.DataSource"
maxTotal="2" maxIdle="2"
initialSize="1" maxWaitMillis="10000"
username="root" password="rootie"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/taalmaatjes"
removeAbandonedOnBorrow="true"
removeAbandonedOnMaintenance="true"
removeAbandonedTimeout="5"
singleton="false" defaultAutoCommit="false" rollbackOnReturn="true" />
</Context>
设置 maxTotal、maxIdle、initialSize、maxWaitMillis 似乎没有被拾取。使用 commando 'SHOW PROCESSLIST' 并连接到我的应用程序 39 次,我得到:
3 root localhost:56576 taalmaatjes Query 0 starting /* ApplicationName=IntelliJ IDEA 2017.3.4 */ SHOW PROCESSLIST
231 root localhost:57932 taalmaatjes Sleep 102
232 root localhost:57933 taalmaatjes Sleep 102
233 root localhost:57934 taalmaatjes Sleep 100
234 root localhost:57935 taalmaatjes Sleep 99
235 root localhost:57936 taalmaatjes Sleep 99
236 root localhost:57937 taalmaatjes Sleep 99
237 root localhost:57938 taalmaatjes Sleep 99
238 root localhost:57939 taalmaatjes Sleep 98
239 root localhost:57940 taalmaatjes Sleep 98
240 root localhost:57941 taalmaatjes Sleep 98
241 root localhost:57942 taalmaatjes Sleep 98
242 root localhost:57943 taalmaatjes Sleep 98
243 root localhost:57944 taalmaatjes Sleep 98
244 root localhost:57945 taalmaatjes Sleep 97
245 root localhost:57950 taalmaatjes Sleep 93
246 root localhost:57951 taalmaatjes Sleep 93
247 root localhost:57952 taalmaatjes Sleep 93
248 root localhost:57953 taalmaatjes Sleep 92
249 root localhost:57954 taalmaatjes Sleep 92
250 root localhost:57955 taalmaatjes Sleep 92
251 root localhost:57956 taalmaatjes Sleep 92
252 root localhost:57957 taalmaatjes Sleep 92
253 root localhost:57958 taalmaatjes Sleep 92
254 root localhost:57959 taalmaatjes Sleep 92
255 root localhost:57960 taalmaatjes Sleep 92
256 root localhost:57961 taalmaatjes Sleep 91
257 root localhost:57962 taalmaatjes Sleep 91
258 root localhost:57963 taalmaatjes Sleep 91
259 root localhost:57964 taalmaatjes Sleep 91
260 root localhost:57965 taalmaatjes Sleep 91
261 root localhost:57966 taalmaatjes Sleep 91
262 root localhost:57967 taalmaatjes Sleep 91
263 root localhost:57968 taalmaatjes Sleep 91
264 root localhost:57969 taalmaatjes Sleep 90
265 root localhost:57970 taalmaatjes Sleep 90
266 root localhost:57971 taalmaatjes Sleep 90
267 root localhost:57972 taalmaatjes Sleep 90
268 root localhost:57974 taalmaatjes Sleep 90
269 root localhost:57975 taalmaatjes Sleep 90
在代码中调用获取DataSource
这里有一个例子,它放在我可以调用的方法中:
@GET
public StringValueObject getVersion() throws NamingException, SQLException {
DataSource dataSource1 = (DataSource) ((javax.naming.Context) new InitialContext()
.lookup("java:comp/env"))
.lookup("jdbc/Taalmaatjes-db");
dataSource1.getConnection().close();
return new StringValueObject("v0.1-unsecured");
}
请注意,在调用此 getVersion 20 次后,我仍有 20 个连接显示在 SHOW PROCESSLIST 中。
可能有用的详细信息
我使用 jOOQ 作为我的 ORM 和 MySQL 数据库。它在 tomcat 上运行。
我还尝试添加 factory="org.apache.tomcat.jdbc.pool.DataSourceFactory",因为 中提到了这一点。但是,它也没有用。
我的知识
我对这个 JBNI 真的很陌生,JDBC 东西。我知道的太少,这就是我来这里学习的原因!我以为我使用的是 Tomcat JBNI,但是当我使用设置 removeAbandoned="true" 时,它给出了错误,该设置不存在,我应该使用 removeAbandonedOnBorrow。显然我正在使用 Apache JBDC?真的不知道区别,因为我只是想让 tomcat 来处理我的连接池。把它留给另一个图书馆似乎很聪明。
问题是您已将数据源资源配置为非单例,此处:
<Resource name="jdbc/Taalmaatjes-db" auth="Container" type="javax.sql.DataSource"
maxTotal="2" maxIdle="2"
initialSize="1" maxWaitMillis="10000"
username="root" password="rootie"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/taalmaatjes"
removeAbandonedOnBorrow="true"
removeAbandonedOnMaintenance="true"
removeAbandonedTimeout="5"
<!-- here -->
singleton="false" defaultAutoCommit="false" rollbackOnReturn="true" />
删除它并重试。
目标
我正在创建一个 REST web 服务(使用 RESTEasy),我认为如果我不管理连接池会很好,但我让 Tomcat 这样做。所以我在 META-INF/context.xml
问题
我看到在 MySQL 服务器上建立了很多连接,但我已经配置了总共只允许 2 个连接。我不知道为什么,但我的设置没有获取有关连接数等的信息。
Context.xml
<Context>
<!-- DataSource management from tomcat (reference created in web.xml) -->
<Resource name="jdbc/Taalmaatjes-db" auth="Container" type="javax.sql.DataSource"
maxTotal="2" maxIdle="2"
initialSize="1" maxWaitMillis="10000"
username="root" password="rootie"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/taalmaatjes"
removeAbandonedOnBorrow="true"
removeAbandonedOnMaintenance="true"
removeAbandonedTimeout="5"
singleton="false" defaultAutoCommit="false" rollbackOnReturn="true" />
</Context>
设置 maxTotal、maxIdle、initialSize、maxWaitMillis 似乎没有被拾取。使用 commando 'SHOW PROCESSLIST' 并连接到我的应用程序 39 次,我得到:
3 root localhost:56576 taalmaatjes Query 0 starting /* ApplicationName=IntelliJ IDEA 2017.3.4 */ SHOW PROCESSLIST
231 root localhost:57932 taalmaatjes Sleep 102
232 root localhost:57933 taalmaatjes Sleep 102
233 root localhost:57934 taalmaatjes Sleep 100
234 root localhost:57935 taalmaatjes Sleep 99
235 root localhost:57936 taalmaatjes Sleep 99
236 root localhost:57937 taalmaatjes Sleep 99
237 root localhost:57938 taalmaatjes Sleep 99
238 root localhost:57939 taalmaatjes Sleep 98
239 root localhost:57940 taalmaatjes Sleep 98
240 root localhost:57941 taalmaatjes Sleep 98
241 root localhost:57942 taalmaatjes Sleep 98
242 root localhost:57943 taalmaatjes Sleep 98
243 root localhost:57944 taalmaatjes Sleep 98
244 root localhost:57945 taalmaatjes Sleep 97
245 root localhost:57950 taalmaatjes Sleep 93
246 root localhost:57951 taalmaatjes Sleep 93
247 root localhost:57952 taalmaatjes Sleep 93
248 root localhost:57953 taalmaatjes Sleep 92
249 root localhost:57954 taalmaatjes Sleep 92
250 root localhost:57955 taalmaatjes Sleep 92
251 root localhost:57956 taalmaatjes Sleep 92
252 root localhost:57957 taalmaatjes Sleep 92
253 root localhost:57958 taalmaatjes Sleep 92
254 root localhost:57959 taalmaatjes Sleep 92
255 root localhost:57960 taalmaatjes Sleep 92
256 root localhost:57961 taalmaatjes Sleep 91
257 root localhost:57962 taalmaatjes Sleep 91
258 root localhost:57963 taalmaatjes Sleep 91
259 root localhost:57964 taalmaatjes Sleep 91
260 root localhost:57965 taalmaatjes Sleep 91
261 root localhost:57966 taalmaatjes Sleep 91
262 root localhost:57967 taalmaatjes Sleep 91
263 root localhost:57968 taalmaatjes Sleep 91
264 root localhost:57969 taalmaatjes Sleep 90
265 root localhost:57970 taalmaatjes Sleep 90
266 root localhost:57971 taalmaatjes Sleep 90
267 root localhost:57972 taalmaatjes Sleep 90
268 root localhost:57974 taalmaatjes Sleep 90
269 root localhost:57975 taalmaatjes Sleep 90
在代码中调用获取DataSource
这里有一个例子,它放在我可以调用的方法中:
@GET
public StringValueObject getVersion() throws NamingException, SQLException {
DataSource dataSource1 = (DataSource) ((javax.naming.Context) new InitialContext()
.lookup("java:comp/env"))
.lookup("jdbc/Taalmaatjes-db");
dataSource1.getConnection().close();
return new StringValueObject("v0.1-unsecured");
}
请注意,在调用此 getVersion 20 次后,我仍有 20 个连接显示在 SHOW PROCESSLIST 中。
可能有用的详细信息
我使用 jOOQ 作为我的 ORM 和 MySQL 数据库。它在 tomcat 上运行。
我还尝试添加 factory="org.apache.tomcat.jdbc.pool.DataSourceFactory",因为
我的知识
我对这个 JBNI 真的很陌生,JDBC 东西。我知道的太少,这就是我来这里学习的原因!我以为我使用的是 Tomcat JBNI,但是当我使用设置 removeAbandoned="true" 时,它给出了错误,该设置不存在,我应该使用 removeAbandonedOnBorrow。显然我正在使用 Apache JBDC?真的不知道区别,因为我只是想让 tomcat 来处理我的连接池。把它留给另一个图书馆似乎很聪明。
问题是您已将数据源资源配置为非单例,此处:
<Resource name="jdbc/Taalmaatjes-db" auth="Container" type="javax.sql.DataSource"
maxTotal="2" maxIdle="2"
initialSize="1" maxWaitMillis="10000"
username="root" password="rootie"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/taalmaatjes"
removeAbandonedOnBorrow="true"
removeAbandonedOnMaintenance="true"
removeAbandonedTimeout="5"
<!-- here -->
singleton="false" defaultAutoCommit="false" rollbackOnReturn="true" />
删除它并重试。