2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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.onap.dmaap.dbcapi.aaf.database;
26 public abstract class ConnWrapper<T, U> {
27 protected Connection c;
28 protected PreparedStatement ps;
29 protected ResultSet rs;
30 protected abstract T run(U u) throws Exception;
31 public T protect(ConnectionFactory cf, U u) throws DBException {
34 return(attempt(cf, u, false));
35 } catch (SQLException sqle) {
36 return(attempt(cf, u, true));
38 } catch (RuntimeException rte) {
40 } catch (Exception e) {
41 throw new DBException(e);
44 private T attempt(ConnectionFactory cf, U u, boolean fresh) throws Exception {
55 if (rs != null) { try { rs.close(); } catch (Exception e) {}}
57 if (ps != null) { try { ps.close(); } catch (Exception e) {}}
59 if (c != null) { try { c.close(); } catch (Exception e) {}}