将ComboPooledDataSource与Postgres一起使用时c3p0抛出com.mchange.v2.resourcepool.CannotAcquireResourceException

时间:2019-01-15 19:31:56

标签: java postgresql jdbc c3p0

我正在尝试使用Postgres DB为弹簧/休眠应用程序设置c3p0连接池,但无法连接到服务器。

如果我使用“ org.postgresql.jdbc2.optional.PoolingDataSource”,则应用程序可以连接到数据库,但是如果我使用“ com.mchange.v2.c3p0.ComboPooledDataSource”,它将挂起一段时间,然后抛出: “ com.mchange.v2.resourcepool.CannotAcquireResourceException:ResourcePool无法从其主工厂或源中获取资源。”

这是数据源的类

    @Configuration
    @EnableTransactionManagement
    public class JpaConfigurator {

        @Bean
        public DataSource getDataSource() throws PropertyVetoException {

    //      PoolingDataSource  dataSource =  new PoolingDataSource();
    //      dataSource.setServerName("localhost");
    //      dataSource.setPortNumber(5432);
    //      dataSource.setDatabaseName("projeto_jpa");
    //      dataSource.setUser("postgres");
    //      dataSource.setPassword("pass");
    //      dataSource.setInitialConnections(2);
    //      dataSource.setMaxConnections(5);
    //
            ComboPooledDataSource dataSource = new ComboPooledDataSource();
            dataSource.setDriverClass("org.postgresql.Driver");
            dataSource.setJdbcUrl("jdbc:postgresql://localhost:5432/projeto_jpa");
            dataSource.setUser("postgres");
            dataSource.setPassword("pass");
            dataSource.setMinPoolSize(5);
            dataSource.setNumHelperThreads(5);

            return dataSource;
        }

        @Bean
        public LocalContainerEntityManagerFactoryBean getEntityManagerFactory(DataSource dataSource) {
            LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();

            entityManagerFactory.setPackagesToScan("br.com.caelum");
            entityManagerFactory.setDataSource(dataSource);

            entityManagerFactory
                    .setJpaVendorAdapter(new HibernateJpaVendorAdapter());

            Properties props = new Properties();

            props.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQL95Dialect");
            props.setProperty("hibernate.show_sql", "true");
            props.setProperty("hibernate.hbm2ddl.auto", "create-drop");

            entityManagerFactory.setJpaProperties(props);
            return entityManagerFactory;
        }

        @Bean
        public JpaTransactionManager getTransactionManager(EntityManagerFactory emf) {
            JpaTransactionManager transactionManager = new JpaTransactionManager();
            transactionManager.setEntityManagerFactory(emf);

            return transactionManager;
        }

    }

以下是测试的主要课程

    public class TestPool {

        public static void main(String[] args) throws PropertyVetoException, SQLException, InterruptedException{


    //      PoolingDataSource datasource = (PoolingDataSource) new JpaConfigurator().getDataSource();

            ComboPooledDataSource datasource = (ComboPooledDataSource) new JpaConfigurator().getDataSource();

            for (int i = 0; i < 10; i++) {
                datasource.getConnection();
                System.out.println("Got connection " + i);

            }

        }

    }

使用c3p0时似乎存在某种配置问题。有什么想法吗?

全异常堆栈跟踪

Exception in thread "main" java.sql.SQLException: Connections could not be acquired from the underlying database!
    at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:529)
    at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)
    at br.com.caelum.TestPool.main(TestPool.java:18)
Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.
    at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1319)
    at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557)
    at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525)
    ... 2 more

1 个答案:

答案 0 :(得分:-1)

在升级到PostgreSQL 10时,我遇到了c3p0的类似问题。它无法从数据库获取连接。在研究此问题时,我浏览了该博客Connection Pool Analysis,并切换了HikariCP。在性能以及要升级数据库方面,它比C3p0更好,因此HikariCP具有更好的支持。