【问题标题】:Spring JUnit Test fail caused by: org.postgresql.util.PSQLException: FATAL: sorry, too many clients alreadySpring JUnit 测试失败的原因是:org.postgresql.util.PSQLException: FATAL: sorry, too many clients already
【发布时间】:2014-11-04 10:13:43
【问题描述】:

如果我一次运行所有 JUnit 测试用例 (~300),我会得到以下异常:

org.postgresql.util.PSQLException: FATAL: sorry, too many clients already
    at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:293)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:108)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:66)
    at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:125)
    at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:30)
    at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:22)
    at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:32)
    at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:24)
    at org.postgresql.Driver.makeConnection(Driver.java:393)
    at org.postgresql.Driver.connect(Driver.java:267)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at test.testingTools.DatabaseConnector.getTestConnection(DatabaseConnector.java:24)
    at test.repository.cascade.merge_persist.ParentCascadeMPTest.setUp(ParentCascadeMPTest.java:56)
    at sun.reflect.GeneratedMethodAccessor73.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:233)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:176)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

在 postgres 配置 (/etc/postgresql/9.2/main/postgresql.conf) 中,该值设置为 100,如下所示: max_connections = 100

Spring AppContext:

package test;

import java.beans.PropertyVetoException;
import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.hibernate.dialect.PostgreSQL9Dialect;
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.EnableLoadTimeWeaving;
import org.springframework.context.annotation.EnableLoadTimeWeaving.AspectJWeaving;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.aspectj.EnableSpringConfigured;
import org.springframework.core.env.Environment;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;

import com.mchange.v2.c3p0.AbstractComboPooledDataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;

@Configuration
@EnableSpringConfigured
@EnableLoadTimeWeaving(aspectjWeaving = AspectJWeaving.ENABLED)
@ComponentScan(basePackages = { "test" })
@PropertySource("META-INF/spring/props.properties")
@EnableJpaRepositories("test.repository")
public class AppContext {

    @Autowired
    private Environment env;
    @Autowired
    private DataSource dataSource;

    @Bean(name = "dataSource", destroyMethod = "close")
    @Profile("test")
    public DataSource dataSourceTest() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass(env.getProperty("testDb.driverClass"));
        dataSource.setJdbcUrl(env.getProperty("testDb.jdbcUrl"));
        dataSource.setUser(env.getProperty("testDb.user"));
        dataSource.setPassword(env.getProperty("testDb.password"));
        setDataSourceProperties(dataSource);
        return dataSource;
    }

    // use this to configure c3p0
    /*
     * with the @Repeat annotation from springframework.test at a test connecting to the test database, a PSQLException
     * for too many clients can easily be reproduced to try out new configuration parameters
     */
    private void setDataSourceProperties(AbstractComboPooledDataSource dataSource) {
        dataSource.setAcquireIncrement(5);
        dataSource.setMinPoolSize(5);
        dataSource.setMaxPoolSize(100);
    }

    @Bean
    public Map<String, Object> jpaProperties() {
        Map<String, Object> props = new HashMap<String, Object>();
        props.put("hibernate.dialect", PostgreSQL9Dialect.class.getName());
        return props;
    }

    @Bean
    public JpaVendorAdapter jpaVendorAdapter() {
        HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
        hibernateJpaVendorAdapter.setShowSql(false);
        hibernateJpaVendorAdapter.setGenerateDdl(true);
        hibernateJpaVendorAdapter.setDatabase(Database.POSTGRESQL);
        return hibernateJpaVendorAdapter;
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        return new JpaTransactionManager(entityManagerFactory().getObject());
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
        return new PersistenceExceptionTranslationPostProcessor();
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
        emf.setDataSource(this.dataSource);
        emf.setJpaPropertyMap(this.jpaProperties());
        emf.setJpaVendorAdapter(this.jpaVendorAdapter());
        emf.setPackagesToScan("test");
        return emf;
    }

}

DBUnit 数据库连接:

package test;

import java.sql.Connection;
import java.sql.DriverManager;

import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.springframework.stereotype.Component;

@Component
public class DatabaseConnector {

    public DatabaseConnector() {

    }

    public IDatabaseConnection getTestConnection() throws Exception {

        Connection dbConnection = null;

        Class.forName("org.postgresql.Driver");

        dbConnection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/test", "user", "password");

        IDatabaseConnection connection = new DatabaseConnection(dbConnection);

        return connection;
    }
}

我尝试过的想法:

  • 更改 c3p0 的 setMaxPoolSize(从 1 到 100)

  • 增加 postgres 配置中的 max_connections(postgres 不再启动)

  • 添加 Thread.sleep(1000);到@Before 和@Before(1 到 10 秒)

如果我经常运行所有测试,有时它会工作,当我只运行一个子集时,它大部分时间都可以工作。

我也确信 100 个数据库连接应该足以进行 300 次测试...

【问题讨论】:

    标签: spring postgresql junit dbunit spring-test


    【解决方案1】:

    我在 DBUnit 数据库连接中发现了问题。 它一直在创建一个新的连接。

    现在它的行为就像一个单例,问题就消失了。

    package test.testingTools;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    
    import org.dbunit.database.DatabaseConnection;
    import org.dbunit.database.IDatabaseConnection;
    import org.springframework.stereotype.Component;
    
    @Component
    public class DatabaseConnector {
    
        IDatabaseConnection connection;
    
        public DatabaseConnector() throws Exception {
            Connection dbConnection = null;
    
            Class.forName("org.postgresql.Driver");
    
            dbConnection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/test", "user", "password");
    
            connection = new DatabaseConnection(dbConnection);
    
        }
    
        public IDatabaseConnection getTestConnection() {
    
            return connection;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-07-26
      • 2018-04-05
      • 2019-05-01
      • 2022-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-27
      相关资源
      最近更新 更多