This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
Question concerning thread-safety and using JDBC in JavaBeans...I hope this makes some sense. I have a web application built that loosely uses the MVC architecture (like the Model 2 implementation of servlet-JavaBeans-JSP) with an RDBMS back-end. I have a datasource that is maintained (with pooled connections) in the app server (ex. JRun, Websphere, etc). One of my JavaBeans is my DataAccessBean, which has a number of methods that perform DB queries/calls/connections through JDBC and returns an instance of another non-data bean (which will be used in storage for request or session to be displayed in JSP). This DataAccessBean has a single "class variable" of Datasource type. Abridged code snippet follows:
When my controller servlet is invoked, the DataAccessBean is instantiated upon init() call and the DataSource member is instantiated with a call to bean's setDataSource() method (snippet follows):
To my understanding, the servlet architecture, while having one instance of a servlet, would normally contain multiple threads that can interact upon class variables and cause disruption. Would this apply to "class variables", such as the DataSource member in DBAccessBean, created in JavaBeans? Or any other factors with DataSource-type members that make it inherently thread-safe? [ April 18, 2002: Message edited by: Toby Davis ]
Joined: Apr 09, 2002
I confirmed, in short, any class instantiated within a servlet method is "thread-safe" since that object exists within the method's space. (Thanks Mike Curwen) However, my question has to do with the fact that this DBAccessBean (and its Datasource class member that was set) was instantiated one time in the init() method, it's available throughout the entire time that the instance is alive - regardless of the number of threads accessing it. Does the DBAccessBean remain thread-safe? (Moderator: please move this to "Servlets" or "Threads" forums, please - I don't want to double-post)