/**
* Copyright 2010 Wallace Wadge
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jolbox.bonecp;
import java.io.InputStream;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.sql.Connection;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import javax.sql.DataSource;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.google.common.base.Objects;
import com.jolbox.bonecp.hooks.ConnectionHook;
/**
* Configuration class.
*
* @author wallacew
*/
public class BoneCPConfig implements BoneCPConfigMBean, Cloneable, Serializable {
/** Constant. */
private static final String USER = "user";
/** Constant. */
private static final String PASSWORD = "password";
/** Serialization UID. */
private static final long serialVersionUID = 6090570773474131622L;
/** For toString(). */
private static final String CONFIG_TOSTRING = "JDBC URL = %s, Username = %s, partitions = %d, max (per partition) = %d, min (per partition) = %d, helper threads = %d, idle max age = %d min, idle test period = %d min";
/** For toString(). */
private static final String CONFIG_DS_TOSTRING = "JDBC URL = (via datasource bean), Username = (via datasource bean), partitions = %d, max (per partition) = %d, min (per partition) = %d, helper threads = %d, idle max age = %d min, idle test period = %d min";
/** Logger class. */
private static final Logger logger = LoggerFactory.getLogger(BoneCPConfig.class);
/** Min number of connections per partition. */
private int minConnectionsPerPartition;
/** Max number of connections per partition. */
private int maxConnectionsPerPartition;
/** Number of new connections to create in 1 batch. */
private int acquireIncrement = 2;
/** Number of partitions. */
private int partitionCount = 1;
/** DB connection string. */
private String jdbcUrl;
/** User name to use. */
private String username;
/** Password to use. */
private String password;
/** Connections older than this are sent a keep-alive statement. */
private long idleConnectionTestPeriodInSeconds = 240*60;
/** Maximum age of an unused connection before it is closed off. */
private long idleMaxAgeInSeconds = 60*60;
/** SQL statement to use for keep-alive/test of connection. */
private String connectionTestStatement;
/** Min no of prepared statements to cache. */
private int statementsCacheSize = 0;
/** No of statements that can be cached per connection. Deprecated. */
private int statementsCachedPerConnection = 0;
/** Number of release-connection helper threads to create per partition. */
private int releaseHelperThreads = 3;
/** Number of statement release helper threads to create. */
private int statementReleaseHelperThreads = 0;
/** Hook class (external). */
private ConnectionHook connectionHook;
/** Query to send once per connection to the database. */
private String initSQL;
/** If set to true, create a new thread that monitors a connection and displays warnings if application failed to
* close the connection. FOR DEBUG PURPOSES ONLY!
*/
private boolean closeConnectionWatch;
/** If set to true, log SQL statements being executed. */
private boolean logStatementsEnabled;
/** After attempting to acquire a connection and failing, wait for this value before attempting to acquire a new connection again. */
private long acquireRetryDelayInMs=7000;
/** After attempting to acquire a connection and failing, try to connect these many times before giving up. */
private int acquireRetryAttempts=5;
/** If set to true, the connection pool will remain empty until the first connection is obtained. */
private boolean lazyInit;
/** If set to true, stores all activity on this connection to allow for replaying it again. */
private boolean transactionRecoveryEnabled;
/** Connection hook class name. */
private String connectionHookClassName;
/** Classloader to use when loading the JDBC driver. */
private ClassLoader classLoader = this.getClassLoader();
/** Name of the pool for JMX and thread names. */
private String poolName;
/** Set to true to disable JMX. */
private boolean disableJMX;
/** If set, use datasourceBean.getConnection() to obtain a new connection. */
private DataSource datasourceBean;
/** Queries taking longer than this limit to execute are logged. */
private long queryExecuteTimeLimitInMs = 0;
/** Create more connections when we hit x% of our possible number of connections. */
private int poolAvailabilityThreshold = 20;
/** Disable connection tracking. */
private boolean disableConnectionTracking;
/** Used when the alternate way of obtaining a connection is required */
private Properties driverProperties;
/** Time to wait before a call to getConnection() times out and returns an error. */
private long connectionTimeoutInMs = 0;
/** Time in ms to wait for close connection watch thread. */
private long closeConnectionWatchTimeoutInMs = 0;
/** A connection older than maxConnectionAge will be destroyed and purged from the pool. */
private long maxConnectionAgeInSeconds = 0;
/** Config property. */
private String configFile;
/** Queue mode. Values currently understood are FIFO and LIFO. */
private String serviceOrder;
/** If true, keep track of some statistics. */
private boolean statisticsEnabled;
/** The default auto-commit state of created connections. */
private Boolean defaultAutoCommit;
/** The default read-only state of created connections. */
private Boolean defaultReadOnly;
/** The default transaction isolation state of created connections. */
private String defaultTransactionIsolation;
/** The default catalog state of created connections. */
private String defaultCatalog;
/** The parsed transaction isolation value. Default = driver value*/
private int defaultTransactionIsolationValue = -1;
/** If true, stop caring about username/password when obtaining raw connections. */
private boolean externalAuth;
/** Returns the name of the pool for JMX and thread names.
* @return a pool name.
*/
public String getPoolName() {
return this.poolName;
}
/** Sets the name of the pool for JMX and thread names.
* @param poolName to set.
*/
public void setPoolName(String poolName) {
this.poolName = poolName;
}
/** {@inheritDoc}
* @see com.jolbox.bonecp.BoneCPConfigMBean#getMinConnectionsPerPartition()
*/
public int getMinConnectionsPerPartition() {
return this.minConnectionsPerPartition;
}
/**
* Sets the minimum number of connections that will be contained in every partition.
*
* @param minConnectionsPerPartition number of connections
*/
public void setMinConnectionsPerPartition(int minConnectionsPerPartition) {
this.minConnectionsPerPartition = minConnectionsPerPartition;
}
/** {@inheritDoc}
* @see com.jolbox.bonecp.BoneCPConfigMBean#getMaxConnectionsPerPartition()
*/
public int getMaxConnectionsPerPartition() {
return this.maxConnectionsPerPartition;
}
/**
* Sets the maximum number