Spring Batch - 在某些单元测试中拒绝连接

时间:2012-01-31 22:19:49

标签: junit database-connection spring-batch

如果我一起运行所有(31)单元测试,我会在2 - 3单元测试中得到以下连接错误,但是如果我单独运行这些测试,则运行成功。你能指导我这个奇怪的行为吗?我使用的是Spring 3.0.6,Spring batch 2.1.7和Oracle XE数据库。

以下是错误

    java.lang.IllegalStateException: Failed to load ApplicationContext   
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:308)   
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)   
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)   
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)   
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)   
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)   
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)   
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)   
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)   
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)   
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)   
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)   
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)   
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)   
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)   
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)   
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)   
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)   
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)   
    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.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)   
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)   
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192)   
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)   
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobRepository' defined in class path resource [simple-job-launcher-context.xml]: Invocation of init method failed; nested exception is org.springframework.jdbc.support.MetaDataAccessException: Could not get Connection for extracting meta data; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection   
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1420)   
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)   
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)   
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)   
    ... 40 more   
    Caused by: org.springframework.jdbc.support.MetaDataAccessException: Could not get Connection for extracting meta data; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection   
    at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:293)   
    at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:320)   
    ... 47 more   
Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection   
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80)   
    at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:280)   
    ... 52 more   
    Caused by: java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection   
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:419)   
    at oracle.jdbc.driver.PhysicalConnection.(PhysicalConnection.java:536)   
              .............   
    at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:840)   
    at org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:96)   
    at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)   
    at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)   
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)   
    ... 53 more   
    Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection   
    at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:375)   
    ..............   
    at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1054)   
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:308)   
    ... 64 more   
Caused by: java.net.ConnectException: Connection refused: connect   
    at java.net.PlainSocketImpl.socketConnect(Native Method)   
                 ............................   
    at oracle.net.nt.ConnOption.connect(ConnOption.java:123)   
    at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:353)   
    ... 69 more

2 个答案:

答案 0 :(得分:1)

之前发生过这样的事情。当我将数据库的IP从127.0.0.1更改为192.168.1.27(在我的网络中我的计算机的IP,它可以在windows中执行ip config或在linux中执行ifconfig)时解决了。它可能不是一个永久的解决方案,但它会躲过一劫。

答案 1 :(得分:0)

只是有同样的问题: 根本原因: 所有可用的连接已花费

解决方案: 使用后关闭连接。