目标
我正在创建一个REST Web服务(使用RESTEasy),我认为如果我不管理连接池会很好,但我让Tomcat这样做。所以我在META-INF / context.xml中添加了一个资源
问题
我在MySQL服务器上看到了很多连接,但是我已经配置了只允许2个连接。我不知道为什么,但是我的设置没有得到关于nr连接等的信息。
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似乎没有被提取。使用突击队'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”,因为Why does my Tomcat only open 8 JDBC connections中提到了这一点。但是,它也没有用。
我的知识
我真的很喜欢这个JBNI,JDBC的东西。我知道的太少了,这就是我在这里学习的原因!我以为我使用的是Tomcat JBNI,但是当我使用设置removeAbandoned =“true”时,它给出了错误,即此设置不存在,我应该使用removeAbandonedOnBorrow。显然我正在使用Apache JBDC?真的不知道区别,因为我只是想让tomcat来处理我的连接池。看起来聪明,把它留给另一个图书馆。
答案 0 :(得分:0)
问题是您已将数据源资源配置为非单例,此处:
<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" />
将其删除,然后重试。