aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Golabek <tomasz.golabek@nokia.com>2018-10-12 12:20:36 +0200
committerTomasz Golabek <tomasz.golabek@nokia.com>2018-10-17 11:15:16 +0200
commit0d01dc7f9cea83d9b70a021ac52749045cfbf6ad (patch)
tree631718967879dcf1ffef4fd7b5cf18c3f4d9eae4
parent74d55c254031a25f06f106291e8506eb53bd813d (diff)
Retrying for connection when postgres is not ready
Added 5 attempts for establishing connection to the postgres. Needed when postgres havent start before buscontroller container. Change-Id: Ie838161d1ecc63acceb5e5068dffb0dd0092440f Issue-ID: DMAAP-828 Signed-off-by: Tomasz Golabek <tomasz.golabek@nokia.com>
-rw-r--r--pom.xml2
-rw-r--r--src/main/java/org/onap/dmaap/dbcapi/database/ConnectionFactory.java17
-rw-r--r--version.properties2
3 files changed, 19 insertions, 2 deletions
diff --git a/pom.xml b/pom.xml
index 10c5db8..64fd408 100644
--- a/pom.xml
+++ b/pom.xml
@@ -381,7 +381,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jettyVersion>9.3.8.RC0</jettyVersion>
<eelf.version>0.0.1</eelf.version>
- <artifact.version>1.0.15-SNAPSHOT</artifact.version>
+ <artifact.version>1.0.16-SNAPSHOT</artifact.version>
<!-- SONAR -->
<jacoco.version>0.7.7.201606060606</jacoco.version>
<sonar-jacoco-listeners.version>3.2</sonar-jacoco-listeners.version>
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);
+ }
+ }
}
diff --git a/version.properties b/version.properties
index 815d79c..5b5f570 100644
--- a/version.properties
+++ b/version.properties
@@ -27,7 +27,7 @@
major=1
minor=0
-patch=15
+patch=16
base_version=${major}.${minor}.${patch}
# Release must be completed with git revision # in Jenkins