C3P0使用ComboPooledDataSource与Postgres的时候抛出com.mchange.v2.resourcepool.CannotAcquireResourceException

问题描述 投票:0回答:1

我试图设立一个春天c3p0连接使用的Postgres DB /休眠应用程序,我无法连接到服务器。

如果我使用“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
java postgresql jdbc c3p0
1个回答
-1
投票

我面临着C3P0类似的问题,而我是升级到PostgreSQL 10.这是不能获得来自数据库的连接。虽然对这个问题研究,我就过来了这个博客Connection Pool Analysis和切换HikariCP。它比C3P0在性能方面更好,以及如果你想升级你的数据库,然后HikariCP有更好的支持。

© www.soinside.com 2019 - 2024. All rights reserved.