Class DataSourceFactory

java.lang.Object
org.apache.tomcat.jdbc.pool.DataSourceFactory
All Implemented Interfaces:
ObjectFactory

public class DataSourceFactory extends Object implements ObjectFactory

JNDI object factory that creates an instance of DataSource - or XADataSource, when the reference names a type to be built as XA - configured from the RefAddr values of the specified Reference. Address names are matched against the pool properties listed in ALL_PROPERTIES; they are the attributes of the pool documented with PoolConfiguration's setters, not the property set of any other pool implementation.

The factory applies a strict reading of the reference: an attribute whose value is blank leaves its property at the PoolProperties default, and a value the property cannot take fails the lookup with an exception naming the attribute instead of being silently ignored. The legacy settings numTestsPerEvictionRun, accessToUnderlyingConnectionAllowed, poolPreparedStatements and maxOpenPreparedStatements are the exception: they are accepted for configuration compatibility but have no effect, so a non-blank value is reported with a warning rather than failing the lookup.

  • Field Details

    • PROP_DEFAULTAUTOCOMMIT

      protected static final String PROP_DEFAULTAUTOCOMMIT
      The defaultAutoCommit property name.
      See Also:
    • PROP_DEFAULTREADONLY

      protected static final String PROP_DEFAULTREADONLY
      The defaultReadOnly property name.
      See Also:
    • PROP_DEFAULTTRANSACTIONISOLATION

      protected static final String PROP_DEFAULTTRANSACTIONISOLATION
      The defaultTransactionIsolation property name.
      See Also:
    • PROP_DEFAULTCATALOG

      protected static final String PROP_DEFAULTCATALOG
      The defaultCatalog property name.
      See Also:
    • PROP_DRIVERCLASSNAME

      protected static final String PROP_DRIVERCLASSNAME
      The driverClassName property name.
      See Also:
    • PROP_PASSWORD

      protected static final String PROP_PASSWORD
      The password property name.
      See Also:
    • PROP_URL

      protected static final String PROP_URL
      The url property name.
      See Also:
    • PROP_USERNAME

      protected static final String PROP_USERNAME
      The username property name.
      See Also:
    • PROP_MAXACTIVE

      protected static final String PROP_MAXACTIVE
      The maxActive property name.
      See Also:
    • PROP_MAXIDLE

      protected static final String PROP_MAXIDLE
      The maxIdle property name.
      See Also:
    • PROP_MINIDLE

      protected static final String PROP_MINIDLE
      The minIdle property name.
      See Also:
    • PROP_INITIALSIZE

      protected static final String PROP_INITIALSIZE
      The initialSize property name.
      See Also:
    • PROP_MAXWAIT

      protected static final String PROP_MAXWAIT
      The maxWait property name.
      See Also:
    • PROP_MAXAGE

      protected static final String PROP_MAXAGE
      The maxAge property name.
      See Also:
    • PROP_TESTONBORROW

      protected static final String PROP_TESTONBORROW
      The testOnBorrow property name.
      See Also:
    • PROP_TESTONRETURN

      protected static final String PROP_TESTONRETURN
      The testOnReturn property name.
      See Also:
    • PROP_TESTWHILEIDLE

      protected static final String PROP_TESTWHILEIDLE
      The testWhileIdle property name.
      See Also:
    • PROP_TESTONCONNECT

      protected static final String PROP_TESTONCONNECT
      The testOnConnect property name.
      See Also:
    • PROP_VALIDATIONQUERY

      protected static final String PROP_VALIDATIONQUERY
      The validationQuery property name.
      See Also:
    • PROP_VALIDATIONQUERY_TIMEOUT

      protected static final String PROP_VALIDATIONQUERY_TIMEOUT
      The validationQueryTimeout property name.
      See Also:
    • PROP_VALIDATOR_CLASS_NAME

      protected static final String PROP_VALIDATOR_CLASS_NAME
      The validatorClassName property name.
      See Also:
    • PROP_NUMTESTSPEREVICTIONRUN

      protected static final String PROP_NUMTESTSPEREVICTIONRUN
      The numTestsPerEvictionRun property name.
      See Also:
    • PROP_TIMEBETWEENEVICTIONRUNSMILLIS

      protected static final String PROP_TIMEBETWEENEVICTIONRUNSMILLIS
      The timeBetweenEvictionRunsMillis property name.
      See Also:
    • PROP_MINEVICTABLEIDLETIMEMILLIS

      protected static final String PROP_MINEVICTABLEIDLETIMEMILLIS
      The minEvictableIdleTimeMillis property name.
      See Also:
    • PROP_ACCESSTOUNDERLYINGCONNECTIONALLOWED

      protected static final String PROP_ACCESSTOUNDERLYINGCONNECTIONALLOWED
      The accessToUnderlyingConnectionAllowed property name.
      See Also:
    • PROP_REMOVEABANDONED

      protected static final String PROP_REMOVEABANDONED
      The removeAbandoned property name.
      See Also:
    • PROP_REMOVEABANDONEDTIMEOUT

      protected static final String PROP_REMOVEABANDONEDTIMEOUT
      The removeAbandonedTimeout property name.
      See Also:
    • PROP_LOGABANDONED

      protected static final String PROP_LOGABANDONED
      The logAbandoned property name.
      See Also:
    • PROP_ABANDONWHENPERCENTAGEFULL

      protected static final String PROP_ABANDONWHENPERCENTAGEFULL
      The abandonWhenPercentageFull property name.
      See Also:
    • PROP_POOLPREPAREDSTATEMENTS

      protected static final String PROP_POOLPREPAREDSTATEMENTS
      The poolPreparedStatements property name.
      See Also:
    • PROP_MAXOPENPREPAREDSTATEMENTS

      protected static final String PROP_MAXOPENPREPAREDSTATEMENTS
      The maxOpenPreparedStatements property name.
      See Also:
    • PROP_CONNECTIONPROPERTIES

      protected static final String PROP_CONNECTIONPROPERTIES
      The connectionProperties property name.
      See Also:
    • PROP_INITSQL

      protected static final String PROP_INITSQL
      The initSQL property name.
      See Also:
    • PROP_INTERCEPTORS

      protected static final String PROP_INTERCEPTORS
      The jdbcInterceptors property name.
      See Also:
    • PROP_VALIDATIONINTERVAL

      protected static final String PROP_VALIDATIONINTERVAL
      The validationInterval property name.
      See Also:
    • PROP_JMX_ENABLED

      protected static final String PROP_JMX_ENABLED
      The jmxEnabled property name.
      See Also:
    • PROP_FAIR_QUEUE

      protected static final String PROP_FAIR_QUEUE
      The fairQueue property name.
      See Also:
    • PROP_USE_EQUALS

      protected static final String PROP_USE_EQUALS
      The useEquals property name.
      See Also:
    • PROP_USE_LOCK

      protected static final String PROP_USE_LOCK
      The useLock property name.
      See Also:
    • PROP_DATASOURCE

      protected static final String PROP_DATASOURCE
      The dataSource property name.
      See Also:
    • PROP_DATASOURCE_JNDI

      protected static final String PROP_DATASOURCE_JNDI
      The dataSourceJNDI property name.
      See Also:
    • PROP_SUSPECT_TIMEOUT

      protected static final String PROP_SUSPECT_TIMEOUT
      The suspectTimeout property name.
      See Also:
    • PROP_ALTERNATE_USERNAME_ALLOWED

      protected static final String PROP_ALTERNATE_USERNAME_ALLOWED
      The alternateUsernameAllowed property name.
      See Also:
    • PROP_COMMITONRETURN

      protected static final String PROP_COMMITONRETURN
      The commitOnReturn property name.
      See Also:
    • PROP_ROLLBACKONRETURN

      protected static final String PROP_ROLLBACKONRETURN
      The rollbackOnReturn property name.
      See Also:
    • PROP_USEDISPOSABLECONNECTIONFACADE

      protected static final String PROP_USEDISPOSABLECONNECTIONFACADE
      Property name for useDisposableConnectionFacade. The property is deprecated and has no effect; the disposable connection facade is always used.
      See Also:
    • PROP_LOGVALIDATIONERRORS

      protected static final String PROP_LOGVALIDATIONERRORS
      The logValidationErrors property name.
      See Also:
    • PROP_PROPAGATEINTERRUPTSTATE

      protected static final String PROP_PROPAGATEINTERRUPTSTATE
      The propagateInterruptState property name.
      See Also:
    • PROP_IGNOREEXCEPTIONONPRELOAD

      protected static final String PROP_IGNOREEXCEPTIONONPRELOAD
      The ignoreExceptionOnPreLoad property name.
      See Also:
    • PROP_USESTATEMENTFACADE

      protected static final String PROP_USESTATEMENTFACADE
      Property name for useStatementFacade. The property is deprecated and has no effect; the statement facade is always used.
      See Also:
    • UNKNOWN_TRANSACTIONISOLATION

      public static final int UNKNOWN_TRANSACTIONISOLATION
      Value indicating an unknown transaction isolation level.
      See Also:
    • OBJECT_NAME

      public static final String OBJECT_NAME
      Property name for the JMX object name.
      See Also:
    • ALL_PROPERTIES

      protected static final String[] ALL_PROPERTIES
      Array of all supported property names.
  • Constructor Details

    • DataSourceFactory

      public DataSourceFactory()
      Default constructor.
  • Method Details

    • getObjectInstance

      public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?,?> environment) throws Exception

      Creates and returns the pooled data source that the given Reference describes: an DataSource, or an XADataSource when the reference names javax.sql.XADataSource or the pool's own XA class.

      Specified by:
      getObjectInstance in interface ObjectFactory
      Parameters:
      obj - The possibly null object containing location or reference information that can be used in creating an object
      name - The name of this object relative to nameCtx
      nameCtx - The context relative to which the name parameter is specified, or null if name is relative to the default initial context
      environment - The possibly null environment that is used in creating this object
      Returns:
      the configured data source, or null if obj is not a Reference or names a class this factory does not build (the latter is logged)
      Throws:
      IllegalArgumentException - if an attribute carries a value its property cannot take: an unusable boolean, numeric or transaction-isolation value, or a dataSource attribute holding a String or another connection pool
      NamingException - if the name given by the dataSourceJNDI attribute does not resolve to a data source that can back the pool
      Exception - if an exception occurs creating the instance
    • parsePoolProperties

      public static PoolConfiguration parsePoolProperties(Properties properties)
      Parse the given properties into a PoolConfiguration.

      A blank attribute value - empty or whitespace only, the shape a cleared template attribute yields - leaves the property at its PoolProperties default. A value the property cannot take fails the parse naming the attribute, rather than being silently dropped the way unparsed values used to be. Numeric, boolean, transaction-isolation and class-name (driverClassName and validatorClassName) attributes apply their trimmed value, and so does the lookup of the name given by dataSourceJNDI: whitespace around a number, a keyword or a class or JNDI name is always noise. Other string values are stored verbatim - a catalog, a validation query, a credential or a URL can legitimately carry surrounding whitespace.

      Two attributes are not plain property assignments: connectionProperties is resolved into the driver properties through PoolProperties.setConnectionProperties(String), and username/password are merged into that same driver property set afterwards, so credentials configured as their own attributes override a user or password pair inside connectionProperties.

      Parameters:
      properties - The properties to parse
      Returns:
      The configured PoolConfiguration
      Throws:
      IllegalArgumentException - if an attribute carries a value its property cannot take
    • createDataSource

      public DataSource createDataSource(Properties properties) throws Exception
      Creates and configures a DataSource instance based on the given properties.
      Parameters:
      properties - the datasource configuration properties
      Returns:
      the datasource
      Throws:
      Exception - if an error occurs creating the data source
    • createDataSource

      public DataSource createDataSource(Properties properties, Context context, boolean XA) throws Exception
      Creates and configures a DataSource instance based on the given properties.
      Parameters:
      properties - The datasource configuration properties
      context - The JNDI context
      XA - true to create an XA data source
      Returns:
      The configured datasource
      Throws:
      Exception - if an error occurs creating the data source
    • createDataSource

      protected DataSource createDataSource(Properties properties, Context context, boolean XA, Object dataSourceObject) throws Exception
      Creates and configures a DataSource instance based on the given properties, with an optional live data source for the pool to wrap.

      When dataSourceObject is not null, it is set on the pool configuration before the pool is created, and the dataSourceJNDI lookup is skipped: the pool is built around the object that was handed to the factory directly. The object is one of the data source types the pool's connect path accepts (javax.sql.DataSource, javax.sql.XADataSource or javax.sql.ConnectionPoolDataSource), not necessarily a javax.sql.DataSource.

      Parameters:
      properties - The datasource configuration properties
      context - The JNDI context
      XA - true to create an XA data source
      dataSourceObject - A live data source object the pool should wrap, or null
      Returns:
      The configured datasource
      Throws:
      Exception - if an error occurs creating the data source
    • performJNDILookup

      public void performJNDILookup(Context context, PoolConfiguration poolProperties) throws NamingException
      Perform a JNDI lookup for the data source.
      Parameters:
      context - The JNDI context
      poolProperties - The pool configuration
      Throws:
      NamingException - if the name configured through the dataSourceJNDI property cannot be resolved, or resolves to an object the pool cannot create connections from. The name was configured specifically to supply the connections for the pool, so a pool without that data source is not a usable fallback and the failure is reported instead of being deferred to the first connection request.
    • getProperties

      protected static Properties getProperties(String propText)
      Parse properties from the string. Format of the string must be [propertyName=property;]*.

      Kept as a protected extension point for subclasses that parse property strings of their own; this factory no longer calls it, as its connectionProperties attribute goes through the canonical PoolProperties.setConnectionProperties(String), which parses with the same delegate used here.

      Parameters:
      propText - The properties string
      Returns:
      the properties