title: Spring Batch - Connection Refused in some Unit Tests
body: <p>If I run all(31) the unit test together, I get following connection error in 2 - 3 unit tests, however if I run those tests individually, the run successfully. Can you please guide me on this strange behaviour. I am using Spring 3.0.6, Spring batch 2.1.7 and Oracle XE database. </p>
  
  <p>below is the error </p>
  
  <pre>
      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
  </pre>