连接数与JPA的配置中的连接池大小不同

时间:2016-04-02 16:54:30

标签: java spring hibernate jpa spring-boot

我在我的应用程序中使用Spring Boot并将C3P0用于连接池。当我查询数据库时,即使是刚启动的应用程序也显示~14连接

find -name 'd\*'

当我对应用程序施加一些负载时,它会达到config + ~14个连接中指定的计数。我不确定这些连接的开放位置。

这是我用于初始化数据库的配置文件。

SELECT usename, count(*) as count FROM pg_stat_activity group by usename

这些是属性文件

中的值
public class PgDataSourceConfig {
    @Autowired
    JpaVendorAdapter jpaVendorAdapter;

    @Autowired
    DataSource dataSource;

    @Value("${spring.datasource.url}")
    private String databaseUrl;

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;

    @Value("${pg.import_files}")
    private String importFiles;

    @Value("${pg.hbm2ddl.auto}")
    private String hbm2Ddl;

    @Value("${pg.default_schema}")
    private String default_schema;

    @Value("${hibernate.jdbc.batch_size}")
    private String batch_size;
    @Value("${hibernate.c3p0.max_size}")
    private String max_size;
    @Value("${hibernate.c3p0.min_size}")
    private String min_size;
    @Value("${hibernate.c3p0.timeout}")
    private String timeout;
    @Value("${hibernate.c3p0.max_statements}")
    private String max_statements;
    @Value("${hibernate.c3p0.idle_test_period}")
    private String idle_test_period;
    @Value("${hibernate.c3p0.acquire_increment}")
    private String acquire_increment;
    @Value("${hibernate.c3p0.validate}")
    private String validate;

    @Value("${pg.format_sql}")
    private String formatSql;

    @Value("${pg.use_sql_comments}")
    private String useSqlComments;


    @Bean
    @DependsOn(value = {"flyway"})
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {
        Properties properties = new Properties();
        properties.setProperty("hibernate.connection.driver_class", "org.postgresql.Driver");
        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQL9Dialect");
        properties.setProperty("hibernate.hbm2ddl.auto", hbm2Ddl);
        properties.setProperty("hibernate.connection.url", databaseUrl);
        properties.setProperty("hibernate.connection.username", username);
        properties.setProperty("hibernate.connection.password", password);

        properties.setProperty("hibernate.hbm2ddl.import_files", importFiles);
        properties.setProperty("hibernate.format_sql", formatSql);
        properties.setProperty("hibernate.use_sql_comments", useSqlComments);
        properties.setProperty("hibernate.jdbc.batch_size", batch_size);
        properties.setProperty("hibernate.connection.provider_class", "org.hibernate.connection.C3P0ConnectionProvider");
        properties.setProperty("hibernate.c3p0.max_size", max_size);
        properties.setProperty("hibernate.c3p0.min_size", min_size);
        properties.setProperty("hibernate.c3p0.timeout", timeout);
        properties.setProperty("hibernate.c3p0.max_statements", max_statements);
        properties.setProperty("hibernate.c3p0.idle_test_period", idle_test_period);
        properties.setProperty("hibernate.c3p0.acquire_increment", acquire_increment);
        properties.setProperty("hibernate.c3p0.validate", validate);
        properties.setProperty("hibernate.id.new_generator_mappings", "true");

        final LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        localContainerEntityManagerFactoryBean.setDataSource(dataSource);
        localContainerEntityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter);
        localContainerEntityManagerFactoryBean.setPackagesToScan("com.test");
        localContainerEntityManagerFactoryBean.setPersistenceUnitName("default");
        localContainerEntityManagerFactoryBean.setJpaProperties(properties);
        localContainerEntityManagerFactoryBean.afterPropertiesSet();

        return localContainerEntityManagerFactoryBean;
    }


    @Bean(name = "entityManager")
    public EntityManager entityManager() {
        return entityManagerFactory().createEntityManager();
    }

    @Primary
    @Bean(name = "entityManagerFactory")
    public EntityManagerFactory entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean emf = this.entityManagerFactoryBean();
        return emf.getObject();
    }

    @Bean(name = "transactionManager")
    public PlatformTransactionManager transactionManager() {
        JpaTransactionManager tm = new JpaTransactionManager();
        tm.setEntityManagerFactory(entityManagerFactory());
        return tm;
    }
}

我不确定导致额外连接的配置有什么问题。 我怎样才能解决这个问题? 感谢

0 个答案:

没有答案