Class DataSource
- java.lang.Object
-
- org.apache.tomcat.jdbc.pool.DataSourceProxy
-
- org.apache.tomcat.jdbc.pool.DataSource
-
- All Implemented Interfaces:
java.sql.Wrapper,javax.management.MBeanRegistration,javax.sql.CommonDataSource,javax.sql.ConnectionPoolDataSource,javax.sql.DataSource,ConnectionPoolMBean,PoolConfiguration
- Direct Known Subclasses:
XADataSource
public class DataSource extends DataSourceProxy implements javax.sql.DataSource, javax.management.MBeanRegistration, ConnectionPoolMBean, javax.sql.ConnectionPoolDataSource
A DataSource that can be instantiated through IoC and implements the DataSource interface since the DataSourceProxy is used as a generic proxy. The DataSource simply wraps aConnectionPoolin order to provide a standard interface to the user.A JMX registration made before the pool exists publishes nothing, because there is no pool MBean to publish. Create the pool before registering the data source, or register it again after the pool has been created, to have the pool monitored.
Deregistering a published data source releases the pool MBean and the connection and interceptor MBeans published below it, but it does not close the pool. A JMX-enabled pool that is no longer wanted must therefore be closed before the data source is dropped: while the pool MBean is registered it holds the pool, and the connection MBeans hold the pooled connections. Abandoning a registered pool without
close(boolean)keeps the pool, its pooled sessions and its JDBC driver class loader reachable for the life of the JVM.
-
-
Field Summary
Fields Modifier and Type Field Description protected javax.management.ObjectNameonameThe JMX ObjectName for the connection pool MBean.-
Fields inherited from class org.apache.tomcat.jdbc.pool.DataSourceProxy
pool, poolProperties
-
Fields inherited from interface org.apache.tomcat.jdbc.pool.jmx.ConnectionPoolMBean
FAILED_QUERY_NOTIFICATION, NOTIFY_ABANDON, NOTIFY_CONNECT, NOTIFY_INIT, POOL_EMPTY, SLOW_QUERY_NOTIFICATION, SUSPECT_ABANDONED_NOTIFICATION, SUSPECT_RETURNED_NOTIFICATION
-
Fields inherited from interface org.apache.tomcat.jdbc.pool.PoolConfiguration
PKG_PREFIX
-
-
Constructor Summary
Constructors Constructor Description DataSource()Constructor for reflection only.DataSource(PoolConfiguration poolProperties)Constructs a DataSource object wrapping a connection
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose(boolean all)Unregisters the connection pool MBean before closing the pool, so a pool under teardown does not keep reporting statistics under a live object name (and does not pin the closing pool, its credentials and its JDBC driver class loader in the MBean server).javax.management.ObjectNamecreateObjectName(javax.management.ObjectName original)Creates the ObjectName for the ConnectionPoolMBean object to be registeredvoidpostDeregister()Unregisters the underlying connection pool mbean.
voidpostRegister(java.lang.Boolean registrationDone)Releases the pool MBean published by this registration when the MBean server did not complete it.voidpreDeregister()no-op
javax.management.ObjectNamepreRegister(javax.management.MBeanServer server, javax.management.ObjectName name)If the connection pool MBean exists, it is registered during this operation.protected ConnectionPoolpublishJmx()Publishes the pool MBean under this data source's monitor and returns the pool whose connections must then be published, ornullwhen nothing was published.protected voidregisterJmx()Registers the ConnectionPoolMBean under a unique name derived from the DataSource's ObjectName.protected voidunregisterJmx(javax.management.ObjectName name)Unregisters the ConnectionPoolMBean published under the given name.-
Methods inherited from class org.apache.tomcat.jdbc.pool.DataSourceProxy
checkAbandoned, checkIdle, close, closePool, createPool, getAbandonWhenPercentageFull, getActive, getBorrowedCount, getCommitOnReturn, getConnection, getConnection, getConnectionAsync, getConnectionProperties, getCreatedCount, getDataSource, getDataSourceJNDI, getDbProperties, getDefaultAutoCommit, getDefaultCatalog, getDefaultReadOnly, getDefaultTransactionIsolation, getDriverClassName, getIdle, getInitialSize, getInitSQL, getJdbcInterceptors, getJdbcInterceptorsAsArray, getLoginTimeout, getLogValidationErrors, getLogWriter, getMaxActive, getMaxAge, getMaxIdle, getMaxWait, getMinEvictableIdleTimeMillis, getMinIdle, getName, getNumActive, getNumIdle, getNumTestsPerEvictionRun, getParentLogger, getPassword, getPool, getPooledConnection, getPooledConnection, getPoolName, getPoolProperties, getPoolSize, getPropagateInterruptState, getReconnectedCount, getReleasedCount, getReleasedIdleCount, getRemoveAbandonedCount, getRemoveAbandonedTimeout, getReturnedCount, getRollbackOnReturn, getSize, getSuspectTimeout, getTimeBetweenEvictionRunsMillis, getUrl, getUseDisposableConnectionFacade, getUseLock, getUsername, getUseStatementFacade, getValidationInterval, getValidationQuery, getValidationQueryTimeout, getValidator, getValidatorClassName, getWaitCount, getXAConnection, getXAConnection, isAccessToUnderlyingConnectionAllowed, isAlternateUsernameAllowed, isDefaultAutoCommit, isDefaultReadOnly, isFairQueue, isIgnoreExceptionOnPreLoad, isJmxEnabled, isLogAbandoned, isPoolSweeperEnabled, isRemoveAbandoned, isTestOnBorrow, isTestOnConnect, isTestOnReturn, isTestWhileIdle, isUseEquals, isWrapperFor, purge, purgeOnReturn, resetStats, setAbandonWhenPercentageFull, setAccessToUnderlyingConnectionAllowed, setAlternateUsernameAllowed, setCommitOnReturn, setConnectionProperties, setDataSource, setDataSourceJNDI, setDbProperties, setDefaultAutoCommit, setDefaultCatalog, setDefaultReadOnly, setDefaultTransactionIsolation, setDriverClassName, setFairQueue, setIgnoreExceptionOnPreLoad, setInitialSize, setInitSQL, setJdbcInterceptors, setJmxEnabled, setLogAbandoned, setLoginTimeout, setLogValidationErrors, setLogWriter, setMaxActive, setMaxAge, setMaxIdle, setMaxWait, setMinEvictableIdleTimeMillis, setMinIdle, setName, setNumTestsPerEvictionRun, setPassword, setPoolProperties, setPropagateInterruptState, setRemoveAbandoned, setRemoveAbandonedTimeout, setRollbackOnReturn, setSuspectTimeout, setTestOnBorrow, setTestOnConnect, setTestOnReturn, setTestWhileIdle, setTimeBetweenEvictionRunsMillis, setUrl, setUseDisposableConnectionFacade, setUseEquals, setUseLock, setUsername, setUseStatementFacade, setValidationInterval, setValidationQuery, setValidationQueryTimeout, setValidator, setValidatorClassName, takePoolForClose, testIdle, toString, unwrap
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface javax.sql.CommonDataSource
createShardingKeyBuilder, getParentLogger
-
Methods inherited from interface javax.sql.ConnectionPoolDataSource
createPooledConnectionBuilder, getLoginTimeout, getLogWriter, getPooledConnection, getPooledConnection, setLoginTimeout, setLogWriter
-
Methods inherited from interface org.apache.tomcat.jdbc.pool.jmx.ConnectionPoolMBean
checkAbandoned, checkIdle, getActive, getBorrowedCount, getCreatedCount, getIdle, getNumActive, getNumIdle, getReconnectedCount, getReleasedCount, getReleasedIdleCount, getRemoveAbandonedCount, getReturnedCount, getSize, getWaitCount, purge, purgeOnReturn, resetStats, testIdle
-
Methods inherited from interface javax.sql.DataSource
createConnectionBuilder, getConnection, getConnection, getLoginTimeout, getLogWriter, setLoginTimeout, setLogWriter
-
Methods inherited from interface org.apache.tomcat.jdbc.pool.PoolConfiguration
getAbandonWhenPercentageFull, getCommitOnReturn, getConnectionProperties, getDataSource, getDataSourceJNDI, getDbProperties, getDefaultAutoCommit, getDefaultCatalog, getDefaultReadOnly, getDefaultTransactionIsolation, getDriverClassName, getInitialSize, getInitSQL, getJdbcInterceptors, getJdbcInterceptorsAsArray, getLogValidationErrors, getMaxActive, getMaxAge, getMaxIdle, getMaxWait, getMinEvictableIdleTimeMillis, getMinIdle, getName, getNumTestsPerEvictionRun, getPassword, getPoolName, getPropagateInterruptState, getRemoveAbandonedTimeout, getRollbackOnReturn, getSuspectTimeout, getTimeBetweenEvictionRunsMillis, getUrl, getUseDisposableConnectionFacade, getUseLock, getUsername, getUseStatementFacade, getValidationInterval, getValidationQuery, getValidationQueryTimeout, getValidator, getValidatorClassName, isAccessToUnderlyingConnectionAllowed, isAlternateUsernameAllowed, isDefaultAutoCommit, isDefaultReadOnly, isFairQueue, isIgnoreExceptionOnPreLoad, isJmxEnabled, isLogAbandoned, isPoolSweeperEnabled, isRemoveAbandoned, isTestOnBorrow, isTestOnConnect, isTestOnReturn, isTestWhileIdle, isUseEquals, setAbandonWhenPercentageFull, setAccessToUnderlyingConnectionAllowed, setAlternateUsernameAllowed, setCommitOnReturn, setConnectionProperties, setDataSource, setDataSourceJNDI, setDbProperties, setDefaultAutoCommit, setDefaultCatalog, setDefaultReadOnly, setDefaultTransactionIsolation, setDriverClassName, setFairQueue, setIgnoreExceptionOnPreLoad, setInitialSize, setInitSQL, setJdbcInterceptors, setJmxEnabled, setLogAbandoned, setLogValidationErrors, setMaxActive, setMaxAge, setMaxIdle, setMaxWait, setMinEvictableIdleTimeMillis, setMinIdle, setName, setNumTestsPerEvictionRun, setPassword, setPropagateInterruptState, setRemoveAbandoned, setRemoveAbandonedTimeout, setRollbackOnReturn, setSuspectTimeout, setTestOnBorrow, setTestOnConnect, setTestOnReturn, setTestWhileIdle, setTimeBetweenEvictionRunsMillis, setUrl, setUseDisposableConnectionFacade, setUseEquals, setUseLock, setUsername, setUseStatementFacade, setValidationInterval, setValidationQuery, setValidationQueryTimeout, setValidator, setValidatorClassName
-
-
-
-
Constructor Detail
-
DataSource
public DataSource()
Constructor for reflection only. A default set of pool properties will be created.
-
DataSource
public DataSource(PoolConfiguration poolProperties)
Constructs a DataSource object wrapping a connection- Parameters:
poolProperties- The pool properties
-
-
Method Detail
-
close
public void close(boolean all)
Unregisters the connection pool MBean before closing the pool, so a pool under teardown does not keep reporting statistics under a live object name (and does not pin the closing pool, its credentials and its JDBC driver class loader in the MBean server). Monitoring stops with this call, whichever way the close goes: withall=falsea connection that is currently borrowed keeps its database session and stays usable until its holder returns it, but it is no longer reported by the pool MBean.
Close the connection pool. If a pool creation is in flight, this method does not wait for it - the driver connects of the creation's initial pre-load can block for an unbounded, uninterruptible time against a hung database - it marks this data source closed and returns: the creation observes the closure at its publication step and closes the pool it built instead of publishing it. The same reason keeps the teardown outside the monitor: the physical disconnect of every pooled session is as unbounded against a hung database as the connect is, and it must not park the paths that need the monitor meanwhile - a later borrow's publication step, or the container's JMX lifecycle callbacks.- Overrides:
closein classDataSourceProxy- Parameters:
all- if true close all connections, otherwise only idle ones
-
postDeregister
public void postDeregister()
Unregisters the underlying connection pool mbean.
- Specified by:
postDeregisterin interfacejavax.management.MBeanRegistration
-
postRegister
public void postRegister(java.lang.Boolean registrationDone)
Releases the pool MBean published by this registration when the MBean server did not complete it. A failed registration does not runpostDeregister(), so this is the only chance to undo a publish this registration made. A registration that published nothing releases nothing.
- Specified by:
postRegisterin interfacejavax.management.MBeanRegistration
-
preDeregister
public void preDeregister() throws java.lang.Exceptionno-op
- Specified by:
preDeregisterin interfacejavax.management.MBeanRegistration- Throws:
java.lang.Exception
-
preRegister
public javax.management.ObjectName preRegister(javax.management.MBeanServer server, javax.management.ObjectName name) throws java.lang.ExceptionIf the connection pool MBean exists, it is registered during this operation. A second registration is refused while this data source already holds a publication.
- Specified by:
preRegisterin interfacejavax.management.MBeanRegistration- Throws:
java.lang.Exception
-
createObjectName
public javax.management.ObjectName createObjectName(javax.management.ObjectName original) throws javax.management.MalformedObjectNameExceptionCreates the ObjectName for the ConnectionPoolMBean object to be registered- Parameters:
original- the ObjectName for the DataSource- Returns:
- the ObjectName for the ConnectionPoolMBean
- Throws:
javax.management.MalformedObjectNameException- Invalid object name
-
registerJmx
protected void registerJmx()
Registers the ConnectionPoolMBean under a unique name derived from the DataSource's ObjectName. A pool that has not been created, or a publish the server refused, is skipped without disturbing a handle this data source already holds.
-
publishJmx
protected ConnectionPool publishJmx()
Publishes the pool MBean under this data source's monitor and returns the pool whose connections must then be published, ornullwhen nothing was published. The caller must hold this monitor; the connection sweep is the caller's to run outside it.- Returns:
- the pool whose connections need publishing, or
null
-
unregisterJmx
protected void unregisterJmx(javax.management.ObjectName name)
Unregisters the ConnectionPoolMBean published under the given name. The name is the registration handle captured by the caller, which holds this data source's monitor: the handle is checked, cleared and released as one step there, so it never reaches this method twice and a name this data source never published is never released.- Parameters:
name- the ObjectName the pool MBean was published under, ornullto release nothing
-
-