Results 1 to 4 of 4
  1. #1
    sean is offline Member
    Join Date
    Jul 2007
    Posts
    2
    Rep Power
    0

    Default Accessing a static resouces in a web app.

    Hi,

    I have a struts application that creates a DAO factory on application initialisation.

    The DAO factory is initialised with a DataSource instance like this


    Java Code:
    private IDAOFactory getDaoFactory() throws InitialisationError
    {
    	logger.debug(IConstants.ENTERING);
    
    	IDAOFactory daoFactory = null;
    		
    	if(IConstants.JDBC_DAO_FACTORY.equals(getDaoFactoryType()))
    	{
    		daoFactory = new JDBCDaoFactory(getDataSource());
    	}
    		
    	logger.debug(IConstants.EXITING);
    	return daoFactory;
    }
    Once the DAO factory is retrieved it is passed to a DAOHelper class like this
    Java Code:
    DAOHelper.setDaoFactory(getDaoFactory());
    The DAOHelper class has two methods:

    Java Code:
    public static IDAOFactory getDaoFactory()
    {
    	return daoFactory;
    }
    
    public static void setDaoFactory(IDAOFactory daoFactory)
    {
    	DAOHelper.daoFactory = daoFactory;
    }
    This now enables any class intance to get the DAO factory. What I want to know is - is this safe. If two threads call
    Java Code:
    DAOHelper.getDaoFactory()
    at the same time what will happen? Should I synchronize the
    Java Code:
    DAOHelper.getDaoFactory()
    mathod?

    Thanks.

  2. #2
    goldhouse is offline Senior Member
    Join Date
    Mar 2007
    Posts
    134
    Rep Power
    0

    Default

    I would recomment to make the DaoFactory a singleton instance and bind it So every time you will check if there is already check for existing one if not present return a new one, I think that way you can avoid synchornization

  3. #3
    sean is offline Member
    Join Date
    Jul 2007
    Posts
    2
    Rep Power
    0

    Default

    Thanks for your reply. Can you tell me what you mean by 'bind it'?

    Thanks again.

  4. #4
    goldhouse is offline Senior Member
    Join Date
    Mar 2007
    Posts
    134
    Rep Power
    0

    Default

    Two choices one to make it public static ,
    second a better one JVM_Bind , the same way we used to bind it with datasource connections , If you dont know go with the statc thing

Similar Threads

  1. Replies: 3
    Last Post: 03-20-2009, 12:35 AM
  2. accessing instance variables from static methods
    By ravian in forum New To Java
    Replies: 7
    Last Post: 03-01-2009, 10:09 PM
  3. Replies: 0
    Last Post: 04-16-2008, 11:07 PM
  4. Replies: 1
    Last Post: 08-07-2007, 05:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 09:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •