简体   繁体   English

tomcat jdbc池在连接重用方面有一些不同的行为

[英]tomcat jdbc pool has some different behaviour about connection reuse

I have configured Tomcat JDBC pool in my web application and added maven dependency with version 8.0.38 which is my tomcat version also. 我已经在Web应用程序中配置了Tomcat JDBC池,并添加了8.0.38版本的maven依赖,这也是我的tomcat版本。 Now I get connection from that pool and check value of autoCommit property and it is "true" then I set that connection's autoCommit to "false". 现在,我从该池中获得连接,并检查autoCommit属性的值,它的值为“ true”,然后将该连接的autoCommit设置为“ false”。 Then I commit the transaction, then close that connection. 然后,我提交事务,然后关闭该连接。 Now I get another connection from pool and check the value of autoCommit property and it was "false". 现在,我从池中获得另一个连接,并检查autoCommit属性的值,它是“ false”。 But I was expecting it as true. 但是我期望它是真的。 I also use Apache Common DBCP2 pooling library, and that has not this kind of behaviour. 我还使用Apache Common DBCP2池库,但没有这种行为。 Whenever I get connection from common DBCP2 pool, it return connection with autoCommit set to "true". 每当我从公共DBCP2池获得连接时,它都会将autoCommit设置为“ true”返回连接。 I have tested and seen this behaviour of tomcat jdbc pool. 我已经测试并看到了tomcat jdbc池的这种行为。

Connection con1 = basicDataSourceWrite.getConnection();
con1.setAutoCommit(false);
System.out.println(con1.getAutoCommit()+ " con1  " );
con1.commit();
con1.close();

Connection con2 = basicDataSourceWrite.getConnection();
System.out.println(con2.getAutoCommit()+ " con2  " );

Output of above code is 上面代码的输出是

false con1 错误的骗局
false con2 错误的con2

<bean id="basicDataSourceWrite" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="${mysqlEndpointWrite}" />
    <property name="username" value="${mysqlUserWrite}" />
    <property name="password" value="${mysqlPasswordWrite}" />
    <property name="defaultAutoCommit" value="true" />
    <property name="initialSize" value="4" />
    <property name="maxActive" value="5" />
</bean>

here setting defaultAutoCommit to "true" even not work for me. 在这里将defaultAutoCommit设置为“ true”甚至对我不起作用。 it always return connection with autoCoomit false. 它总是返回带有autoCoomit false的连接。

So I want to know how common DBCP2 manage this and how to achieve this in tomcat JDBC pool? 所以我想知道常见的DBCP2如何管理它以及如何在tomcat JDBC池中实现它?

This is a known bug (or rather a feature since it hasn't been fixed) of Tomcat JDBC. 这是Tomcat JDBC的已知错误 (或更确切地说是一个功能,因为尚未修复)。

Setting the value of defaultAutoCommit isn't enough, you also need to enable interceptors which will actually enforce those settings. 仅设置defaultAutoCommit的值还不够,您还需要启用拦截器 ,该拦截器实际上将强制执行这些设置。 This will make the defaultAutoCommit value affect the connections. 这将使defaultAutoCommit值影响连接。

<property name="jdbcInterceptors" value="ConnectionState" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM