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;
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
30 import java.util.concurrent.TimeUnit;
31 import org.onap.dmaap.dbcapi.util.DmaapConfig;
33 public class ConnectionFactory {
34 static final EELFLogger logger = EELFManager.getInstance().getLogger( ConnectionFactory.class );
35 static final EELFLogger appLogger = EELFManager.getInstance().getApplicationLogger();
36 static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
37 static final EELFLogger debugLogger = EELFManager.getInstance().getDebugLogger();
38 static final EELFLogger errorLogger = EELFManager.getInstance().getErrorLogger();
39 static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
40 static final EELFLogger serverLogger = EELFManager.getInstance().getServerLogger();
41 static final int PREPARE_PSQL_CONNECTION_ATTEMPTS = 5;
45 Class.forName("org.postgresql.Driver");
46 } catch (Exception e) {
47 logger.error("Unable to load postgres driver " + e, e);
50 private static ConnectionFactory instance = new ConnectionFactory();
52 private String dbname;
53 private String dbuser;
55 private String schema;
57 public ConnectionFactory() {
58 Properties p = DmaapConfig.getConfig();
59 host = p.getProperty("DB.host", "dcae-pstg-write-ftl.domain.notset.com");
60 dbname = p.getProperty("DB.name", "dmaap");
61 dbuser = p.getProperty("DB.user", "dmaap_admin");
62 dbcr = p.getProperty("DB.cred", "test234-ftl");
63 schema = p.getProperty("DB.schema", "public");
65 public static ConnectionFactory getDefaultInstance() {
68 private Connection[] pool = new Connection[5];
70 public Connection get(boolean fresh) throws SQLException {
78 Properties p = new Properties();
79 p.put("user", dbuser);
80 p.put("password", dbcr);
81 for (int i=1; i<PREPARE_PSQL_CONNECTION_ATTEMPTS; i++){
83 return(DriverManager.getConnection("jdbc:postgresql://" + host + "/" + dbname, p));
84 }catch(SQLException e){
85 logger.error("Unable to connect to the postgres server. " + i + "attempt failed. ", e);
89 return(DriverManager.getConnection("jdbc:postgresql://" + host + "/" + dbname, p));
91 public String getSchema() {
94 public void release(Connection c) {
96 if (cur < pool.length) {
103 } catch (Exception e) {
104 logger.error("Error", e);
107 private void waitFor(long seconds){
109 TimeUnit.SECONDS.sleep(seconds);
110 } catch (InterruptedException e) {
111 logger.debug("Waiting interrupted. ", e);
112 Thread.currentThread().interrupt();