[Frequently] Unable to acquire JDBC Connection - Keycloak Deployed on docker connected to Postgres

I have Keycloak “standalone” deployed using docker [keycloak:12.0.4] connected to postgres [postgres:13.2]

Recently I’m getting this error frequently and randomly and users will be locked out from Keycloak until we restart the keycloak container. I would like to know what is the root cause for this issue so I can handle it properly.
This happens just during normal operation we didn’t restart or upgrade keycloak prior to this issue.

Logs:

**"Keycloak" SQL Error: 0, SQLState: null ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-388) javax.resource.ResourceException: Unable to get managed connection for java:jboss/datasources/KeycloakDS [org.hibernate.event.internal.DefaultLoadEventListener] Error performing load command : org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection.**

the Datasources block in standalone.xml:

    <datasources>
        <datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true" statistics-enabled="${wildfly.datasources.statistics-enabled:${wildfly.statistics-enabled:false}}">
            <connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE</connection-url>
            <driver>h2</driver>
            <security>
                <user-name>user</user-name>
                <password>pass</password>
            </security>
        </datasource>
        <datasource jndi-name="java:jboss/datasources/KeycloakDS" pool-name="KeycloakDS" enabled="true" use-java-context="true" use-ccm="true">
            <connection-url>jdbc:postgresql://${env.DB_ADDR:postgres}/${env.DB_DATABASE:keycloak}${env.JDBC_PARAMS:}</connection-url>
            <driver>postgresql</driver>
            <pool>
                <flush-strategy>IdleConnections</flush-strategy>
            </pool>
            <security>
                <user-name>${env.DB_USER:user}</user-name>
                <password>${env.DB_PASSWORD:pass}</password>
            </security>
            <validation>
                <check-valid-connection-sql>SELECT 1</check-valid-connection-sql>
                <background-validation>true</background-validation>
                <background-validation-millis>60000</background-validation-millis>
            </validation>
        </datasource>
        <drivers>
            <driver name="h2" module="com.h2database.h2">
                <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
            </driver>
            <driver name="postgresql" module="org.postgresql.jdbc">
                <xa-datasource-class>org.postgresql.xa.PGXADataSource</xa-datasource-class>
            </driver>
        </drivers>
    </datasources>

Any idea what should be changed and how to resolve this?

Thanks

1 Like

Hey!

Did you solve the issue?
I have the same problem…

Thanks

1 Like