Class JNDIConnectionSource

  • All Implemented Interfaces:
    ConnectionSource, Component, org.apache.log4j.spi.OptionHandler

    public class JNDIConnectionSource
    extends ConnectionSourceSkeleton
    The JNDIConnectionSource is an implementation of ConnectionSource that obtains a DataSource from a JNDI provider and uses it to obtain a Connection. It is primarily designed to be used inside of J2EE application servers or application server clients, assuming the application server supports remote access of DataSources. In this way one can take advantage of connection pooling and whatever other goodies the application server provides.

    Sample configuration:

        <connectionSource class="org.apache.log4j.jdbc.JNDIConnectionSource">
            <param name="jndiLocation" value="jdbc/MySQLDS" />
        </connectionSource>
      

    Sample configuration (with username and password):

        <connectionSource class="org.apache.log4j.jdbc.JNDIConnectionSource">
            <param name="jndiLocation" value="jdbc/MySQLDS" />
            <param name="username" value="myUser" />
            <param name="password" value="myPassword" />
        </connectionSource>
      

    Note that this class will obtain an InitialContext using the no-argument constructor. This will usually work when executing within a J2EE environment. When outside the J2EE environment, make sure that you provide a jndi.properties file as described by your JNDI provider's documentation.

    • Field Detail

      • jndiLocation

        private java.lang.String jndiLocation
      • dataSource

        private javax.sql.DataSource dataSource
    • Constructor Detail

      • JNDIConnectionSource

        public JNDIConnectionSource()
    • Method Detail

      • activateOptions

        public void activateOptions()
        See Also:
        OptionHandler.activateOptions()
      • getConnection

        public java.sql.Connection getConnection()
                                          throws java.sql.SQLException
        Description copied from interface: ConnectionSource
        Obtain a Connection for use. The client is responsible for closing the Connection when it is no longer required.
        Throws:
        java.sql.SQLException - if a Connection could not be obtained
        See Also:
        ConnectionSource.getConnection()
      • getJndiLocation

        public java.lang.String getJndiLocation()
        Returns the jndiLocation.
        Returns:
        String
      • setJndiLocation

        public void setJndiLocation​(java.lang.String jndiLocation)
        Sets the jndiLocation.
        Parameters:
        jndiLocation - The jndiLocation to set
      • lookupDataSource

        private javax.sql.DataSource lookupDataSource()
                                               throws javax.naming.NamingException,
                                                      java.sql.SQLException
        Throws:
        javax.naming.NamingException
        java.sql.SQLException