This week's giveaways are in the MongoDB and Jobs Discussion forums.
We're giving away four copies of Mongo DB Applied Patterns and 4 resume reviews from Five Year Itch and have the authors/reps on-line!
See this thread and this one for details.
The moose likes Servlets and the fly likes DB query from bean Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "DB query from bean" Watch "DB query from bean" New topic
Author

DB query from bean

John Schretz
Ranch Hand

Joined: Sep 10, 2008
Posts: 171
Is it ok and thread safe to call a static DAO method from a bean class?

I.E. Create a new cart object to be used in a servlet
then in that instance of cart make calls to the DB as ahown below.

Would this give me any problems?



Within cart class
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35220
    
    7
No, that's not thread-safe, since servlets are multi-threaded. Unless you make provisions for it (e.g., synchronizing that code section in the servlet, or making DiscountDB.GetNewUserDiscount() thread-safe) the code can run into trouble.


Android appsImageJ pluginsJava web charts
John Schretz
Ranch Hand

Joined: Sep 10, 2008
Posts: 171
Ulf Dittmer wrote:No, that's not thread-safe, since servlets are multi-threaded. Unless you make provisions for it (e.g., synchronizing that code section in the servlet, or making DiscountDB.GetNewUserDiscount() thread-safe) the code can run into trouble.


ok but i should be fine with executing DiscountDB.GetNewUserDiscount() right from the servlet then, correct?
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Servlets are at its own multithreaded. It is a very bad idea to synchronize servlet requests.

So either remove that 'static' modifier of the DAO method --this makes indeed no sense--, or make sure that your DAO method is written threadsafe (no access to static variables and having only method variables).


Code depot of a Java EE / JSF developer | JSF / Eclipse / Tomcat kickoff tutorial | DAO kickoff tutorial | I ♥ Unicode
John Schretz
Ranch Hand

Joined: Sep 10, 2008
Posts: 171
Bauke Scholtz wrote:Servlets are at its own multithreaded. It is a very bad idea to synchronize servlet requests.

So either remove that 'static' modifier of the DAO method --this makes indeed no sense--, or make sure that your DAO method is written threadsafe (no access to static variables and having only method variables).


I have moved DiscountNewUser newUserDiscI = DiscountDB.GetNewUserDiscount(); into the servlet. All variables are are method variables. So im good now. thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35220
    
    7
I have moved DiscountNewUser newUserDiscI = DiscountDB.GetNewUserDiscount(); into the servlet. All variables are are method variables. So im good now.

... only if DiscountDB.GetNewUserDiscount is thread-safe. Otherwise there'll be trouble.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
When he said " All variables are are method variables.", I think (and hope) that he actually meant the DB method.
John Schretz
Ranch Hand

Joined: Sep 10, 2008
Posts: 171
Bauke Scholtz wrote:When he said " All variables are are method variables.", I think (and hope) that he actually meant the DB method.


Yes
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: DB query from bean
 
Similar Threads
How assign to null an obj that contains other objs as attributes
How to store multiple items into a shopping cart?
logic:equal tag not evaluting inside html:button tag
Shopping cart application using struts
Unable to redirect the page through dispatcher