I am developing a code where there are a set of insert and select statements. This is a web based application using servlet/jsp. How can i ensure that this method is accessed by one request at a time.
Should I use:
- Synchronized method or Synchronized block
- Or a static synchronize method.
public void insertDetails()
{
//Select Statement
If //result of Select statment == "X"
insert1
else
insert 2
}
Generally, using synchronized to control database access means “you’re doing it wrong”. the database has all kinds of internal mechanisms to control concurrent access, so it is best to use them instead. among other reasons, using mechanisms in your jvm may limit future ability to scale out or have multiple apps (i.e. have 2 jvms utilizing the same database server’s data).