I have a struts application, deployed using tomcat 5, that uses database
pooling. I have a helper class called DAOhelper that encapsulates database
interactions. The problem I have is that the class only works during
application initialisation. I have an ApplicationInitaliser class that
extends Plugin. In this class I can use my DAOHelper to acces the database,
however the class does not work when invoked in an action.

The DAOhelper class contains the following method

private static DataSource getDataSource() throws DAOException
{
        logger.debug(IConstants.ENTERING);
        DataSource dataSource = null;
                
        try
        {
                Context initCtx = new InitialContext();
                Context envCtx = (Context) initCtx.lookup("java:comp/env");
        
                //Look up our data source
                dataSource = (DataSource)envCtx.lookup("jdbc/Frontline");
                                                
                logger.debug("DataSource retrieved successfully");
        }
        catch (NamingException e)
        {
                String errorMessage = IConstants.DATA_SOURCE_ERROR + " using 
context
jdbc/Frontline";
                logger.error(errorMessage);
                logger.error(e.getMessage(),e);
                throw new DAOException(errorMessage,e);
        }
                
        logger.debug(IConstants.EXITING);
        return dataSource;
}

The line Context envCtx = (Context) initCtx.lookup("java:comp/env"); throws
a javax.naming.NameNotFoundException when the class is used inside an
action, but works fine during initialising in the init method.

The excpetion message is Name java:comp is not bound in this Context.

Can anyone tell me why this is happening? What I don't understand is how I
can successfully get a connection from within my ApplicationInitialiser
class and cannot get an connetion within an action.

The complete clas listing is below.

/**
 * 
 */
package com.lagan.ini.thinclient.usermanagement;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import org.apache.log4j.Logger;

import com.lagan.ini.frontlinedata.IConstants;
import com.lagan.ini.frontlinedata.dao.DAOException;
import com.lagan.ini.frontlinedata.dao.IDAOFactory;
import com.lagan.ini.frontlinedata.dao.IDao;
import com.lagan.ini.frontlinedata.dao.IDivisionDAO;
import com.lagan.ini.frontlinedata.dao.IJobTitleDAO;
import com.lagan.ini.frontlinedata.dao.ITeamDAO;
import com.lagan.ini.frontlinedata.dao.IUserDAO;
import com.lagan.ini.frontlinedata.dao.IUserInfoDAO;
import com.lagan.ini.frontlinedata.jdbcdao.JDBCDaoFactory;

/**
 * @author SeanMcE
 *
 */
public class DAOHelper
{

        //------------------------------------------------
        //Private Members
        //------------------------------------------------
        
        private static Logger logger = Logger.getLogger(DAOHelper.class);       
        
        private static final ThreadLocal<IDAOFactory> threadDaoFactory 
                = new ThreadLocal<IDAOFactory>();
        
        //------------------------------------------------
        //Constructors
        //------------------------------------------------
        /**
         * 
         */
        public DAOHelper()
        {
        }
        
        //------------------------------------------------
        //Public Methods
        //------------------------------------------------

        public static IUserDAO getUserDAO() throws DAOException
        {               
                return getDAOFactory().getUserDAO();
        }
        
        public static IUserInfoDAO getUserInfoDAO() throws DAOException
        {               
                return getDAOFactory().getUserInfoDAO();
        }
        
        public static ITeamDAO getTeamDAO() throws DAOException
        {               
                return getDAOFactory().getTeamDAO();
        }
        
        public static IDivisionDAO getDivisionDAO() throws DAOException
        {
                return getDAOFactory().getDivsionDAO();
        }
        
        public static IJobTitleDAO getJobTitleDAO() throws DAOException
        {
                return getDAOFactory().getJobTitleDAO();
        }
        
        public static IDAOFactory getDAOFactory() throws DAOException
        {
                IDAOFactory daoFactory = getThreadDaoFactory().get();
                
                if(daoFactory == null)
                {
                        daoFactory = createDaoFactory();
                        getThreadDaoFactory().set(daoFactory);
                }
                
                return daoFactory;
        }
        
        public static void closeDAOConnection(IDao dao)
        {
                if(dao != null)
                {
                        dao.close();
                }
        }
        //------------------------------------------------
        //Protected Methods
        //------------------------------------------------

        //------------------------------------------------
        //Private Methods
        //------------------------------------------------

        private static ThreadLocal<IDAOFactory> getThreadDaoFactory()
        {
                return threadDaoFactory;
        }
        
        private static IDAOFactory createDaoFactory() throws DAOException
        {
                logger.debug(IConstants.ENTERING);

                IDAOFactory daoFactory = null;
                
                if(IConstants.JDBC_DAO_FACTORY.equals(getDaoFactory()))
                {
                        daoFactory = new JDBCDaoFactory(getDataSource());
                }
                
                logger.debug(IConstants.EXITING);
                return daoFactory;
        }
        
        private static DataSource getDataSource() throws DAOException
        {
                logger.debug(IConstants.ENTERING);
                DataSource dataSource = null;
                
                try
                {
                        Context initCtx = new InitialContext();
                        Context envCtx = (Context) 
initCtx.lookup("java:comp/env");
        
                        //Look up our data source
                        dataSource = 
(DataSource)envCtx.lookup("jdbc/Frontline");
                                                
                        logger.debug("DataSource retrieved successfully");
                }
                catch (NamingException e)
                {
                        String errorMessage = IConstants.DATA_SOURCE_ERROR + " 
using context
jdbc/Frontline";
                        logger.error(errorMessage);
                        logger.error(e.getMessage(),e);
                        throw new DAOException(errorMessage,e);
                }
                
                logger.debug(IConstants.EXITING);
                return dataSource;
        }
        
        private static String getDaoFactory() throws DAOException
        {
                logger.debug(IConstants.ENTERING);
                
                InputStream daoPropertiesIs = Thread
                                                                                
.currentThread()
                                                                                
.getContextClassLoader()
                                                                                
.getResourceAsStream(IConstants.DAO_PROPERTIES);
                
                Properties daoProperties = new Properties();
                try
                {
                        daoProperties.load(daoPropertiesIs);
                }
                catch (IOException e)
                {
                        new DAOException(e);
                }
                finally
                {
                        closeInputStream(daoPropertiesIs);
                }
                
                logger.debug(IConstants.EXITING);
                return daoProperties.getProperty(IConstants.DAO_FACTORY_TYPE);
        }
        
        private static void closeInputStream(InputStream is)
        {
                if(is != null)
                {
                        try
                        {
                                is.close();
                                logger.debug("Properties file closed 
successfully");
                        }
                        catch (IOException e)
                        {
                                logger.error("Error closing Properties File: " 
+ e.getMessage());
                        }
                }
        }
}

-- 
View this message in context: 
http://www.nabble.com/Struts-and-JNDI-tf4372040.html#a12461293
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to