I have a setup where Spring-Boot, meaning Spring-Data in the end, manages all my database connections with the default settings. No problems there. For integration-tests I overrode the DataSource, like this:
@Configuration
public class DataSourceConfiguration {
@Bean
public DataSource dataSource() { return new ExtendedHikariDataSource(); }
// If you override dataSource, Spring needs the 3 other Beans to be provided too:
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
Map<String, String> jpaProperties = new HashMap<>();
jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
jpaProperties.put(TransactionSettings.AUTO_CLOSE_SESSION, "false");
jpaProperties.put(MappingSettings.PHYSICAL_NAMING_STRATEGY ...);
...
return new EntityManagerFactoryBuilder(new HibernateJpaVendorAdapter(),
jpaProperties, null)
.dataSource(dataSource())
.packages("org.mypackage")
.build();
}
@Bean
public JdbcTemplate jdbcTemplate() {
return new JdbcTemplate(dataSource());
}
@Bean
public PlatformTransactionManager transactionManager() {
return new JpaTransactionManager(entityManagerFactory().getObject());
}
}
The problem is now, that Spring closes the database connection after almost all operations, even a simple save. Like this:
...
// Standard Spring "CrudRepository".
@Autowired private MyCrudRepository myCrudRepository;
@Test
void testSave() {
myCrudRepository.save(myObject);
// Connection gets closed. Instantly, no timeouts or anything.
}
Now I constantly run into exceptions being thrown, because the connection got closed. What setting do I have to set, to tell Spring / Hibernate / JPA / etc. that I want the “normal” behavior?
11