gpt4 book ai didi

spring - 我的 Spring 应用程序泄漏数据库连接,而我使用默认的 Roo 配置

转载 作者:行者123 更新时间:2023-11-28 22:13:44 26 4
gpt4 key购买 nike

我的申请遇到了严重的问题。它泄漏数据库连接,而我使用默认的 Spring Roo 数据源配置如下:

<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
<property name="driverClassName" value="${database.driverClassName}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.username}" />
<property name="password" value="${database.password}" />
<property name="testOnBorrow" value="true" />
<property name="testOnReturn" value="true" />
<property name="testWhileIdle" value="true" />
<property name="timeBetweenEvictionRunsMillis" value="1800000" />
<property name="numTestsPerEvictionRun" value="3" />
<property name="minEvictableIdleTimeMillis" value="1800000" />
<property name="validationQuery" value="SELECT 1" />
<property name="maxActive" value="2"/>
<property name="logAbandoned" value="true"/>
<property name="removeAbandoned" value="true"/>
</bean>

这是导致泄漏的 Controller 方法:

@RequestMapping(value = "getMessages", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public DeferredResult<List<Message>> getMessages(@RequestParam final Long senderId) {
// TODO: check that recipientId was not changed by malicious user!!
final Long recipientId = memberService.retrieveCurrentMember().getId();
final String messageRequestKey = new StringBuilder().append(senderId).append(":").append(recipientId).toString();
final DeferredResult<List<Message>> deferredResult = new DeferredResult<List<Message>>(null, Collections.emptyList());
messageRequests.put(messageRequestKey, deferredResult);

deferredResult.onCompletion(new Runnable() {
@Override
public void run() {
messageRequests.remove(messageRequestKey);
}
});

List<Message> unReadMessages = messageService.findUnreadMessages(senderId, recipientId);
if (!unReadMessages.isEmpty()) {
deferredResult.setResult(unReadMessages);
}
return deferredResult;
}

似乎连接没有通过上述方法返回到池中(由 ajax 连续轮询)。

有人可以帮忙吗?

编辑:

休眠配置:

<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<!-- value="create" to build a new database on each run; value="update" to modify an existing database; value="create-drop" means the same as "create" but also drops tables when Hibernate closes; value="validate" makes no changes to the database -->
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
<property name="hibernate.connection.charSet" value="UTF-8"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.use_sql_comments" value="true"/>
<property name="hibernate.connection.release_mode" value="after_transaction"/>
</properties>
</persistence-unit>

休眠版本:

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.8.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.1.8.Final</version>
<exclusions>
<exclusion>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
</exclusion>
<exclusion>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>

最佳答案

我不确定你是否使用休眠?如果是这样,请查看设置 hibernate.connection.release_mode 。我们遇到了这个问题并将其提交给 after_transaction 解决了我们所有的连接问题。

编辑

我们的配置:



<pre><code><beans:bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<beans:property name="dataSource" ref="mvcDatasource" />
<beans:property name="persistenceUnitName" value="mvcPersistenceUnit" />

<beans:property name="persistenceProvider">
<beans:bean class="org.hibernate.ejb.HibernatePersistence" />
</beans:property>

<!-- Fix Hibernate not properly connected with spring -->
<beans:property name="jpaVendorAdapter">
<beans:bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</beans:property>
<beans:property name="jpaPropertyMap">
<beans:map>
<!-- Connection release fix -->
<beans:entry key="hibernate.connection.release_mode" value="after_transaction" />

<beans:entry key="hibernate.dialect" value="${hibernate.dialect}" />
...
</beans:map>
</beans:property>
</beans:bean>
</code></pre>

关于spring - 我的 Spring 应用程序泄漏数据库连接,而我使用默认的 Roo 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15573370/

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