Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker compose - Databases not automatically created (Workaround) #64

Closed
valentinajemuovic opened this issue Dec 30, 2022 · 1 comment
Closed
Assignees
Labels
bug Something isn't working wontfix This will not be worked on

Comments

@valentinajemuovic
Copy link
Owner

valentinajemuovic commented Dec 30, 2022

This is a local issue that appeared on some local machines, pending to find full solution.

When you run:

./gradlew integrationTest

You see the following error:

> Task :integrationTest

BankingApplicationTests > contextLoads() FAILED
    java.lang.IllegalStateException at DefaultCacheAwareContextLoaderDelegate.java:132
        Caused by: org.springframework.beans.factory.BeanCreationException at AbstractAutowireCapableBeanFactory.java:1804
            Caused by: javax.persistence.PersistenceException at AbstractEntityManagerFactoryBean.java:421
                Caused by: org.hibernate.exception.GenericJDBCException at StandardSQLExceptionConverter.java:42
                    Caused by: org.postgresql.util.PSQLException at QueryExecutorImpl.java:2675

JpaBankAccountStorageTest > should_return_empty_given_non_existent_account_number() FAILED
    org.junit.jupiter.api.extension.ParameterResolutionException at ExecutableInvoker.java:239
        Caused by: java.lang.IllegalStateException at DefaultCacheAwareContextLoaderDelegate.java:132
            Caused by: org.springframework.beans.factory.BeanCreationException at AbstractAutowireCapableBeanFactory.java:1804
                Caused by: javax.persistence.PersistenceException at AbstractEntityManagerFactoryBean.java:421
                    Caused by: org.hibernate.exception.GenericJDBCException at StandardSQLExceptionConverter.java:42
                        Caused by: org.postgresql.util.PSQLException at QueryExecutorImpl.java:2675

JpaBankAccountStorageTest > should_return_added_bank_account() FAILED
    org.junit.jupiter.api.extension.ParameterResolutionException at ExecutableInvoker.java:239
        Caused by: java.lang.IllegalStateException at DefaultCacheAwareContextLoaderDelegate.java:132
            Caused by: org.springframework.beans.factory.BeanCreationException at AbstractAutowireCapableBeanFactory.java:1804
                Caused by: javax.persistence.PersistenceException at AbstractEntityManagerFactoryBean.java:421
                    Caused by: org.hibernate.exception.GenericJDBCException at StandardSQLExceptionConverter.java:42
                        Caused by: org.postgresql.util.PSQLException at QueryExecutorImpl.java:2675

JpaBankAccountStorageTest > should_find_multiple_added_bank_accounts() FAILED
    org.junit.jupiter.api.extension.ParameterResolutionException at ExecutableInvoker.java:239
        Caused by: java.lang.IllegalStateException at DefaultCacheAwareContextLoaderDelegate.java:132
            Caused by: org.springframework.beans.factory.BeanCreationException at AbstractAutowireCapableBeanFactory.java:1804
                Caused by: javax.persistence.PersistenceException at AbstractEntityManagerFactoryBean.java:421
                    Caused by: org.hibernate.exception.GenericJDBCException at StandardSQLExceptionConverter.java:42
                        Caused by: org.postgresql.util.PSQLException at QueryExecutorImpl.java:2675

BankAccountControllerSystemTest > should_open_account_given_valid_request() FAILED
    org.junit.jupiter.api.extension.ParameterResolutionException at ExecutableInvoker.java:239
        Caused by: java.lang.IllegalStateException at DefaultCacheAwareContextLoaderDelegate.java:132
            Caused by: org.springframework.beans.factory.BeanCreationException at AbstractAutowireCapableBeanFactory.java:1804
                Caused by: javax.persistence.PersistenceException at AbstractEntityManagerFactoryBean.java:421
                    Caused by: org.hibernate.exception.GenericJDBCException at StandardSQLExceptionConverter.java:42
                        Caused by: org.postgresql.util.PSQLException at QueryExecutorImpl.java:2675

22 tests completed, 5 failed

> Task :integrationTest FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':integrationTest'.
> There were failing tests. See the report at: file:///C:/Users/valen/GitHub/valentinacupac/banking-kata-java/build/reports/tests/integrationTest/index.html

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 23s
8 actionable tasks: 8 executed

You open up the detailed file (e.g. file:///C:/Users/valen/GitHub/valentinacupac/banking-kata-java/build/reports/tests/integrationTest/index.html above).
You open up the error log for BankingApplicationTests. contextLoads()

As you scroll to the bottom, you see the underlying cause is FATAL: database "banking_kata_db" does not exist:

java.lang.IllegalStateException: Failed to load ApplicationContext
	at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132)
	at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:124)
	at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190)
    ...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.GenericJDBCException: Unable to open JDBC Connection for DDL execution
	at app//org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804)
	at app//org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620)
	at app//org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
    ...
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.GenericJDBCException: Unable to open JDBC Connection for DDL execution
	at app//org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:421)
	at app//org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396)
	at app//org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341)
    ...
Caused by: org.hibernate.exception.GenericJDBCException: Unable to open JDBC Connection for DDL execution
	at app//org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
	at app//org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:113)
	at app//org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:99)
    ...
Caused by: org.postgresql.util.PSQLException: FATAL: database "banking_kata_db" does not exist
	at app//org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2675)
	at app//org.postgresql.core.v3.QueryExecutorImpl.readStartupMessages(QueryExecutorImpl.java:2787)
	at app//org.postgresql.core.v3.QueryExecutorImpl.<init>(QueryExecutorImpl.java:173)
    ...

Root Cause

The root cause is that for some reason, on some local machines, the docker-entrypoint-initdb.d seems not to be executed in docker.compose.yaml
This issue was only noted on some local machines, but on GitHub Actions everything works well.

Workaround:

Open up Docker Desktop, go into the Docker container postgres and open up its CLI.

Then input the following:

psql -U postgres;
\l
CREATE DATABASE banking_kata_db OWNER postgres;
CREATE DATABASE keycloak_db OWNER postgres;
\l

Then re-run the integration tests and verify they pass:

./gradlew integrationTest

If you experienced the issue we described in this ticket, please comment below.
If you tried the workaround, please let us know whether or not it worked for you.
If you tried and found any other solutions, please comment too.

@valentinajemuovic valentinajemuovic added the bug Something isn't working label Dec 30, 2022
@valentinajemuovic valentinajemuovic changed the title Docker compose - Databases not automatically created Docker compose - Databases not automatically created (Workaround) Dec 30, 2022
@valentinajemuovic valentinajemuovic added the wontfix This will not be worked on label Dec 30, 2022
@valentinajemuovic valentinajemuovic self-assigned this Dec 30, 2022
@valentinajemuovic
Copy link
Owner Author

If you experienced the issue we described in this ticket, please comment below.
If you tried the workaround, please let us know whether or not it worked for you.
If you tried and found any other solutions, please comment too.

Currently we're not sure if this is a one-off issue or if others are experiencing it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

1 participant