Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Connection Pool for Application Clients in JBoss 3.2

0.00/5 (No votes)
4 Oct 2004 1  
This article describes how to resolve the problem of connections of application clients with database in JBoss 3.2.x

Problem

The support of the application clients was added in JBoss in the version 3.2.0. That is why it is clear why the application client based on the response of one of the JBoss developers doesn’t support the pool of connections for application clients.

It has been issued in the very first release of JBoss 4.0. All the available documentation at the moment relates to version 3.2. That is why I think that the given information on how to resolve this problem in JBoss 3.2 could still be useful for someone else.

Solution - The Library Apache Common DBCP

One of the distributions of JBoss includes Apache Tomcat which uses for creation of connection tools one of the available implementations in the market is Apache Commons DBCP, an efficient package under the ASF license. This package is also used in other Apache projects such as James (Java Apache Mail Enterprise Server) and Avalon Framework. The package commons-DBCP depends on another Apache package commons-pool.

Implementation

import java.util.*;
import javax.naming.*;
import org.apache.log4j.*;

/**
 * The class verifies if JNDI tree contains the database connection pool 
 * initialized and if no performs the initialization.
 *
 * @author Yury Fedorov
 */
public abstract class ApplicationClient {
    
    private final String JNDI_NAME = "applicationClientPool";
    
    /**
     * The logger.
     */
    private static Logger logger = Logger.getLogger(ApplicationClient.class);

    /**
     * Creates a new instance of the class.
     * 
     * @throws ApplicationClientException
     */
    public ApplicationClient() throws ApplicationClientException {
        Context ctx = null;
        boolean isPoolInitialized = false;
        
        try {
            // Initialization of the context.
            ctx = new InitialContext();

            try {
                // Attempt to perform lookup for determination if 
                // the pool is already initialized.
                isPoolInitialized = ( ctx.lookup( JNDI_NAME ) != null );
            } catch (NamingException ne) {
                logger.warn("Impossible to perform lookup.");
            }

            if ( ! isPoolInitialized ) {
                // Initialization of the connection pool in the JNDI tree.
                // Construct BasicDataSource reference
                Reference ref = new Reference(
                    "javax.sql.DataSource",
                    "org.apache.commons.dbcp.BasicDataSourceFactory", 
                    null );
                ref.add( new StringRefAddr( "driverClassName", 
                    CommonNames.DRIVER_JDBC ) );
                ref.add( new StringRefAddr( "url", 
                    CommonNames.DATABASE_URL) );
                ref.add( new StringRefAddr( "username", 
                    CommonNames.DATABASE_LOGIN ) );
                ref.add( new StringRefAddr( "password", 
                    CommonNames.DATABASE_PASSWORD ) );
                ctx.rebind( JNDI_NAME, ref );
                logger.debug(
                    "Executed the bind of the data source." );
            }
            
        } catch (NamingException ne) {
            logger.fatal( "It is impossibile to use the JNDI service.", ne );
            throw new ApplicationClientException(
                "It is impossibile to use the JNDI service." );
        } catch (Exception ex) {
            logger.fatal( 
                "An unknown error happened during the initialization 
                 of the application client.", ex );
            throw new ApplicationClientException(
                "An unknown error happened during the initialization 
                 of the application client." );
        }
    }
}

History

  • 5th October, 2004: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here