[SDNC-5] Rebase sdnc-core
[sdnc/core.git] / dblib / common / src / main / java / org / apache / tomcat / jdbc / pool / interceptor / ResetAbandonedTimer.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
38 package org.apache.tomcat.jdbc.pool.interceptor;
39
40 import java.lang.reflect.Method;
41
42 import org.apache.tomcat.jdbc.pool.JdbcInterceptor;
43 import org.apache.tomcat.jdbc.pool.PooledConnection;
44 import org.apache.tomcat.jdbc.pool.ProxyConnection;
45
46 /**
47  * Class that resets the abandoned timer on any activity on the
48  * Connection or any successful query executions.
49  * This interceptor is useful for when you have a {@link org.apache.tomcat.jdbc.pool.PoolConfiguration#setRemoveAbandonedTimeout(int)}
50  * that is fairly low, and you want to reset the abandoned time each time any operation on the connection is performed
51  * This is useful for batch processing programs that use connections for extensive amount of times.
52  *
53  */
54 public class ResetAbandonedTimer extends AbstractQueryReport {
55
56     public ResetAbandonedTimer() {
57     }
58
59     public boolean resetTimer() {
60         boolean result = false;
61         JdbcInterceptor interceptor = this.getNext();
62         while (interceptor!=null && result==false) {
63             if (interceptor instanceof ProxyConnection) {
64                 PooledConnection con = ((ProxyConnection)interceptor).getConnection();
65                 if (con!=null) {
66                     con.setTimestamp(System.currentTimeMillis());
67                     result = true;
68                 } else {
69                     break;
70                 }
71             }
72             interceptor = interceptor.getNext();
73         }
74         return result;
75     }
76
77
78     @Override
79     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
80         Object result = super.invoke(proxy, method, args);
81         resetTimer();
82         return result;
83     }
84
85     @Override
86     protected void prepareCall(String query, long time) {
87         resetTimer();
88     }
89
90     @Override
91     protected void prepareStatement(String sql, long time) {
92         resetTimer();
93
94     }
95
96     @Override
97     public void closeInvoked() {
98         resetTimer();
99     }
100
101     @Override
102     protected String reportQuery(String query, Object[] args, String name,long start, long delta) {
103         resetTimer();
104         return super.reportQuery(query, args, name, start, delta);
105     }
106
107     @Override
108     protected String reportSlowQuery(String query, Object[] args, String name,long start, long delta) {
109         resetTimer();
110         return super.reportSlowQuery(query, args, name, start, delta);
111     }
112 }