Spring JUnit Test fail caused by: org.postgresql.util.PSQLException: FATAL: sorry, too many clients already -
if run junit test cases @ once (~300) following exception:
org.postgresql.util.psqlexception: fatal: sorry, many clients @ org.postgresql.core.v3.connectionfactoryimpl.doauthentication(connectionfactoryimpl.java:293) @ org.postgresql.core.v3.connectionfactoryimpl.openconnectionimpl(connectionfactoryimpl.java:108) @ org.postgresql.core.connectionfactory.openconnection(connectionfactory.java:66) @ org.postgresql.jdbc2.abstractjdbc2connection.<init>(abstractjdbc2connection.java:125) @ org.postgresql.jdbc3.abstractjdbc3connection.<init>(abstractjdbc3connection.java:30) @ org.postgresql.jdbc3g.abstractjdbc3gconnection.<init>(abstractjdbc3gconnection.java:22) @ org.postgresql.jdbc4.abstractjdbc4connection.<init>(abstractjdbc4connection.java:32) @ org.postgresql.jdbc4.jdbc4connection.<init>(jdbc4connection.java:24) @ org.postgresql.driver.makeconnection(driver.java:393) @ org.postgresql.driver.connect(driver.java:267) @ java.sql.drivermanager.getconnection(drivermanager.java:664) @ java.sql.drivermanager.getconnection(drivermanager.java:247) @ test.testingtools.databaseconnector.gettestconnection(databaseconnector.java:24) @ test.repository.cascade.merge_persist.parentcascademptest.setup(parentcascademptest.java:56) @ sun.reflect.generatedmethodaccessor73.invoke(unknown source) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:483) @ org.junit.runners.model.frameworkmethod$1.runreflectivecall(frameworkmethod.java:47) @ org.junit.internal.runners.model.reflectivecallable.run(reflectivecallable.java:12) @ org.junit.runners.model.frameworkmethod.invokeexplosively(frameworkmethod.java:44) @ org.junit.internal.runners.statements.runbefores.evaluate(runbefores.java:24) @ org.springframework.test.context.junit4.statements.runbeforetestmethodcallbacks.evaluate(runbeforetestmethodcallbacks.java:74) @ org.springframework.test.context.junit4.statements.runaftertestmethodcallbacks.evaluate(runaftertestmethodcallbacks.java:83) @ org.springframework.test.context.junit4.statements.springrepeat.evaluate(springrepeat.java:72) @ org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:233) @ org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:87) @ org.junit.runners.parentrunner$3.run(parentrunner.java:238) @ org.junit.runners.parentrunner$1.schedule(parentrunner.java:63) @ org.junit.runners.parentrunner.runchildren(parentrunner.java:236) @ org.junit.runners.parentrunner.access$000(parentrunner.java:53) @ org.junit.runners.parentrunner$2.evaluate(parentrunner.java:229) @ org.springframework.test.context.junit4.statements.runbeforetestclasscallbacks.evaluate(runbeforetestclasscallbacks.java:61) @ org.springframework.test.context.junit4.statements.runaftertestclasscallbacks.evaluate(runaftertestclasscallbacks.java:71) @ org.junit.runners.parentrunner.run(parentrunner.java:309) @ org.springframework.test.context.junit4.springjunit4classrunner.run(springjunit4classrunner.java:176) @ org.eclipse.jdt.internal.junit4.runner.junit4testreference.run(junit4testreference.java:50) @ org.eclipse.jdt.internal.junit.runner.testexecution.run(testexecution.java:38) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:459) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:675) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.run(remotetestrunner.java:382) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.main(remotetestrunner.java:192)
in postgres config (/etc/postgresql/9.2/main/postgresql.conf) value set 100 shown: max_connections = 100
the 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 configure c3p0 /* * @repeat annotation springframework.test @ test connecting test database, psqlexception * many clients can reproduced 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; } }
the dbunit database connection:
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; } }
thinks tried:
change setmaxpoolsize c3p0 (from 1 100)
increase max_connections in postgres config (postgres didn't start anymore)
add thread.sleep(1000); @before , @before (1 10 seconds)
if run test enough times work, when run subset works of time.
i'm convinced 100 database connections should enough 300 tests...
i found problem in dbunit database connection. created new connection time.
now behaves singleton , problem gone.
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; } }
Comments
Post a Comment