title: Handle database down in Spring Boot
body: <p>I would like to test on my system if the database is down with Spring Boot. I have written a rest call for testing. When the database is up, it return "test" which is correct result. But the database is down, it return nothing.
  The Goal is to make it to return "test" even the database is down. Below is the error</p>
  
  <pre><code>2020-04-27 17:27:00.003 ERROR 9536     [] --- [pool-1-thread-1] o.s.s.s.TaskUtils$LoggingErrorHandler    : Unexpected error occurred in scheduled task.
  org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection
          at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:450)
          at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:378)
          at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:137)
          at org.springframework.session.jdbc.JdbcOperationsSessionRepository.cleanUpExpiredSessions(JdbcOperationsSessionRepository.java:589)
          at org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessionConfiguration.lambda$configureTasks$0(JdbcHttpSessionConfiguration.java:194)
          at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
          at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93)
          at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
          at java.util.concurrent.FutureTask.run(FutureTask.java:266)
          at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
          at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
          at java.lang.Thread.run(Thread.java:748)
  Caused by: org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection
          at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:48)
          at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
          at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)
          at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97)
          at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.acquireConnectionIfNeeded(LogicalConnectionManagedImpl.java:109)
          at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getPhysicalConnection(LogicalConnectionManagedImpl.java:136)
          at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getConnectionForTransactionManagement(LogicalConnectionManagedImpl.java:254)
          at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.begin(LogicalConnectionManagedImpl.java:262)
          at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.begin(JdbcResourceLocalTransactionCoordinatorImpl.java:214)
          at org.hibernate.engine.transaction.internal.TransactionImpl.begin(TransactionImpl.java:56)
          at org.springframework.orm.jpa.vendor.HibernateJpaDialect.beginTransaction(HibernateJpaDialect.java:162)
          at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:400)
          ... 13 common frames omitted
  Caused by: java.sql.SQLTransientConnectionException: HikariCP - Connection is not available, request timed out after 60000ms.
          at com.zaxxer.hikari.pool.HikariPool.createTimeoutException(HikariPool.java:669)
          at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:183)
          at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:148)
          at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:128)
          at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122)
          at org.hibernate.internal.NonContextualJdbcConnectionAccess.obtainConnection(NonContextualJdbcConnectionAccess.java:35)
          at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.acquireConnectionIfNeeded(LogicalConnectionManagedImpl.java:106)
          ... 20 common frames omitted
  Caused by: org.postgresql.util.PSQLException: Connection to X:X:X:X:xxxx refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
          at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:280)
          at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
          at org.postgresql.jdbc.PgConnection.&lt;init&gt;(PgConnection.java:195)
          at org.postgresql.Driver.makeConnection(Driver.java:454)
          at org.postgresql.Driver.connect(Driver.java:256)
          at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:117)
          at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:123)
          at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:365)
          at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:194)
          at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:460)
          at com.zaxxer.hikari.pool.HikariPool.access$100(HikariPool.java:71)
          at com.zaxxer.hikari.pool.HikariPool$PoolEntryCreator.call(HikariPool.java:699)
          at com.zaxxer.hikari.pool.HikariPool$PoolEntryCreator.call(HikariPool.java:685)
          at java.util.concurrent.FutureTask.run(FutureTask.java:266)
          ... 3 common frames omitted
  Caused by: java.net.ConnectException: Connection refused (Connection refused)
          at java.net.PlainSocketImpl.socketConnect(Native Method)
          at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
          at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
          at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
          at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
          at java.net.Socket.connect(Socket.java:589)
          at org.postgresql.core.PGStream.&lt;init&gt;(PGStream.java:70)
          at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:91)
          at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:192)
          ... 16 common frames omitted
  </code></pre>
  
  <p>And the test controller is in below</p>
  
  <pre><code>@RestController
  public class TestController {
      @GetMapping(value = "/test")
      public String test() {
          System.out.println("test");
          return "Test";
      }
  
  }
  </code></pre>
  
  <p>the <code>application.properties</code> is like below</p>
  
  <pre><code>spring.datasource.hikari.maximumPoolSize=10
  spring.datasource.hikari.idleTimeout=50000
  spring.datasource.hikari.maxLifetime=2000000
  spring.datasource.hikari.connectionTimeout=60000
  spring.datasource.continue-on-error=true
  spring.datasource.initialization-mode=never
  spring.datasource.hikari.pool-name=KlmsHikariCP
  spring.datasource.hikari.connection-test-query=SELECT 1
  spring.datasource.hikari.initialization-fail-timeout= -1
  spring.datasource.hikari.isolate-internal-queries=true
  spring.datasource.hikari.allow-pool-suspension=true
  spring.datasource.test-while-idle=true
  spring.datasource.test-on-borrow=true
  spring.datasource.test-on-return=false
  spring.datasource.validation-interval=40000
  spring.datasource.validation-query=SELECT 1 FROM DUAL
  spring.datasource.time-between-eviction-runs-millis=5000
  spring.datasource.min-evictable-idle-time-millis=5000
  spring.datasource.remove-abandoned=true
  spring.datasource.remove-abandoned-timeout=3
  spring.datasource.log-abandoned=true
  spring.datasource.log-validation-errors=true
  
  
  ################### Hibernate Configuration ##########################
  spring.jpa.hibernate.ddl-auto=none
  spring.jpa.show-sql=false
  spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=true
  spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
  </code></pre>