Hi,
It is best to have a single connection and associated objects like statement, resultset etc for a single thread. Hence, for multi-threaded access, you may create your DAO layer like this:
have a singleton DAOFactory class having a static getInstance() method returning a single DAOFactory instance. This class will create DAO objects through its instance methods. Each of these DAO objects will have a connection as a private field. This connection will be picked up from the app server created pool. The client will call like this: call the DAOFactory.getInstance() and on the returned instance call the getDAO() method which will return a new DAO instance each time. ANd then work with that DAO object which is unique for a unique thread or request.
Hope you are able to understand.
Regards,
Partha
----------
From: A mailing list for Java(tm) 2 Platform, Enterprise Edition[SMTP:J2EE-INTEREST@JAVA.SUN.COM] on behalf of bin cai[SMTP:bcai_2003@YAHOO.COM]
Sent: Thursday, November 18, 2004 7:40 AM
To: J2EE-INTEREST@JAVA.SUN.COM
Subject: Singleton class issue
Hello,
I am developing web application based on j2ee technology:jsp, java bean and jdbc.
I ceated a singleton class called "JdbcController.java" that takes the responsibility to establishing connection to DB and works fine. But my concern is :since this is multi-thread application. every thread will invoke JDBCController class . i am wondering if this will result in concurrent issue becase every thread will compete to use this class to get connection to DB.
one alternative is :i don't use singleton pattern. when every thread needs to get access to DB, it will ceate new instance of JDBCControoler object. but it will use lot of memory although JVM has garbage collection mechanism.
So which way is better according to you guys' experience. Please let me know.
Thanks
bill
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com =========================================================================== To unsubscribe, send email to listserv@java.sun.com and include in the body of the message "signoff J2EE-INTEREST". For general help, send email to listserv@java.sun.com and include in the body of the message "help".