Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-26-2007, 02:13 PM
Member
 
Join Date: Jul 2007
Posts: 2
sean is on a distinguished road
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


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
Code:
DAOHelper.setDaoFactory(getDaoFactory());
The DAOHelper class has two methods:

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
Code:
DAOHelper.getDaoFactory()
at the same time what will happen? Should I synchronize the
Code:
DAOHelper.getDaoFactory()
mathod?

Thanks.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-08-2007, 03:01 AM
Senior Member
 
Join Date: Mar 2007
Posts: 136
goldhouse is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-08-2007, 04:25 PM
Member
 
Join Date: Jul 2007
Posts: 2
sean is on a distinguished road
Thanks for your reply. Can you tell me what you mean by 'bind it'?

Thanks again.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-09-2007, 01:51 AM
Senior Member
 
Join Date: Mar 2007
Posts: 136
goldhouse is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Explicit static initialization with the static clause Java Tip java.lang 0 04-17-2008 01:07 AM
accessing instance variables from static methods ravian New To Java 5 11-08-2007 10:53 AM
Error: Non-static method append(char) cannot be referenced from a static context paul Advanced Java 1 08-07-2007 07:05 AM
Error: non-static variable height cannot be referenced from a static context at line fernando AWT / Swing 1 08-01-2007 11:25 PM
non-static member can not be referenced from a static context christina New To Java 1 07-31-2007 08:11 PM


All times are GMT +3. The time now is 08:10 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org