embedded-database-spring-test icon indicating copy to clipboard operation
embedded-database-spring-test copied to clipboard

@PreDestoy is executed after database is shutdown

Open mcherb opened this issue 1 year ago • 1 comments

I've a simple service with a @PreDestoy method which calls a database repository. This PreDestory method is often executed after the database has shutdown !

Scenario: run anything # features/bugdemo.feature:3 Given Create entities # com.example.zonky.cucumber.CucumberTestDefinitions.saveData() Then Count entities equals 3 # com.example.zonky.cucumber.CucumberTestDefinitions.assertDataCount() 2022-06-13 16:20:41.760 INFO 48398 --- [gres:pid(48457)] i.z.t.d.p.embedded.EmbeddedPostgres : LOG: received fast shutdown request 2022-06-13 16:20:41.760 INFO 48398 --- [gres:pid(48457)] i.z.t.d.p.embedded.EmbeddedPostgres : LOG: aborting any active transactions 2022-06-13 16:20:41.760 INFO 48398 --- [gres:pid(48457)] i.z.t.d.p.embedded.EmbeddedPostgres : LOG: autovacuum launcher shutting down 2022-06-13 16:20:41.761 INFO 48398 --- [gres:pid(48457)] i.z.t.d.p.embedded.EmbeddedPostgres : LOG: shutting down 2022-06-13 16:20:41.763 INFO 48398 --- [gres:pid(48457)] i.z.t.d.p.embedded.EmbeddedPostgres : FATAL: the database system is shutting down 2022-06-13 16:20:41.777 INFO 48398 --- [gres:pid(48457)] i.z.t.d.p.embedded.EmbeddedPostgres : LOG: database system is shut down 2022-06-13 16:20:41.778 WARN 48398 --- [ionShutdownHook] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 57P03 2022-06-13 16:20:41.779 ERROR 48398 --- [ionShutdownHook] o.h.engine.jdbc.spi.SqlExceptionHelper : FATAL: the database system is shutting down 2022-06-13 16:20:41.783 WARN 48398 --- [ionShutdownHook] .s.c.a.CommonAnnotationBeanPostProcessor : Destroy method on bean with name 'anyService' threw an exception: org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection 2022-06-13 16:20:41.783 INFO 48398 --- [ionShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default' BUILD SUCCESSFUL in 9s

@Slf4j
@Service
@RequiredArgsConstructor
public class AnyService {

    private final AnyEntityRepository repository;

    @PreDestroy
    public void onDestory() {
        long count = repository.count();
        log.info("Count: [{}]", count);
    }
}

Is there a way to make the database shutdown after all PreDestroy methods have been called ?

You can find a minimal reproduction code here.

I've already opned an issue at zonky-postgres but they redirect me to here :)

mcherb avatar Jun 13 '22 14:06 mcherb

Hello again! Thanks for the reproducer, I really appreciate that. At first glance, the use of the library seems correct. I'll try to figure out what exactly is going on internally and let you know.

tomix26 avatar Jun 14 '22 08:06 tomix26