diff options
author | varun gudisena <vg411h@att.com> | 2018-10-17 14:02:55 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2018-10-17 14:02:55 +0000 |
commit | 7a368c449d32a779157332ca7d06219efaf4fc4d (patch) | |
tree | 2e0b5842cddf2c65ba35561613958e5a83f5b010 /src/main/java/org | |
parent | 378d55bf699e9a3d6c5a1f1a5e47bdbde9594abc (diff) | |
parent | 0d01dc7f9cea83d9b70a021ac52749045cfbf6ad (diff) |
Merge "Retrying for connection when postgres is not ready"1.0.16
Diffstat (limited to 'src/main/java/org')
-rw-r--r-- | src/main/java/org/onap/dmaap/dbcapi/database/ConnectionFactory.java | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/src/main/java/org/onap/dmaap/dbcapi/database/ConnectionFactory.java b/src/main/java/org/onap/dmaap/dbcapi/database/ConnectionFactory.java index aee3ff1..0f14e60 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/database/ConnectionFactory.java +++ b/src/main/java/org/onap/dmaap/dbcapi/database/ConnectionFactory.java @@ -26,6 +26,7 @@ import java.util.*; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; +import java.util.concurrent.TimeUnit; import org.onap.dmaap.dbcapi.util.DmaapConfig; public class ConnectionFactory { @@ -36,6 +37,7 @@ public class ConnectionFactory { static final EELFLogger errorLogger = EELFManager.getInstance().getErrorLogger(); static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger(); static final EELFLogger serverLogger = EELFManager.getInstance().getServerLogger(); + static final int PREPARE_PSQL_CONNECTION_ATTEMPTS = 5; static { try { @@ -75,6 +77,14 @@ public class ConnectionFactory { Properties p = new Properties(); p.put("user", dbuser); p.put("password", dbcr); + for (int i=1; i<PREPARE_PSQL_CONNECTION_ATTEMPTS; i++){ + try{ + return(DriverManager.getConnection("jdbc:postgresql://" + host + "/" + dbname, p)); + }catch(SQLException e){ + logger.error("Unable to connect to the postgres server. " + i + "attempt failed. ", e); + waitFor(1); + } + } return(DriverManager.getConnection("jdbc:postgresql://" + host + "/" + dbname, p)); } public String getSchema() { @@ -93,4 +103,11 @@ public class ConnectionFactory { logger.error("Error", e); } } + private void waitFor(long seconds){ + try { + TimeUnit.SECONDS.sleep(seconds); + } catch (InterruptedException e) { + logger.debug("Waiting interrupted. ", e); + } + } } |