  | Mailing List | | Home | | Forum Home | | JBoss - Java Application Server | | Tomcat - JSP/Servlet container | | Struts - A MVC web framework | | iText - An open source PDF Java Library | | JDOM - JDOM XML Parser | | J2EE - A mailing list for Java(tm) 2 Platform, Enterprise Edition | | J2EE Pattern - An interest list for Sun Java Center J2EE Pattern Catalog | | JSP - A mailing list about Java Server Pages specification and reference | | Servlet - A mailing list for discussion about Sun Microsystem's Java Servlet API Technology | |
Struts & Hibernate
|
|
|
  | | | Singleton class issue | Singleton class issue 2004-11-17 - By Binildas
Back Bill,
The simplest (and preferred) method is to design the methods inside your singleton class as idempotent. This can be achieved by not using any writable, instance fields in your singleton class. If so, even if the methods are executed concurrently by multiple threads (either in a single CPU or multiple CPU m/c), each thread will be executing in its "separate, local stack memory" only.
Regards
Binildas Senior Technical Architect
http://www.infosys.com <http://www.infosys.com/>
-- --Original Message-- -- From: A mailing list for Java(tm) 2 Platform, Enterprise Edition [mailto:J2EE-INTEREST@(protected)] On Behalf Of bin cai Sent: Thursday, November 18, 2004 7:41 AM To: J2EE-INTEREST@(protected) 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@(protected) and include in the body of the message "signoff J2EE-INTEREST". For general help, send email to listserv@(protected) and include in the body of the message "help".
=========================================================================== To unsubscribe, send email to listserv@(protected) and include in the body of the message "signoff J2EE-INTEREST". For general help, send email to listserv@(protected) and include in the body of the message "help".
<html>
<head> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">
<meta name=Generator content="Microsoft Word 10 (filtered)">
<style> <!-- /* Font Definitions */ @(protected) {font-family:Tahoma; panose-1 (See http://ose-1.ora-code.com):2 11 6 4 3 5 4 4 2 4;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {margin:0in; margin-bottom:.0001pt; font-size:12.0pt; font-family:"Times New Roman";} a:link, span.MsoHyperlink {color:blue; text-decoration:underline;} a:visited, span.MsoHyperlinkFollowed {color:purple; text-decoration:underline;} p {margin-right:0in; margin-left:0in; font-size:12.0pt; font-family:"Times New Roman";} span.EmailStyle18 {font-family:Arial; color:navy;} @(protected) Section1 {size:8.5in 11.0in; margin:1.0in 1.25in 1.0in 1.25in;} div.Section1 {page:Section1;} --> </style>
</head>
<body lang=EN-US link=blue vlink=purple>
<div class=Section1>
<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size: 10.0pt;font-family:Arial;color:navy'>Bill,</span></font></p>
<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size: 10.0pt;font-family:Arial;color:navy'> </span></font></p>
<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size: 10.0pt;font-family:Arial;color:navy'>The simplest (and preferred) method is to design the methods inside your singleton class as idempotent. This can be achieved by not using any writable, instance fields in your singleton class. If so, even if the methods are executed concurrently by multiple threads (either in a single CPU or multiple CPU m/c), each thread will be executing in its “ ;separate, local stack memory” only.</span></font></p>
<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size: 10.0pt;font-family:Arial;color:navy'> </span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt; font-family:Arial'>Regards </span></font></p>
<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'> </span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt; font-family:Arial'>Binildas<br> Senior Technical Architect</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt; font-family:Arial'><a href="http://www.infosys.com/">http://www.infosys.com</a> </span></font></p>
<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size: 10.0pt;font-family:Arial;color:navy'> </span></font></p>
<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Tahoma><span style='font-size:10.0pt;font-family:Tahoma'>-- --Original Message-- --<br> <b><span style='font-weight:bold'>From:</span></b> A mailing list for Java(tm) 2 Platform, Enterprise Edition [mailto:J2EE-INTEREST@(protected)] <b><span style='font-weight:bold'>On Behalf Of </span></b>bin cai<br> <b><span style='font-weight:bold'>Sent:</span></b> Thursday, November 18, 2004 7:41 AM<br> <b><span style='font-weight:bold'>To:</span></b> J2EE-INTEREST@(protected)<br> <b><span style='font-weight:bold'>Subject:</span></b> Singleton class issue< /span></font></p>
<p class=MsoNormal style='margin-left:.5in'><font size=3 face="Times New Roman" ><span style='font-size:12.0pt'> </span></font></p>
<div>
<p class=MsoNormal style='margin-left:.5in'><font size=3 face="Times New Roman" ><span style='font-size:12.0pt'>Hello,</span></font></p>
</div>
<div>
<p class=MsoNormal style='margin-left:.5in'><font size=3 face="Times New Roman" ><span style='font-size:12.0pt'>I am developing web application based on j2ee technology:jsp, java bean and jdbc.</span></font></p>
</div>
<div>
<p class=MsoNormal style='margin-left:.5in'><font size=3 face="Times New Roman" ><span style='font-size:12.0pt'>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.</span></font></p>
</div>
<div>
<p class=MsoNormal style='margin-left:.5in'><font size=3 face="Times New Roman" ><span style='font-size:12.0pt'>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.</span></font></p>
</div>
<div>
<p class=MsoNormal style='margin-left:.5in'><font size=3 face="Times New Roman" ><span style='font-size:12.0pt'>So which way is better according to you guys' experience. Please let me know.</span></font></p>
</div>
<div>
<p class=MsoNormal style='margin-left:.5in'><font size=3 face="Times New Roman" ><span style='font-size:12.0pt'>Thanks</span></font></p>
</div>
<div>
<p class=MsoNormal style='margin-left:.5in'><font size=3 face="Times New Roman" ><span style='font-size:12.0pt'>bill </span></font></p>
</div>
<p style='margin-left:.5in'><font size=3 face="Times New Roman"><span style='font-size:12.0pt'>__ ____ ____ ____ ____ ____ ____ ____ ____ ____ __<br> Do You Yahoo!?<br> Tired of spam? Yahoo! Mail has the best spam protection around <br> http://mail.yahoo.com ========================================================= ================== To unsubscribe, send email to listserv@(protected) and include in the body of the message "signoff J2EE-INTEREST". For general help, send email to listserv@(protected) and include in the body of the message "help". </span></font></p>
</div>
</body>
</html> =========================================================================== To unsubscribe, send email to listserv@(protected) and include in the body of the message "signoff J2EE-INTEREST". For general help, send email to listserv@(protected) and include in the body of the message "help". <p>
|
|
 |