• Post Reply Bookmark Topic Watch Topic
  • New Topic

synchronization question  RSS feed

 
Lucy Smaile
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got quite far with a project (student), and now I'm thinking I should synchronize some of my database access methods.
I've got one class called DataBean which is an application scope bean, and which contains all of the methods that access the database. What I'm trying to figure out is how I should organise synchronizing some of its methods. I can't seem to relate this situation to the chapter I read about threads, which specifically creates and destroys threads. With JSP, I'd have thought it's different sessions whose access I want to synchronize?
Please can anyone throw any light on the question?
Thanks
 
Lucy Smaile
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How good a solution would it be just to mark all the necessary methods of DataBean as synchronized?
I read in Bruce Eckel Ch14 that:
"When you call any synchronized method, that object is locked and no other synchronized method of that object can be called until the first one finishes and releases the lock"
...so that would mean that no two synchronized methods of DataBean could run concurrently at all. I don't need that much security. If I just put the actual database access bits inside the methods in a block like
synchronized(this) {
// do actual database update
}
would that solve the problem better? I only need to stop versions of each individual bit of database code running concurrently with each other.
Am I missing anything important?
Thanks
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it depends on how your data access bean is written. Are you using one connection to the database or are you creating a connection each time a request comes in? Basically, do you use a member variable for your connection or do you use an automatic local variable inside your method? I would NOT suggest using a member variable, because that would mean you are sharing one database connection for all of your requests and you would need to do some sort of synchronization. I would suggest obtaining a new connection (ideally from a connection pooled DataSource object) each time you need one.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!