gpt4 book ai didi

java - hibernate 与缓存的连接过多

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:10:43 26 4
gpt4 key购买 nike

我正在使用 hibernate 来尝试检索缓存的查询。

@Transactional
public interface ProductDAO extends JpaRepository<Product, Long> {
@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value = "true") })
Product findByCode(String code);
}

我正在进行负载测试,我在 1000 次迭代的大循环中执行此操作。

for (int i = 0; i < 500; i++) {
URL myURL = new URL("http://localhost:8080/test");
URLConnection myURLConnection = myURL.openConnection();
myURLConnection.connect();
myURLConnection.getContent();
}

我已经用 showsql 进行了检查,我可以看到只有一条 SQL 语句是在我第一次访问数据库时生成的,之后它被缓存了。

但即使没有显示 SQL,我仍然收到以下错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Too many connections

我的 hibernate 属性:

#hibernate properties
hibernate.dialect = ${hibernate.dialect}
hibernate.show_sql = false
hibernate.hbm2ddl.auto = ${hibernate.hbm2ddl}
hibernate.c3p0.min_size = 10
hibernate.c3p0.max_size = 100
hibernate.c3p0.timeout = 300
hibernate.c3p0.max_statements = 50
hibernate.c3p0.acquire_increment = 5
hibernate.c3p0.idle_test_period = 3000

hibernate.cache.use_second_level_cache=true
hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
hibernate.cache.use_query_cache=true

数据库配置:

<bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="${db.url}" />
<property name="driverClassName" value="${db.driverClassName}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
</bean>

<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
<property name="dataSource" ref="dataSource" />

<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="false" />
<property name="generateDdl" value="true" />
<property name="databasePlatform" value="${hibernate.dialect}" />
</bean>
</property>

<property name="jpaProperties" ref="hibernateProperties" />

<property name="packagesToScan">
<array>
<value>com.exammple.model</value>
</array>
</property>
</bean>

<bean id="hibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="classpath:/spring/hibernate.properties" />
</bean>

<bean id="sessionFactory" factory-bean="entityManagerFactory" factory-method="getSessionFactory" />

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="dataSource" ref="dataSource" />
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

<jpa:repositories base-package="com.example.dal" entity-manager-factory-ref="entityManagerFactory"
transaction-manager-ref="transactionManager" repository-impl-postfix="CustomImpl" />

最佳答案

问题是您的配置没有使用连接池。

您正在配置 DriverManagerDataSource这不是一个合适的连接池。您正在将此 bean 注入(inject) LocalContainerEntityManagerFactoryBean这会呈现您的 hibernate.connectionhibernate.c3p0属性无用,它们未被使用。

解决方案很简单,删除 hibernate.c3p0hibernate.connection属性并替换 DriverManagerDataSource使用适当的池实现。我会推荐HikariCP优于 C3P0,但这是个人偏好。

<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
<property name="poolName" value="springHikariCP" />
<property name="connectionTestQuery" value="SELECT 1" />
<property name="dataSourceClassName" value="${db.driverClassName}" />
<property name="dataSourceProperties">
<props>
<prop key="url">${db.url}</prop>
<prop key="user">${db.username}</prop>
<prop key="password">${jdb.password}</prop>
</props>
</property>
</bean>

<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<constructor-arg ref="hikariConfig" />
</bean>

关于java - hibernate 与缓存的连接过多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28948510/

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