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);
63 public MySQLCachedDataSource(BaseDBConfiguration jdbcElem)
69 protected void configure(BaseDBConfiguration xmlElem) throws DBConfigException
71 BaseDBConfiguration jdbcConfig = (BaseDBConfiguration)xmlElem;
72 if(jdbcConfig.getConnTimeout() > 0){
73 this.CONN_REQ_TIMEOUT = jdbcConfig.getConnTimeout();
75 if(jdbcConfig.getRequestTimeout() > 0){
76 this.DATA_REQ_TIMEOUT = jdbcConfig.getRequestTimeout();
79 // set connection pool name
80 String dbConnectionName = jdbcConfig.getDbConnectionName();
81 super.setDbConnectionName(dbConnectionName);
82 // Configure the JDBC connection
83 dbUserId = jdbcConfig.getDbUserId();
86 String errorMsg = "Invalid XML contents: JDBCConnection missing dbUserId attribute";
87 LOGGER.error(AS_CONF_ERROR + errorMsg);
88 throw new DBConfigException(errorMsg);
91 dbPasswd = jdbcConfig.getDbPasswd();
94 String errorMsg = "Invalid XML contents: JDBCConnection missing dbPasswd attribute";
95 LOGGER.error(AS_CONF_ERROR + errorMsg);
96 throw new DBConfigException(errorMsg);
99 dbDriver = jdbcConfig.getDbDriver();
100 if (dbDriver == null)
102 String errorMsg = "Invalid XML contents: JDBCConnection missing dbDriver attribute";
103 LOGGER.error(AS_CONF_ERROR + errorMsg);
104 throw new ScpTblUpdateError(errorMsg);
108 minLimit = Integer.toString(jdbcConfig.getDbMinLimit());
109 if (minLimit == null)
111 String errorMsg = "Invalid XML contents: JDBC Connection missing minLimit attribute";
112 LOGGER.error(AS_CONF_ERROR + errorMsg);
113 throw new DBConfigException(errorMsg);
115 maxLimit = Integer.toString(jdbcConfig.getDbMaxLimit());
116 if (maxLimit == null)
118 String errorMsg = "Invalid XML contents: JDBC Connection missing maxLimit attribute";
119 LOGGER.error(AS_CONF_ERROR + errorMsg);
120 throw new DBConfigException(errorMsg);
122 initialLimit = Integer.toString(jdbcConfig.getDbInitialLimit());
123 if (initialLimit == null)
125 String errorMsg = "Invalid XML contents: JDBC Connection missing initialLimit attribute";
126 LOGGER.error(AS_CONF_ERROR + errorMsg);
127 throw new DBConfigException(errorMsg);
130 dbUrl = jdbcConfig.getDbUrl();
132 String errorMsg = "Invalid XML contents: JDBCConnection missing dbUrl attribute";
133 LOGGER.error(AS_CONF_ERROR + errorMsg);
134 throw new DBConfigException(errorMsg);
139 MysqlDataSource dataSource = new MysqlDataSource();
140 dataSource.setUser(dbUserId);
141 dataSource.setPassword(dbPasswd);
142 dataSource.setURL(dbUrl);
143 // dataSource.setInitialSize(5);
144 // dataSource.setMaxTotal(60);
145 // dataSource.setMaxActive(100);
146 // dataSource.setMaxWait(10000);
147 // dataSource.setMaxIdle(10);
149 Properties connAttr = new Properties();
151 connAttr.setProperty("MinLimit", minLimit);
152 connAttr.setProperty("MaxLimit", maxLimit);
153 connAttr.setProperty("InitialLimit", initialLimit);
154 connAttr.setProperty("TRANSACTION_ISOLATION","SERIALIZABLE");
155 connAttr.setProperty("CONNECTION_TAG", dbConnectionName.toUpperCase()+"_CONNECTION");
156 connAttr.setProperty("InactivityTimeout", "900");
157 connAttr.setProperty("AbandonedConnectionTimeout", "600");
158 connAttr.setProperty("PropertyCheckInterval", "60");
159 connAttr.setProperty("ValidateConnection", "true");
164 this.ds = dataSource;
167 LOGGER.info("MySQLDataSource <"+dbConnectionName+"> configured successfully. Using URL: "+dbUrl);
170 // } catch (SQLException exc) {
171 // initialized = false;
172 // StringBuffer sb = new StringBuffer();
173 // sb.append("Failed to initialize MySQLDataSource<");
174 // sb.append(dbConnectionName).append(">. Reason: ");
175 // sb.append(exc.getMessage());
176 // LOGGER.error("AS_CONF_ERROR: " + sb.toString());
177 //// throw new DBConfigException(e.getMessage());
178 } catch (Exception exc) {
180 StringBuffer sb = new StringBuffer();
181 sb.append("Failed to initialize MySQLCachedDataSource <");
182 sb.append(dbConnectionName).append(">. Reason: ");
183 sb.append(exc.getMessage());
184 LOGGER.error("AS_CONF_ERROR: " + sb.toString());
185 // throw new DBConfigException(e.getMessage());
189 public final String getDbUrl()
194 public final String getDbUserId()
199 public final String getDbPasswd()
204 public static MySQLCachedDataSource createInstance(BaseDBConfiguration config) /*throws Exception*/ {
205 return new MySQLCachedDataSource(config);
208 public String toString(){
209 return getDbConnectionName();
212 public java.util.logging.Logger getParentLogger()
213 throws SQLFeatureNotSupportedException {
214 // TODO Auto-generated method stub