• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DB query from bean

 
John Schretz
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
John Schretz
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
John Schretz
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When he said " All variables are are method variables.", I think (and hope) that he actually meant the DB method.
 
John Schretz
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bauke Scholtz wrote:When he said " All variables are are method variables.", I think (and hope) that he actually meant the DB method.


Yes
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic