aspose file tools*
The moose likes JDBC and the fly likes database-access JavaBean - thread safety Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "database-access JavaBean - thread safety" Watch "database-access JavaBean - thread safety" New topic
Author

database-access JavaBean - thread safety

Toby Davis
Ranch Hand

Joined: Apr 09, 2002
Posts: 65
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 ]

SCJP2 (1.4)
Toby Davis
Ranch Hand

Joined: Apr 09, 2002
Posts: 65
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)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: database-access JavaBean - thread safety