HikariCP与jooq的连接太多了

时间:2016-04-20 13:48:54

标签: java mysql spring jooq hikaricp

在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>

1 个答案:

答案 0 :(得分:0)

好的,我修复了问题:

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

因此,我没有直接使用DataSource,而是使用了连接并将其释放。

相关问题