Class DataSourceFactory

  • All Implemented Interfaces:
    javax.naming.spi.ObjectFactory

    public class DataSourceFactory
    extends java.lang.Object
    implements javax.naming.spi.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 Detail

      • PROP_DEFAULTAUTOCOMMIT

        protected static final java.lang.String PROP_DEFAULTAUTOCOMMIT
        The defaultAutoCommit property name.
        See Also:
        Constant Field Values
      • PROP_DEFAULTREADONLY

        protected static final java.lang.String PROP_DEFAULTREADONLY
        The defaultReadOnly property name.
        See Also:
        Constant Field Values
      • PROP_DEFAULTTRANSACTIONISOLATION

        protected static final java.lang.String PROP_DEFAULTTRANSACTIONISOLATION
        The defaultTransactionIsolation property name.
        See Also:
        Constant Field Values
      • PROP_DEFAULTCATALOG

        protected static final java.lang.String PROP_DEFAULTCATALOG
        The defaultCatalog property name.
        See Also:
        Constant Field Values
      • PROP_DRIVERCLASSNAME

        protected static final java.lang.String PROP_DRIVERCLASSNAME
        The driverClassName property name.
        See Also:
        Constant Field Values
      • PROP_PASSWORD

        protected static final java.lang.String PROP_PASSWORD
        The password property name.
        See Also:
        Constant Field Values
      • PROP_URL

        protected static final java.lang.String PROP_URL
        The url property name.
        See Also:
        Constant Field Values
      • PROP_USERNAME

        protected static final java.lang.String PROP_USERNAME
        The username property name.
        See Also:
        Constant Field Values
      • PROP_MAXACTIVE

        protected static final java.lang.String PROP_MAXACTIVE
        The maxActive property name.
        See Also:
        Constant Field Values
      • PROP_MAXIDLE

        protected static final java.lang.String PROP_MAXIDLE
        The maxIdle property name.
        See Also:
        Constant Field Values
      • PROP_MINIDLE

        protected static final java.lang.String PROP_MINIDLE
        The minIdle property name.
        See Also:
        Constant Field Values
      • PROP_INITIALSIZE

        protected static final java.lang.String PROP_INITIALSIZE
        The initialSize property name.
        See Also:
        Constant Field Values
      • PROP_MAXWAIT

        protected static final java.lang.String PROP_MAXWAIT
        The maxWait property name.
        See Also:
        Constant Field Values
      • PROP_MAXAGE

        protected static final java.lang.String PROP_MAXAGE
        The maxAge property name.
        See Also:
        Constant Field Values
      • PROP_TESTONBORROW

        protected static final java.lang.String PROP_TESTONBORROW
        The testOnBorrow property name.
        See Also:
        Constant Field Values
      • PROP_TESTONRETURN

        protected static final java.lang.String PROP_TESTONRETURN
        The testOnReturn property name.
        See Also:
        Constant Field Values
      • PROP_TESTWHILEIDLE

        protected static final java.lang.String PROP_TESTWHILEIDLE
        The testWhileIdle property name.
        See Also:
        Constant Field Values
      • PROP_TESTONCONNECT

        protected static final java.lang.String PROP_TESTONCONNECT
        The testOnConnect property name.
        See Also:
        Constant Field Values
      • PROP_VALIDATIONQUERY

        protected static final java.lang.String PROP_VALIDATIONQUERY
        The validationQuery property name.
        See Also:
        Constant Field Values
      • PROP_VALIDATIONQUERY_TIMEOUT

        protected static final java.lang.String PROP_VALIDATIONQUERY_TIMEOUT
        The validationQueryTimeout property name.
        See Also:
        Constant Field Values
      • PROP_VALIDATOR_CLASS_NAME

        protected static final java.lang.String PROP_VALIDATOR_CLASS_NAME
        The validatorClassName property name.
        See Also:
        Constant Field Values
      • PROP_NUMTESTSPEREVICTIONRUN

        protected static final java.lang.String PROP_NUMTESTSPEREVICTIONRUN
        The numTestsPerEvictionRun property name.
        See Also:
        Constant Field Values
      • PROP_TIMEBETWEENEVICTIONRUNSMILLIS

        protected static final java.lang.String PROP_TIMEBETWEENEVICTIONRUNSMILLIS
        The timeBetweenEvictionRunsMillis property name.
        See Also:
        Constant Field Values
      • PROP_MINEVICTABLEIDLETIMEMILLIS

        protected static final java.lang.String PROP_MINEVICTABLEIDLETIMEMILLIS
        The minEvictableIdleTimeMillis property name.
        See Also:
        Constant Field Values
      • PROP_ACCESSTOUNDERLYINGCONNECTIONALLOWED

        protected static final java.lang.String PROP_ACCESSTOUNDERLYINGCONNECTIONALLOWED
        The accessToUnderlyingConnectionAllowed property name.
        See Also:
        Constant Field Values
      • PROP_REMOVEABANDONED

        protected static final java.lang.String PROP_REMOVEABANDONED
        The removeAbandoned property name.
        See Also:
        Constant Field Values
      • PROP_REMOVEABANDONEDTIMEOUT

        protected static final java.lang.String PROP_REMOVEABANDONEDTIMEOUT
        The removeAbandonedTimeout property name.
        See Also:
        Constant Field Values
      • PROP_LOGABANDONED

        protected static final java.lang.String PROP_LOGABANDONED
        The logAbandoned property name.
        See Also:
        Constant Field Values
      • PROP_ABANDONWHENPERCENTAGEFULL

        protected static final java.lang.String PROP_ABANDONWHENPERCENTAGEFULL
        The abandonWhenPercentageFull property name.
        See Also:
        Constant Field Values
      • PROP_POOLPREPAREDSTATEMENTS

        protected static final java.lang.String PROP_POOLPREPAREDSTATEMENTS
        The poolPreparedStatements property name.
        See Also:
        Constant Field Values
      • PROP_MAXOPENPREPAREDSTATEMENTS

        protected static final java.lang.String PROP_MAXOPENPREPAREDSTATEMENTS
        The maxOpenPreparedStatements property name.
        See Also:
        Constant Field Values
      • PROP_CONNECTIONPROPERTIES

        protected static final java.lang.String PROP_CONNECTIONPROPERTIES
        The connectionProperties property name.
        See Also:
        Constant Field Values
      • PROP_INITSQL

        protected static final java.lang.String PROP_INITSQL
        The initSQL property name.
        See Also:
        Constant Field Values
      • PROP_INTERCEPTORS

        protected static final java.lang.String PROP_INTERCEPTORS
        The jdbcInterceptors property name.
        See Also:
        Constant Field Values
      • PROP_VALIDATIONINTERVAL

        protected static final java.lang.String PROP_VALIDATIONINTERVAL
        The validationInterval property name.
        See Also:
        Constant Field Values
      • PROP_JMX_ENABLED

        protected static final java.lang.String PROP_JMX_ENABLED
        The jmxEnabled property name.
        See Also:
        Constant Field Values
      • PROP_FAIR_QUEUE

        protected static final java.lang.String PROP_FAIR_QUEUE
        The fairQueue property name.
        See Also:
        Constant Field Values
      • PROP_USE_EQUALS

        protected static final java.lang.String PROP_USE_EQUALS
        The useEquals property name.
        See Also:
        Constant Field Values
      • PROP_USE_LOCK

        protected static final java.lang.String PROP_USE_LOCK
        The useLock property name.
        See Also:
        Constant Field Values
      • PROP_DATASOURCE

        protected static final java.lang.String PROP_DATASOURCE
        The dataSource property name.
        See Also:
        Constant Field Values
      • PROP_DATASOURCE_JNDI

        protected static final java.lang.String PROP_DATASOURCE_JNDI
        The dataSourceJNDI property name.
        See Also:
        Constant Field Values
      • PROP_SUSPECT_TIMEOUT

        protected static final java.lang.String PROP_SUSPECT_TIMEOUT
        The suspectTimeout property name.
        See Also:
        Constant Field Values
      • PROP_ALTERNATE_USERNAME_ALLOWED

        protected static final java.lang.String PROP_ALTERNATE_USERNAME_ALLOWED
        The alternateUsernameAllowed property name.
        See Also:
        Constant Field Values
      • PROP_COMMITONRETURN

        protected static final java.lang.String PROP_COMMITONRETURN
        The commitOnReturn property name.
        See Also:
        Constant Field Values
      • PROP_ROLLBACKONRETURN

        protected static final java.lang.String PROP_ROLLBACKONRETURN
        The rollbackOnReturn property name.
        See Also:
        Constant Field Values
      • PROP_USEDISPOSABLECONNECTIONFACADE

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

        protected static final java.lang.String PROP_LOGVALIDATIONERRORS
        The logValidationErrors property name.
        See Also:
        Constant Field Values
      • PROP_PROPAGATEINTERRUPTSTATE

        protected static final java.lang.String PROP_PROPAGATEINTERRUPTSTATE
        The propagateInterruptState property name.
        See Also:
        Constant Field Values
      • PROP_IGNOREEXCEPTIONONPRELOAD

        protected static final java.lang.String PROP_IGNOREEXCEPTIONONPRELOAD
        The ignoreExceptionOnPreLoad property name.
        See Also:
        Constant Field Values
      • PROP_USESTATEMENTFACADE

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

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

        public static final java.lang.String OBJECT_NAME
        Property name for the JMX object name.
        See Also:
        Constant Field Values
      • ALL_PROPERTIES

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

      • DataSourceFactory

        public DataSourceFactory()
        Default constructor.
    • Method Detail

      • getObjectInstance

        public java.lang.Object getObjectInstance​(java.lang.Object obj,
                                                  javax.naming.Name name,
                                                  javax.naming.Context nameCtx,
                                                  java.util.Hashtable<?,​?> environment)
                                           throws java.lang.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 javax.naming.spi.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:
        java.lang.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
        javax.naming.NamingException - if the name given by the dataSourceJNDI attribute does not resolve to a data source that can back the pool
        java.lang.Exception - if an exception occurs creating the instance
      • parsePoolProperties

        public static PoolConfiguration parsePoolProperties​(java.util.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:
        java.lang.IllegalArgumentException - if an attribute carries a value its property cannot take
      • createDataSource

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

        public javax.sql.DataSource createDataSource​(java.util.Properties properties,
                                                     javax.naming.Context context,
                                                     boolean XA)
                                              throws java.lang.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:
        java.lang.Exception - if an error occurs creating the data source
      • createDataSource

        protected javax.sql.DataSource createDataSource​(java.util.Properties properties,
                                                        javax.naming.Context context,
                                                        boolean XA,
                                                        java.lang.Object dataSourceObject)
                                                 throws java.lang.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:
        java.lang.Exception - if an error occurs creating the data source
      • performJNDILookup

        public void performJNDILookup​(javax.naming.Context context,
                                      PoolConfiguration poolProperties)
                               throws javax.naming.NamingException
        Perform a JNDI lookup for the data source.
        Parameters:
        context - The JNDI context
        poolProperties - The pool configuration
        Throws:
        javax.naming.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 java.util.Properties getProperties​(java.lang.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