Java Mailing List Archive

http://www.junlu.com/

Google
Google
Mailing List
Home
Forum Home
JBoss - Java Application Server
Tomcat - JSP/Servlet container
Struts - A MVC web framework
iText - An open source PDF Java Library
JDOM - JDOM XML Parser
J2EE - A mailing list for Java(tm) 2 Platform, Enterprise Edition
JSP - A mailing list about Java Server Pages specification and reference
J2EE Pattern - An interest list for Sun Java Center J2EE Pattern Catalog
Servlet - A mailing list for discussion about Sun Microsystem's Java Servlet API Technology
Struts & Hibernate
Subjects
JSP editor plugin for eclipse ?
org apache jasper JasperException: Unable to compile class for JSP
Tomcat: Connection reset by peer: socket write error
Cannot retrieve definition for form bean null
Struts Tiles Tutorial (free Struts training)
Where do I download Tomcat 4 0 6?
Data Access Object (DAO) pattern, example DAO 's
Where to download Tomcat v 4 1 24 from?
Tomcat 5 0 16 Requested resource not available
Servlet : Session invalidate
Oracle Connection Pooling in 3 2 2
Servlet action is currently unavailable
Tomcat/Struts Unicode Encoding/Decoding problems
Running a Simple JMS Example
Tomcat and webapplication specific java library path
Mapping in workers2 properties
org apache jasper JasperException
problem with html:text bean throwing exception
Cannot find message resources under key org apache struts action
   MESSAGE
Cannot find message resources under key org apache struts action MESSAGE
invalid direct reference problem with solution
Tool for jsp debug Try Sysdeo Eclipse Plugin
Tomcat 5 Cannot load JDBC driver class 'null ' SQL state: null
weblogic ejbc
java properties file
Jboss 3 2 3 Coyote Can 't re
Tomcat 5, Apache2 and mod jk2 integration problem
JBoss example problem new to J2EE
Value attribute of <html:checkbox
url string for connecting jboss to oracle
javax servlet ServletException: BeanUtils populate
5 0 18: Windows XP Pro vs Windows 2000
HTTP Status 404 The requested resource is not available
 
Struts and JNDI

Struts and JNDI

2007-09-03       - By Mary Poppins

 Back

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;

/**
* @(protected) 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: user-unsubscribe@(protected)
For additional commands, e-mail: user-help@(protected)


©2008 junlu.com - Jax Systems, LLC, U.S.A.