File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes DAO factory and database performance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "DAO factory and database performance" Watch "DAO factory and database performance" New topic
Author

DAO factory and database performance

Hanna Habashy
Ranch Hand

Joined: Aug 20, 2003
Posts: 532
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 ]

SCJD 1.4<br />SCJP 1.4<br />-----------------------------------<br />"With regard to excellence, it is not enough to know, but we must try to have and use it.<br />" Aristotle
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30076
    
149

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Hanna Habashy
Ranch Hand

Joined: Aug 20, 2003
Posts: 532
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

Joined: Apr 23, 2004
Posts: 1
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30076
    
149

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

Joined: Aug 20, 2003
Posts: 532
I got it. Thank you all
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DAO factory and database performance
 
Similar Threads
Settling out which factory pattern EJBHome uses
initialize DAO Factory
Data Access Object
using abstract DAO factory
Help me see if possible Generic DAO Factory