【问题标题】:HikariCP too many Connections with jooqHikariCP 与 jooq 的连接过多
【发布时间】:2016-08-13 06:09:14
【问题描述】:

在 Spring Restful API 中使用 jooq 和 HikariCP DataSource(Autowired)时,我遇到了问题,jooq 不知何故没有释放 DataSource。

一些代码:

@Autowired
private DataSource dataSource;
//Further down 
DSLContext create = DSL.using(dataSource, SQLDialect.MYSQL);

使用此存储库/调用两三次后,我打开的连接太多。

我的 dispatcher-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:security="http://www.springframework.org/schema/security"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/security
        http://www.springframework.org/schema/security/spring-security.xsd">

    <context:component-scan base-package="com.rh" />

     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="20971520"/> <!-- 20 MB -->
    </bean>

    <context:property-placeholder location="classpath:database/database.properties"/>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
        <property name="poolName" value="springHikariCP" />
        <property name="connectionTestQuery" value="SELECT 1" />
        <property name="dataSourceClassName" value="${jdbc.driver}" />
        <property name="maximumPoolSize" value="20" />
        <property name="idleTimeout" value="20" />

        <property name="dataSourceProperties">
            <props>
                <prop key="url">${jdbc.url}</prop>
                <prop key="user">${jdbc.username}</prop>
                <prop key="password">${jdbc.password}</prop>
                <prop key="prepStmtCacheSize">50</prop>
                <prop key="prepStmtCacheSqlLimit">50</prop>
                <prop key="cachePrepStmts">true</prop>
                <prop key="useServerPrepStmts">true</prop>                
            </props>
        </property>
    </bean>

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

    <bean id="passwordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder">
        <!--<beans:constructor-arg value="256" />-->
        <!--<beans:property name="iterations" value="1000" />-->
    </bean>  

    <!--Different providers-->
    <bean id="cloudinaryProvider" class="com.rh.bean.CloudinaryProvider"></bean>
    <bean id="s3Provider" class="com.rh.bean.S3Provider"></bean>    

    <bean id="objectMapper" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean" autowire="no">
        <property name="propertyNamingStrategy" value="CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES" />
    </bean>
    <mvc:annotation-driven>
        <mvc:path-matching suffix-pattern="false" trailing-slash="false" />
        <mvc:argument-resolvers>
            <bean class="com.rh.util.CurrentUserHandlerMethodArgumentResolver"/>         
        </mvc:argument-resolvers>        
        <mvc:message-converters register-defaults="true">
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="objectMapper">
                    <ref bean="objectMapper" />
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven> 

    <security:global-method-security pre-post-annotations="enabled" secured-annotations="enabled"></security:global-method-security> 
</beans>

【问题讨论】:

  • jOOQ很早以前就有bug,和连接管理有关。你用的是什么 jOOQ 版本?
  • 感谢您的帮助 ;) 版本 3.7.3
  • 好的,很有趣。您可以记录与数据源的交互吗? IE。谁从中获得了联系,谁又将其关闭了?您能否展示更多的 jOOQ API 用法来帮助重现这一点,或者您的项目已经非常大了?
  • 不——我会那样做。明天将发布更多代码!非常感谢 :) 我实际上认为这是因为 Autowired - 但通常 jooq 应该只发布 DataSource。
  • 是的,jOOQ 在内部使用DataSourceConnectionProvider(它在 2.x 版本中存在错误,因此是我最初的评论)。 ConnectionProvider 应该负责在查询后关闭所有连接...从高层次来看,无论如何,您的 jOOQ 使用似乎是正确的。

标签: java mysql spring jooq hikaricp


【解决方案1】:

好的,我解决了这个问题:

    @Autowired
private DataSource dataSource;
//Further down 
Connection con=dataSource.getConnection();
DSLContext create = DSL.using(con, SQLDialect.MYSQL);
//Execute code here
con.close();

所以我没有直接使用 DataSource,而是使用了一个连接并释放了它。

【讨论】:

    猜你喜欢
    • 2015-02-14
    • 2019-10-27
    • 2021-01-09
    • 2016-02-22
    • 1970-01-01
    • 2019-08-15
    • 2021-08-24
    • 2016-01-03
    • 1970-01-01
    相关资源
    最近更新 更多