(Mary Poppins: 'Practically Perfect in Every Way')

This is a complicated scenario, what with a private static final ThreadLocal in 
a class with a public constructor.  Without coding it and tracing the 
ThreadLocal context, my guess is that the ThreadLocal is not being carried thru 
the various requests, as it is specific to the thread which created it, even 
though as a static final object, you would normally expect it to exist for all 
class instances.  You might try 2 things:  (1) make the class a singleton, and 
remove the ThreadLocal context for the Factory, or (2) use the 'ServiceLocator' 
pattern from the 'Core J2EE Patterns' book, now available on-line from Sun.

Is there a reason to use ThreadLocal context here?

Sorry I couldn't be more helpful.  

- Ray Clough



> ----- Original Message -----
> From: "Mary Poppins" <[EMAIL PROTECTED]>
> To: user@struts.apache.org
> Subject: Struts and JNDI
> Date: Mon, 3 Sep 2007 05:57:30 -0700 (PDT)
> 
> 
> 
> 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]

>



- Ray Clough
[EMAIL PROTECTED]



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

Reply via email to