2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 IBM.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.dmaap.dbcapi.database;
24 import java.sql.Connection;
25 import java.sql.DriverManager;
26 import java.sql.SQLException;
27 import java.util.Properties;
28 import java.util.concurrent.TimeUnit;
29 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
30 import org.onap.dmaap.dbcapi.util.DmaapConfig;
32 public class ConnectionFactory extends BaseLoggingClass {
34 static final int PREPARE_PSQL_CONNECTION_ATTEMPTS = 5;
38 Class.forName("org.postgresql.Driver");
39 } catch (Exception e) {
40 errorLogger.error("Unable to load postgres driver " + e, e);
43 private static ConnectionFactory instance = new ConnectionFactory();
45 private String dbname;
46 private String dbuser;
48 private String schema;
50 public ConnectionFactory() {
51 Properties p = DmaapConfig.getConfig();
52 host = p.getProperty("DB.host", "dcae-pstg-write-ftl.domain.notset.com");
53 dbname = p.getProperty("DB.name", "dmaap");
54 dbuser = getValue(p, "DB.user", "dmaap_admin");
55 dbcr = getValue(p, "DB.cred", "test234-ftl");
56 schema = p.getProperty("DB.schema", "dmaap_admin");
59 private static String getValue(final Properties props, final String value, final String defaultValue) {
60 String prop = props.getProperty(value, defaultValue);
61 if (prop != null && prop.matches("[$][{].*[}]$")) {
62 return System.getenv(prop.substring(2, prop.length() - 1));
67 public static ConnectionFactory getDefaultInstance() {
70 private Connection[] pool = new Connection[5];
72 public Connection get(boolean fresh) throws SQLException {
80 Properties p = new Properties();
81 p.put("user", dbuser);
82 p.put("password", dbcr);
83 for (int i=1; i<PREPARE_PSQL_CONNECTION_ATTEMPTS; i++){
85 return(DriverManager.getConnection("jdbc:postgresql://" + host + "/" + dbname, p));
86 }catch(SQLException e){
87 errorLogger.error("Unable to connect to the postgres server. " + i + "attempt failed. ", e);
91 return(DriverManager.getConnection("jdbc:postgresql://" + host + "/" + dbname, p));
93 public String getSchema() {
96 public void release(Connection c) {
98 if (cur < pool.length) {
105 } catch (Exception e) {
106 errorLogger.error("Error", e);
109 private void waitFor(long seconds){
111 TimeUnit.SECONDS.sleep(seconds);
112 } catch (InterruptedException e) {
113 debugLogger.debug("Waiting interrupted. ", e);
114 Thread.currentThread().interrupt();