Results 1 to 4 of 4
- 07-26-2007, 12:13 PM #1
Member
- Join Date
- Jul 2007
- Posts
- 2
- Rep Power
- 0
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
Once the DAO factory is retrieved it is passed to a DAOHelper class like thisJava 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; }The DAOHelper class has two methods:Java Code:DAOHelper.setDaoFactory(getDaoFactory());
This now enables any class intance to get the DAO factory. What I want to know is - is this safe. If two threads callJava Code:public static IDAOFactory getDaoFactory() { return daoFactory; } public static void setDaoFactory(IDAOFactory daoFactory) { DAOHelper.daoFactory = daoFactory; }at the same time what will happen? Should I synchronize theJava Code:DAOHelper.getDaoFactory()
mathod?Java Code:DAOHelper.getDaoFactory()
Thanks.
- 08-08-2007, 01:01 AM #2
Senior Member
- Join Date
- Mar 2007
- Posts
- 134
- Rep Power
- 0
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
- 08-08-2007, 02:25 PM #3
Member
- Join Date
- Jul 2007
- Posts
- 2
- Rep Power
- 0
Thanks for your reply. Can you tell me what you mean by 'bind it'?
Thanks again.
- 08-08-2007, 11:51 PM #4
Senior Member
- Join Date
- Mar 2007
- Posts
- 134
- Rep Power
- 0
Similar Threads
-
non-static member can not be referenced from a static context
By christina in forum New To JavaReplies: 3Last Post: 03-20-2009, 12:35 AM -
accessing instance variables from static methods
By ravian in forum New To JavaReplies: 7Last Post: 03-01-2009, 10:09 PM -
Explicit static initialization with the static clause
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 11:07 PM -
Error: Non-static method append(char) cannot be referenced from a static context
By paul in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 05:05 AM -
Error: non-static variable height cannot be referenced from a static context at line
By fernando in forum AWT / SwingReplies: 1Last Post: 08-01-2007, 09:25 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks