65.9K
CodeProject 正在变化。 阅读更多。
Home

JBoss 3.2 中应用程序客户端的连接池

starIconstarIconstarIconemptyStarIconemptyStarIcon

3.00/5 (2投票s)

2004年10月5日

CPOL
viewsIcon

40719

本文描述了如何解决 JBoss 3.2.x 中应用程序客户端与数据库连接的问题。

问题

JBoss 在 3.2.0 版本中添加了对应用程序客户端的支持。 这就是为什么基于 JBoss 开发者回复中的一个应用程序客户端不支持应用程序客户端的连接池的原因。

它是在 JBoss 4.0 的第一个版本中发布的。 目前所有可用的文档都与 3.2 版本相关。 因此,我认为关于如何在 JBoss 3.2 中解决此问题的相关信息仍然对其他人有用。

解决方案 - Apache Common DBCP 库

JBoss 的一个发行版包括 Apache Tomcat,它使用可用的市场实现之一 Apache Commons DBCP 来创建连接工具,这是一个在 ASF 许可下高效的软件包。 该软件包还用于其他 Apache 项目,例如 James(Java Apache Mail Enterprise Server)和 Avalon Framework。 commons-DBCP 软件包依赖于另一个 Apache 软件包 commons-pool。

实现

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." );
        }
    }
}

历史

  • 2004 年 10 月 5 日:初始发布
© . All rights reserved.