[SDNC-5] Rebase sdnc-core
[sdnc/core.git] / dblib / common / src / main / java / org / apache / tomcat / jdbc / pool / interceptor / ConnectionState.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openecomp
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 /*
22  * Licensed to the Apache Software Foundation (ASF) under one or more
23  * contributor license agreements.  See the NOTICE file distributed with
24  * this work for additional information regarding copyright ownership.
25  * The ASF licenses this file to You under the Apache License, Version 2.0
26  * (the "License"); you may not use this file except in compliance with
27  * the License.  You may obtain a copy of the License at
28  *
29  *      http://www.apache.org/licenses/LICENSE-2.0
30  *
31  * Unless required by applicable law or agreed to in writing, software
32  * distributed under the License is distributed on an "AS IS" BASIS,
33  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34  * See the License for the specific language governing permissions and
35  * limitations under the License.
36  */
37 package org.apache.tomcat.jdbc.pool.interceptor;
38
39 import java.lang.reflect.Method;
40 import java.sql.SQLException;
41
42 import org.apache.juli.logging.Log;
43 import org.apache.juli.logging.LogFactory;
44 import org.apache.tomcat.jdbc.pool.ConnectionPool;
45 import org.apache.tomcat.jdbc.pool.DataSourceFactory;
46 import org.apache.tomcat.jdbc.pool.JdbcInterceptor;
47 import org.apache.tomcat.jdbc.pool.PoolConfiguration;
48 import org.apache.tomcat.jdbc.pool.PooledConnection;
49
50 /**
51  * Interceptor that keep track of connection state to avoid roundtrips to the database.
52  * The {@link org.apache.tomcat.jdbc.pool.ConnectionPool} is optimized to do as little work as possible.
53  * The pool itself doesn't remember settings like {@link java.sql.Connection#setAutoCommit(boolean)},
54  * {@link java.sql.Connection#setReadOnly(boolean)}, {@link java.sql.Connection#setCatalog(String)} or
55  * {@link java.sql.Connection#setTransactionIsolation(int)}. It relies on the application to remember how and when
56  * these settings have been applied.
57  * In the cases where the application code doesn't know or want to keep track of the state, this interceptor helps cache the
58  * state, and it also avoids roundtrips to the database asking for it.
59  *
60  */
61
62 public class ConnectionState extends JdbcInterceptor  {
63     private static final Log log = LogFactory.getLog(ConnectionState.class);
64
65     protected final String[] readState = {"getAutoCommit","getTransactionIsolation","isReadOnly","getCatalog"};
66     protected final String[] writeState = {"setAutoCommit","setTransactionIsolation","setReadOnly","setCatalog"};
67
68     protected Boolean autoCommit = null;
69     protected Integer transactionIsolation = null;
70     protected Boolean readOnly = null;
71     protected String catalog = null;
72
73
74     @Override
75     public void reset(ConnectionPool parent, PooledConnection con) {
76         if (parent==null || con==null) {
77             //we are resetting, reset our defaults
78             autoCommit = null;
79             transactionIsolation = null;
80             readOnly = null;
81             catalog = null;
82             return;
83         }
84         PoolConfiguration poolProperties = parent.getPoolProperties();
85         if (poolProperties.getDefaultTransactionIsolation()!=DataSourceFactory.UNKNOWN_TRANSACTIONISOLATION) {
86             try {
87                 if (transactionIsolation==null || transactionIsolation.intValue()!=poolProperties.getDefaultTransactionIsolation()) {
88                     con.getConnection().setTransactionIsolation(poolProperties.getDefaultTransactionIsolation());
89                     transactionIsolation = Integer.valueOf(poolProperties.getDefaultTransactionIsolation());
90                 }
91             }catch (SQLException x) {
92                 transactionIsolation = null;
93                 log.error("Unable to reset transaction isolation state to connection.",x);
94             }
95         }
96         if (poolProperties.getDefaultReadOnly()!=null) {
97             try {
98                 if (readOnly==null || readOnly.booleanValue()!=poolProperties.getDefaultReadOnly().booleanValue()) {
99                     con.getConnection().setReadOnly(poolProperties.getDefaultReadOnly().booleanValue());
100                     readOnly = poolProperties.getDefaultReadOnly();
101                 }
102             }catch (SQLException x) {
103                 readOnly = null;
104                 log.error("Unable to reset readonly state to connection.",x);
105             }
106         }
107         if (poolProperties.getDefaultAutoCommit()!=null) {
108             try {
109                 if (autoCommit==null || autoCommit.booleanValue()!=poolProperties.getDefaultAutoCommit().booleanValue()) {
110                     con.getConnection().setAutoCommit(poolProperties.getDefaultAutoCommit().booleanValue());
111                     autoCommit = poolProperties.getDefaultAutoCommit();
112                 }
113             }catch (SQLException x) {
114                 autoCommit = null;
115                 log.error("Unable to reset autocommit state to connection.",x);
116             }
117         }
118         if (poolProperties.getDefaultCatalog()!=null) {
119             try {
120                 if (catalog==null || (!catalog.equals(poolProperties.getDefaultCatalog()))) {
121                     con.getConnection().setCatalog(poolProperties.getDefaultCatalog());
122                     catalog = poolProperties.getDefaultCatalog();
123                 }
124             }catch (SQLException x) {
125                 catalog = null;
126                 log.error("Unable to reset default catalog state to connection.",x);
127             }
128         }
129
130     }
131
132
133     @Override
134     public void disconnected(ConnectionPool parent, PooledConnection con, boolean finalizing) {
135         //we are resetting, reset our defaults
136         autoCommit = null;
137         transactionIsolation = null;
138         readOnly = null;
139         catalog = null;
140         super.disconnected(parent, con, finalizing);
141     }
142
143
144
145     @Override
146     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
147         String name = method.getName();
148         boolean read = false;
149         int index = -1;
150         for (int i=0; (!read) && i<readState.length; i++) {
151             read = compare(name,readState[i]);
152             if (read) index = i;
153         }
154         boolean write = false;
155         for (int i=0; (!write) && (!read) && i<writeState.length; i++) {
156             write = compare(name,writeState[i]);
157             if (write) index = i;
158         }
159         Object result = null;
160         if (read) {
161             switch (index) {
162                 case 0:{result = autoCommit; break;}
163                 case 1:{result = transactionIsolation; break;}
164                 case 2:{result = readOnly; break;}
165                 case 3:{result = catalog; break;}
166                 default: // NOOP
167             }
168             //return cached result, if we have it
169             if (result!=null) return result;
170         }
171
172         result = super.invoke(proxy, method, args);
173         if (read || write) {
174             switch (index) {
175                 case 0:{autoCommit = (Boolean) (read?result:args[0]); break;}
176                 case 1:{transactionIsolation = (Integer)(read?result:args[0]); break;}
177                 case 2:{readOnly = (Boolean)(read?result:args[0]); break;}
178                 case 3:{catalog = (String)(read?result:args[0]); break;}
179             }
180         }
181         return result;
182     }
183
184 }