• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DAO factory and database performance

 
Hanna Habashy
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all:
if DAO factory frame work is used in a multi- threaded J2EE project, when is it appropriate to cach a single object and return it over and over again, vs. retruning a brand new object.
for example:
//cached object
public class DAOFactory
{
privte EmployeeDAO MyEmployeeDAO = new EmplyeeDAO();
public EmplyeeDAO getEmplyeeDAO();
{ return MyEmployeeDAO;}
}
vs.
//new object
public class DAOFactory
{
public EmplyeeDAO getEmpoyeeDAO()
{ return new EmplyeeDAO();}
}
I really appreciate it if someone can explain to me in what situation should I use which technique, and if there is any performance implication in using any of them.
thanks
[ April 22, 2004: Message edited by: Hanna Habashy ]
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34965
378
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hannah,
If your DAO has a reference to any external object, you would want to cache it. For example, the DAO could read in the user id/password from a properties file.
 
Hanna Habashy
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne:
I don't understand your answer. If my DAO reads from an external object, like user name and password, why I would want to cach it? In this case the DAO object will be unique to that user, and I can't give the same DAO object to another user.
Maybe I didn't explain my question in a good way. In my question, I meant should I make the DAO classes as singletons, or not. In my mind, the DAOs are worker classes. User ask the controler about what they want, the controler delegate it to the DAOs, the DAOs do the job, then return it back to the controler. Does it matter if I have many objects of the same DAO, or one object can handle all the work?
Thnks
[ April 23, 2004: Message edited by: Hanna Habashy ]
 
Tom Maki
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you DAO's thread-safe (don't have any instance variables)? If so you can return a singleton, if not create a new instance each time.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34965
378
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hanna,
Not the user's id and password. The database username and password. Usually in a J2EE application, users log in (say through a web interface) and the server (DAO) submits requests to the database on their behalf. The individual users don't get separate ids to the database.
 
Hanna Habashy
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it. Thank you all
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic