Click here to Skip to main content
15,885,278 members
Articles / Programming Languages / Java / Java SE

Connection Pool for Application Clients in JBoss 3.2

Rate me:
Please Sign up or sign in to vote.
3.00/5 (2 votes)
4 Oct 2004CPOL 40.2K   10  
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

Java
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Switzerland Switzerland
* 20 years in software development;
* 15 years in financial services;
* 2 master degrees: in IT and Quantitative Finance;
Specialisations: back-end software development (C#, C++, Java), Oracle.

Comments and Discussions

 
-- There are no messages in this forum --