View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.dbcp;
19  
20  import java.io.PrintWriter;
21  
22  /**
23   * Configuration settings for handling abandoned db connections.
24   *                                                            
25   * @author Glenn L. Nielsen           
26   * @version $Revision: 1023401 $ $Date: 2010-10-16 21:54:24 -0400 (Sat, 16 Oct 2010) $
27   */
28  public class AbandonedConfig {
29  
30      /**
31       * Whether or not a connection is considered abandoned and eligible
32       * for removal if it has been idle longer than the removeAbandonedTimeout
33       */
34      private boolean removeAbandoned = false;
35  
36      /**
37       * Flag to remove abandoned connections if they exceed the
38       * removeAbandonedTimeout.
39       *
40       * Set to true or false, default false.
41       * If set to true a connection is considered abandoned and eligible
42       * for removal if it has been idle longer than the removeAbandonedTimeout.
43       * Setting this to true can recover db connections from poorly written    
44       * applications which fail to close a connection.
45       *
46       * @return true if abandoned connections are to be removed
47       */
48      public boolean getRemoveAbandoned() {
49          return (this.removeAbandoned);
50      }
51  
52      /**
53       * Flag to remove abandoned connections if they exceed the
54       * removeAbandonedTimeout.
55       *
56       * Set to true or false, default false.
57       * If set to true a connection is considered abandoned and eligible   
58       * for removal if it has been idle longer than the removeAbandonedTimeout.
59       * Setting this to true can recover db connections from poorly written
60       * applications which fail to close a connection.
61       *
62       * @param removeAbandoned true means abandoned connections will be
63       *   removed
64       */
65      public void setRemoveAbandoned(boolean removeAbandoned) {
66          this.removeAbandoned = removeAbandoned;
67      }
68  
69      /**
70       * Timeout in seconds before an abandoned connection can be removed
71       */
72      private int removeAbandonedTimeout = 300;
73  
74      /**
75       * Timeout in seconds before an abandoned connection can be removed.
76       *
77       * Defaults to 300 seconds.
78       *
79       * @return abandoned timeout in seconds
80       */
81      public int getRemoveAbandonedTimeout() {
82          return (this.removeAbandonedTimeout);
83      }
84  
85      /**
86       * Timeout in seconds before an abandoned connection can be removed.
87       *
88       * Defaults to 300 seconds.
89       *
90       * @param removeAbandonedTimeout abandoned timeout in seconds
91       */
92      public void setRemoveAbandonedTimeout(int removeAbandonedTimeout) {
93          this.removeAbandonedTimeout = removeAbandonedTimeout;
94      }
95  
96      /**
97       * Determines whether or not to log stack traces for application code
98       * which abandoned a Statement or Connection.
99       */
100     private boolean logAbandoned = false;
101 
102     /**
103      * Flag to log stack traces for application code which abandoned
104      * a Statement or Connection.
105      *
106      * Defaults to false.
107      * Logging of abandoned Statements and Connections adds overhead
108      * for every Connection open or new Statement because a stack
109      * trace has to be generated.
110      * 
111      * @return boolean true if stack trace logging is turned on for abandoned
112      *  Statements or Connections
113      *
114      */
115     public boolean getLogAbandoned() {
116         return (this.logAbandoned);
117     }
118 
119     /**
120      * Flag to log stack traces for application code which abandoned
121      * a Statement or Connection.
122      *
123      * Defaults to false.
124      * Logging of abandoned Statements and Connections adds overhead
125      * for every Connection open or new Statement because a stack
126      * trace has to be generated.
127      * @param logAbandoned true turns on abandoned stack trace logging
128      *
129      */
130     public void setLogAbandoned(boolean logAbandoned) {
131         this.logAbandoned = logAbandoned;
132     }
133 
134     /**
135      * PrintWriter to use to log information on abandoned objects.
136      */
137     private PrintWriter logWriter = new PrintWriter(System.out);
138     
139     /**
140      * Returns the log writer being used by this configuration to log
141      * information on abandoned objects. If not set, a PrintWriter based on
142      * System.out is used.
143      *
144      * @return log writer in use
145      */
146     public PrintWriter getLogWriter() {
147         return logWriter;
148     }
149     
150     /**
151      * Sets the log writer to be used by this configuration to log
152      * information on abandoned objects.
153      * 
154      * @param logWriter The new log writer
155      */
156     public void setLogWriter(PrintWriter logWriter) {
157         this.logWriter = logWriter;
158     }
159 }