C3P0创建由MinPoolSize指定的连接的两倍

时间:2018-09-09 15:28:12

标签: c3p0

我正在尝试使用C3P0来处理连接池。 这是我的配置类

package com.pack.webstore.config;

import javax.sql.DataSource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.mchange.v2.c3p0.ComboPooledDataSource;

@Configuration
@ComponentScan("com.pack.webstore")
@PropertySource(value= {"classpath:database.properties", "classpath:hibernate.properties", "classpath:c3p0.properties"})
@EnableTransactionManagement
public class RootApplicationContextConfig {

    private Logger logger = Logger.getLogger(RootApplicationContextConfig.class);

    @Autowired
    private Environment environment;    

    @Bean
    public LocalSessionFactoryBean getSessionFactory() {      
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();         
        sessionFactory.setDataSource(getDataSource());
        sessionFactory.setPackagesToScan("com.pack.webstore.domain");   
        return sessionFactory;    
    }

    @Bean
    public HibernateTransactionManager getTransactionManager() {
        HibernateTransactionManager transactionManager = new HibernateTransactionManager();
        transactionManager.setSessionFactory(getSessionFactory().getObject());
        return transactionManager;
    }   

    @Bean
    public DataSource getDataSource() {
        ComboPooledDataSource сomboPooledDataSource = new ComboPooledDataSource();
        try {
             сomboPooledDataSource.setDriverClass(environment.getProperty("spring.datasource.database-driver"));
        }
        catch(Exception e) {

        }       
        сomboPooledDataSource.setJdbcUrl(environment.getProperty("spring.datasource.url"));
        сomboPooledDataSource.setUser(environment.getProperty("spring.datasource.username"));
        сomboPooledDataSource.setPassword(environment.getProperty("spring.datasource.password"));
        сomboPooledDataSource.setMinPoolSize(Integer.parseInt(environment.getProperty("hibernate.c3p0.min_size")));
        сomboPooledDataSource.setMaxPoolSize(Integer.parseInt(environment.getProperty("hibernate.c3p0.max_size")));
        сomboPooledDataSource.setAcquireIncrement(Integer.parseInt(environment.getProperty("hibernate.c3p0.acquire_increment")));
        сomboPooledDataSource.setMaxStatements(Integer.parseInt(environment.getProperty("hibernate.c3p0.max_statements")));
        сomboPooledDataSource.setTestConnectionOnCheckin(Boolean.parseBoolean(environment.getProperty("hibernate.c3p0.test_connection_on_checkin")));
        сomboPooledDataSource.setIdleConnectionTestPeriod(Integer.parseInt(environment.getProperty("hibernate.c3p0.idle_connection_test_period")));
        сomboPooledDataSource.setMaxIdleTimeExcessConnections(Integer.parseInt(environment.getProperty("hibernate.c3p0.max_idle_time_excess_connections")));
        return сomboPooledDataSource;
    }

}

这是我的c3p0.properties文件

#C3P0 properties
hibernate.c3p0.min_size=11
hibernate.c3p0.max_size=20
hibernate.c3p0.acquire_increment=1
hibernate.c3p0.max_statements=30
hibernate.c3p0.test_connection_on_checkin=true
hibernate.c3p0.idle_connection_test_period=300
hibernate.c3p0.max_idle_time_excess_connections=240

当我尝试查看到MySQL数据库的连接列表(使用MySQLWorkbench)时,我看到方法setMinPoolSize()设置的连接总是存在 double ,即使该数字排除通过方法setMaxPoolSize()设置的MaxPoolSize值。

怎么可能?我的代码有问题吗?

0 个答案:

没有答案