* org.onap.dmaap
* ================================================================================
* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2019 IBM.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
import java.sql.*;
import java.util.*;
-import org.apache.log4j.Logger;
-
import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
-import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
+import java.util.concurrent.TimeUnit;
import org.onap.dmaap.dbcapi.util.DmaapConfig;
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 {
Properties p = DmaapConfig.getConfig();
host = p.getProperty("DB.host", "dcae-pstg-write-ftl.domain.notset.com");
dbname = p.getProperty("DB.name", "dmaap");
- dbuser = p.getProperty("DB.user", "dmaap_admin");
- dbcr = p.getProperty("DB.cred", "test234-ftl");
+ dbuser = getValue(p, "DB.user", "dmaap_admin");
+ dbcr = getValue(p, "DB.cred", "test234-ftl");
schema = p.getProperty("DB.schema", "public");
}
+
+ private static String getValue(final Properties props, final String value, final String defaultValue) {
+ String prop = props.getProperty(value, defaultValue);
+ if (prop != null && prop.matches("[$][{].*[}]$")) {
+ return System.getenv(prop.substring(2, prop.length() - 1));
+ }
+ return prop;
+ }
+
public static ConnectionFactory getDefaultInstance() {
return(instance);
}
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() {
return;
}
}
- try { c.close(); } catch (Exception e) {}
+ try {
+ c.close();
+ } catch (Exception e) {
+ logger.error("Error", e);
+ }
+ }
+ private void waitFor(long seconds){
+ try {
+ TimeUnit.SECONDS.sleep(seconds);
+ } catch (InterruptedException e) {
+ logger.debug("Waiting interrupted. ", e);
+ Thread.currentThread().interrupt();
+ }
}
}