7aec0b4acc869c4b674f3f4513d50fd1a114bdde
[sdnc/core.git] / dblib / common / src / main / java / org / apache / tomcat / jdbc / pool / interceptor / QueryTimeoutInterceptor.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 import java.sql.SQLException;
42 import java.sql.Statement;
43 import java.util.Map;
44
45 import org.apache.juli.logging.Log;
46 import org.apache.juli.logging.LogFactory;
47 import org.apache.tomcat.jdbc.pool.PoolProperties.InterceptorProperty;
48
49 public class QueryTimeoutInterceptor extends AbstractCreateStatementInterceptor {
50     private static Log log = LogFactory.getLog(QueryTimeoutInterceptor.class);
51     int timeout = 1;
52
53     @Override
54     public void setProperties(Map<String,InterceptorProperty> properties) {
55         super.setProperties(properties);
56         InterceptorProperty p = properties.get("queryTimeout");
57         if (p!=null) timeout = p.getValueAsInt(timeout);
58     }
59
60     @Override
61     public Object createStatement(Object proxy, Method method, Object[] args, Object statement, long time) {
62         if (statement instanceof Statement && timeout > 0) {
63             Statement s = (Statement)statement;
64             try {
65                 s.setQueryTimeout(timeout);
66             }catch (SQLException x) {
67                 log.warn("[QueryTimeoutInterceptor] Unable to set query timeout:"+x.getMessage(),x);
68             }
69         }
70         return statement;
71     }
72
73     @Override
74     public void closeInvoked() {
75     }
76
77 }