gpt4 book ai didi

java - Hibernate池似乎释放了连接

转载 作者:行者123 更新时间:2023-11-29 13:41:05 24 4
gpt4 key购买 nike

我在使用 hibernate 时遇到了奇怪的行为。我有一个使用 hibernate 和 spring 的 java web 应用程序,使用 MySQL 数据库。

症状:使用以下命令检查我的 sql 上的连接 session :

show processlist;

我可以看到在我的数据源配置中定义的连接数量,但是随着时间的推移,它们的 ID 正在发生变化,让我相信连接正在关闭,然后重新连接。即使没有流量,也会发生此行为。
我希望池化连接将其 ID 保留在数据库中。

配置:

<bean id="DataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<property name="maxWait" value="10" />
<property name="maxIdle" value="5" />
<property name="maxActive" value="0" />
<property name="validationQuery" value="SELECT 1"/>
<property name="testOnBorrow" value="true" />
<property name="testOnReturn" value="true"/>
<property name="testWhileIdle" value="true"/>
<property name="timeBetweenEvictionRunsMillis" value="10000"/>
<property name="minEvictableIdleTimeMillis" value="60000"/>
</bean>
<bean id="SessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="DataSource"></property>
<property name="mappingResources">
<list>
<value>
data/entities/entity.hbm.xml
</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
</bean>

<bean id="entityDaoImpl" class="data.dao.EntityDaoImpl">
<property name="sessionFactory" ref="SessionFactory" />
</bean>

<bean id="SessionFactory2"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="DataSource"></property>
<property name="mappingResources">
<list>
<value>
data/entities/entity2.hbm.xml
</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
</bean>

<bean id="entity2DaoImpl" class="data.dao.Entity2DaoImpl">
<property name="sessionFactory" ref="SessionFactory2" />
</bean>

我的猜测是,这可能与两个不同的 session 工厂使用相同的数据源这一事实有关,但我相信我错过了一些更深入的理解(或者是正确但不知道为什么,或者是完全错误)

我还要补充一点,我使用在 tomcat 6 上运行的 spring 2.5 和 Hibernate 3.1.1。我注意到在很多地方人们不鼓励使用 hibernateTemplate 但代码正在使用它。

编辑:

我打开了审核,试图弄清楚连接正在做什么,我重点关注了我看到的一个自行关闭的连接:

130806 10:58:43      13 Connect     user@localhost on database
130806 10:58:43 13 Query SET NAMES hebrew
130806 10:58:43 13 Query SET character_set_results = NULL
130806 10:58:43 13 Query SHOW VARIABLES
130806 10:58:43 13 Query SHOW COLLATION
130806 10:58:43 13 Query SET autocommit=1
130806 10:58:43 13 Query SET sql_mode='STRICT_TRANS_TABLES'
130806 10:58:43 13 Query SELECT 1
130806 10:58:43 13 Query SET autocommit=1
130806 10:58:54 13 Query SET autocommit=1
130806 10:58:54 13 Query SELECT 1
130806 10:58:54 13 Query SET autocommit=1
130806 10:59:25 13 Query SET autocommit=1
130806 10:59:25 13 Query SELECT 1
130806 10:59:25 13 Query SET autocommit=1
130806 11:00:27 13 Quit

据我了解,这表明问题不在数据库端,因为他收到了退出命令。所以我再次想知道我的数据源配置中是否缺少一些 keepAlive 配置?

谢谢

最佳答案

MySQL 在 wait_timeout 后终止空闲连接。您的连接池检测到它们已死亡并重新连接它们。

一切都很好...

关于java - Hibernate池似乎释放了连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18067167/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com