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=========================================================
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
29 * http://www.apache.org/licenses/LICENSE-2.0
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.
37 package org.apache.tomcat.jdbc.pool.interceptor;
39 import java.lang.reflect.Method;
40 import java.sql.SQLException;
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;
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.
62 public class ConnectionState extends JdbcInterceptor {
63 private static final Log log = LogFactory.getLog(ConnectionState.class);
65 protected final String[] readState = {"getAutoCommit","getTransactionIsolation","isReadOnly","getCatalog"};
66 protected final String[] writeState = {"setAutoCommit","setTransactionIsolation","setReadOnly","setCatalog"};
68 protected Boolean autoCommit = null;
69 protected Integer transactionIsolation = null;
70 protected Boolean readOnly = null;
71 protected String catalog = null;
75 public void reset(ConnectionPool parent, PooledConnection con) {
76 if (parent==null || con==null) {
77 //we are resetting, reset our defaults
79 transactionIsolation = null;
84 PoolConfiguration poolProperties = parent.getPoolProperties();
85 if (poolProperties.getDefaultTransactionIsolation()!=DataSourceFactory.UNKNOWN_TRANSACTIONISOLATION) {
87 if (transactionIsolation==null || transactionIsolation.intValue()!=poolProperties.getDefaultTransactionIsolation()) {
88 con.getConnection().setTransactionIsolation(poolProperties.getDefaultTransactionIsolation());
89 transactionIsolation = Integer.valueOf(poolProperties.getDefaultTransactionIsolation());
91 }catch (SQLException x) {
92 transactionIsolation = null;
93 log.error("Unable to reset transaction isolation state to connection.",x);
96 if (poolProperties.getDefaultReadOnly()!=null) {
98 if (readOnly==null || readOnly.booleanValue()!=poolProperties.getDefaultReadOnly().booleanValue()) {
99 con.getConnection().setReadOnly(poolProperties.getDefaultReadOnly().booleanValue());
100 readOnly = poolProperties.getDefaultReadOnly();
102 }catch (SQLException x) {
104 log.error("Unable to reset readonly state to connection.",x);
107 if (poolProperties.getDefaultAutoCommit()!=null) {
109 if (autoCommit==null || autoCommit.booleanValue()!=poolProperties.getDefaultAutoCommit().booleanValue()) {
110 con.getConnection().setAutoCommit(poolProperties.getDefaultAutoCommit().booleanValue());
111 autoCommit = poolProperties.getDefaultAutoCommit();
113 }catch (SQLException x) {
115 log.error("Unable to reset autocommit state to connection.",x);
118 if (poolProperties.getDefaultCatalog()!=null) {
120 if (catalog==null || (!catalog.equals(poolProperties.getDefaultCatalog()))) {
121 con.getConnection().setCatalog(poolProperties.getDefaultCatalog());
122 catalog = poolProperties.getDefaultCatalog();
124 }catch (SQLException x) {
126 log.error("Unable to reset default catalog state to connection.",x);
134 public void disconnected(ConnectionPool parent, PooledConnection con, boolean finalizing) {
135 //we are resetting, reset our defaults
137 transactionIsolation = null;
140 super.disconnected(parent, con, finalizing);
146 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
147 String name = method.getName();
148 boolean read = false;
150 for (int i=0; (!read) && i<readState.length; i++) {
151 read = compare(name,readState[i]);
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;
159 Object result = null;
162 case 0:{result = autoCommit; break;}
163 case 1:{result = transactionIsolation; break;}
164 case 2:{result = readOnly; break;}
165 case 3:{result = catalog; break;}
168 //return cached result, if we have it
169 if (result!=null) return result;
172 result = super.invoke(proxy, method, args);
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;}