Error: Could not shut down embedded database: The database is open in exclusive mode; can not open additional connections [90135-200]

Viewed 1610

I am using H2 DB for my JUnit testing and getting this error in my JUNIT test cases -

INFO [extShutdownHook] o.s.j.d.e.H2EmbeddedDatabaseConfigurer o.s.j.d.e.AbstractEmbeddedDatabaseConfigurer.shutdown(AbstractEmbeddedDatabaseConfigurer.java:53) - ||||||||||||||--|Could not shut down embedded database: org.h2.jdbc.JdbcSQLNonTransientConnectionException: The database is open in exclusive mode; can not open additional connections [90135-200]

My DB Set up file looks like this -

@Configuration
@ComponentScan(basePackages={"com.sample.repository"})

public class SampleDBConfig {

JdbcTemplate jdbcTemplate = null;
DataSource dataSource = null;

@Bean
public JdbcTemplate jdbcTemplate()
{
if(jdbcTemplate == null){
    LOGGER.info("JdbcTemplate is null, so calling to create ");
    jdbcTemplate = new JdbcTemplate(getDataSource());
}else{
    LOGGER.info("JdbcTemplate is already set");
}
return jdbcTemplate;
}


@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
     return new PropertySourcesPlaceholderConfigurer();
}

@Bean
DataSource getDataSource() {
  EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
  if(dataSource == null){
    dataSource = builder
            .setType(EmbeddedDatabaseType.H2)
            .addScript("classpath:folder/initial-setup.sql")
            .build();
}
return dataSource;
}

}

One of my JUNIT test class looks like this -

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SampleDBConfig.class)
public class XyxImplTest{

@Autowired
ClassInTestImpl classInTestImpl ;

@Test
public void testMethod(){
.......
}
}

And one of the classes am testing are like this -

@Component
@Transactional(rollbackFor = Exception.class)
public class ClassInTestImpl implements ClassInTest {

@Autowired
private JdbcTemplate jdbcTemplate;
.....

Am using SpringJUnit4ClassRunner.class for my JUNIT tests and invoking this DB set up class from each JUNIT test class using @ContextConfiguration(classes = SampleDBConfig.class). I do not want to execute the JUNIT tests sequentially, is there any way we can resolve this issue?

0 Answers
Related