001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     * 
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.dbcp;
019    
020    import java.io.PrintWriter;
021    
022    /**
023     * Configuration settings for handling abandoned db connections.
024     *                                                            
025     * @author Glenn L. Nielsen           
026     * @version $Revision: 892307 $ $Date: 2013-12-31 23:27:28 +0000 (Tue, 31 Dec 2013) $
027     */
028    public class AbandonedConfig {
029    
030        /**
031         * Whether or not a connection is considered abandoned and eligible
032         * for removal if it has been idle longer than the removeAbandonedTimeout
033         */
034        private boolean removeAbandoned = false;
035    
036        /**
037         * Flag to remove abandoned connections if they exceed the
038         * removeAbandonedTimeout.
039         *
040         * Set to true or false, default false.
041         * If set to true a connection is considered abandoned and eligible
042         * for removal if it has been idle longer than the removeAbandonedTimeout.
043         * Setting this to true can recover db connections from poorly written    
044         * applications which fail to close a connection.
045         *
046         * @return true if abandoned connections are to be removed
047         */
048        public boolean getRemoveAbandoned() {
049            return (this.removeAbandoned);
050        }
051    
052        /**
053         * Flag to remove abandoned connections if they exceed the
054         * removeAbandonedTimeout.
055         *
056         * Set to true or false, default false.
057         * If set to true a connection is considered abandoned and eligible   
058         * for removal if it has been idle longer than the removeAbandonedTimeout.
059         * Setting this to true can recover db connections from poorly written
060         * applications which fail to close a connection.
061         *
062         * @param removeAbandoned true means abandoned connections will be
063         *   removed
064         */
065        public void setRemoveAbandoned(boolean removeAbandoned) {
066            this.removeAbandoned = removeAbandoned;
067        }
068    
069        /**
070         * Timeout in seconds before an abandoned connection can be removed
071         */
072        private int removeAbandonedTimeout = 300;
073    
074        /**
075         * Timeout in seconds before an abandoned connection can be removed.
076         *
077         * Defaults to 300 seconds.
078         *
079         * @return abandoned timeout in seconds
080         */
081        public int getRemoveAbandonedTimeout() {
082            return (this.removeAbandonedTimeout);
083        }
084    
085        /**
086         * Timeout in seconds before an abandoned connection can be removed.
087         *
088         * Defaults to 300 seconds.
089         *
090         * @param removeAbandonedTimeout abandoned timeout in seconds
091         */
092        public void setRemoveAbandonedTimeout(int removeAbandonedTimeout) {
093            this.removeAbandonedTimeout = removeAbandonedTimeout;
094        }
095    
096        /**
097         * Determines whether or not to log stack traces for application code
098         * which abandoned a Statement or Connection.
099         */
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    }