2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2016 - 2017 AT&T
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdnc.sli.resource.dblib.jdbc;
23 import java.sql.SQLFeatureNotSupportedException;
24 import java.util.Properties;
26 import org.openecomp.sdnc.sli.resource.dblib.CachedDataSource;
27 import org.openecomp.sdnc.sli.resource.dblib.DBConfigException;
28 import org.openecomp.sdnc.sli.resource.dblib.config.BaseDBConfiguration;
29 import org.openecomp.sdnc.sli.resource.dblib.config.JDBCConfiguration;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
39 * @version $Revision: 1.7 $
41 * Author Date Comments
42 * ============== ======== ====================================================
46 public class MySQLCachedDataSource extends CachedDataSource
48 private String dbUserId;
49 private String dbPasswd;
52 private String minLimit;
53 private String maxLimit;
54 private String initialLimit;
56 private static final String AS_CONF_ERROR = "AS_CONF_ERROR: ";
58 private static Logger LOGGER = LoggerFactory.getLogger(MySQLCachedDataSource.class);
66 public MySQLCachedDataSource(BaseDBConfiguration jdbcElem)
72 protected void configure(BaseDBConfiguration xmlElem) throws DBConfigException
74 BaseDBConfiguration jdbcConfig = (BaseDBConfiguration)xmlElem;
75 if(jdbcConfig.getConnTimeout() > 0){
76 this.CONN_REQ_TIMEOUT = jdbcConfig.getConnTimeout();
78 if(jdbcConfig.getRequestTimeout() > 0){
79 this.DATA_REQ_TIMEOUT = jdbcConfig.getRequestTimeout();
82 // set connection pool name
83 String dbConnectionName = jdbcConfig.getDbConnectionName();
84 super.setDbConnectionName(dbConnectionName);
85 // Configure the JDBC connection
86 dbUserId = jdbcConfig.getDbUserId();
89 String errorMsg = "Invalid XML contents: JDBCConnection missing dbUserId attribute";
90 LOGGER.error(AS_CONF_ERROR + errorMsg);
91 throw new DBConfigException(errorMsg);
94 dbPasswd = jdbcConfig.getDbPasswd();
97 String errorMsg = "Invalid XML contents: JDBCConnection missing dbPasswd attribute";
98 LOGGER.error(AS_CONF_ERROR + errorMsg);
99 throw new DBConfigException(errorMsg);
102 dbDriver = jdbcConfig.getDbDriver();
103 if (dbDriver == null)
105 String errorMsg = "Invalid XML contents: JDBCConnection missing dbDriver attribute";
106 LOGGER.error(AS_CONF_ERROR + errorMsg);
107 throw new ScpTblUpdateError(errorMsg);
111 minLimit = Integer.toString(jdbcConfig.getDbMinLimit());
112 if (minLimit == null)
114 String errorMsg = "Invalid XML contents: JDBC Connection missing minLimit attribute";
115 LOGGER.error(AS_CONF_ERROR + errorMsg);
116 throw new DBConfigException(errorMsg);
118 maxLimit = Integer.toString(jdbcConfig.getDbMaxLimit());
119 if (maxLimit == null)
121 String errorMsg = "Invalid XML contents: JDBC Connection missing maxLimit attribute";
122 LOGGER.error(AS_CONF_ERROR + errorMsg);
123 throw new DBConfigException(errorMsg);
125 initialLimit = Integer.toString(jdbcConfig.getDbInitialLimit());
126 if (initialLimit == null)
128 String errorMsg = "Invalid XML contents: JDBC Connection missing initialLimit attribute";
129 LOGGER.error(AS_CONF_ERROR + errorMsg);
130 throw new DBConfigException(errorMsg);
133 dbUrl = jdbcConfig.getDbUrl();
135 String errorMsg = "Invalid XML contents: JDBCConnection missing dbUrl attribute";
136 LOGGER.error(AS_CONF_ERROR + errorMsg);
137 throw new DBConfigException(errorMsg);
142 MysqlDataSource dataSource = new MysqlDataSource();
143 dataSource.setUser(dbUserId);
144 dataSource.setPassword(dbPasswd);
145 dataSource.setURL(dbUrl);
146 // dataSource.setInitialSize(5);
147 // dataSource.setMaxTotal(60);
148 // dataSource.setMaxActive(100);
149 // dataSource.setMaxWait(10000);
150 // dataSource.setMaxIdle(10);
152 Properties connAttr = new Properties();
154 connAttr.setProperty("MinLimit", minLimit);
155 connAttr.setProperty("MaxLimit", maxLimit);
156 connAttr.setProperty("InitialLimit", initialLimit);
157 connAttr.setProperty("TRANSACTION_ISOLATION","SERIALIZABLE");
158 connAttr.setProperty("CONNECTION_TAG", dbConnectionName.toUpperCase()+"_CONNECTION");
159 connAttr.setProperty("InactivityTimeout", "900");
160 connAttr.setProperty("AbandonedConnectionTimeout", "600");
161 connAttr.setProperty("PropertyCheckInterval", "60");
162 connAttr.setProperty("ValidateConnection", "true");
167 this.ds = dataSource;
170 LOGGER.info("MySQLDataSource <"+dbConnectionName+"> configured successfully. Using URL: "+dbUrl);
173 // } catch (SQLException exc) {
174 // initialized = false;
175 // StringBuffer sb = new StringBuffer();
176 // sb.append("Failed to initialize MySQLDataSource<");
177 // sb.append(dbConnectionName).append(">. Reason: ");
178 // sb.append(exc.getMessage());
179 // LOGGER.error("AS_CONF_ERROR: " + sb.toString());
180 //// throw new DBConfigException(e.getMessage());
181 } catch (Exception exc) {
183 StringBuffer sb = new StringBuffer();
184 sb.append("Failed to initialize MySQLCachedDataSource <");
185 sb.append(dbConnectionName).append(">. Reason: ");
186 sb.append(exc.getMessage());
187 LOGGER.error("AS_CONF_ERROR: " + sb.toString());
188 // throw new DBConfigException(e.getMessage());
192 public final String getDbUrl()
197 public final String getDbUserId()
202 public final String getDbPasswd()
207 public static MySQLCachedDataSource createInstance(BaseDBConfiguration config) /*throws Exception*/ {
208 return new MySQLCachedDataSource(config);
211 public String toString(){
212 return getDbConnectionName();
215 public java.util.logging.Logger getParentLogger()
216 throws SQLFeatureNotSupportedException {
217 // TODO Auto-generated method stub