How to organize synchronization of a method in JVM?
My example is a web application that restricts the registration of a username more than once (in other words, the first user can log in, but if another user signs up with the same username, he gets rejected).
The web application is deployed on multiple servers, so there are several JVMs, and users may try to log on to different servers depending on load balancing.
Here is the method
public synchronized static SessionData logonSync(String userName, String password) throws Exception { int count = DB.count("sessions", "WHERE user_name=?", userName); if (count > 0) { throw new Exception("logon.error.loginAlreadyUsed"); } return logon(userName, password); }
It works correctly on 1 application server due to the synchronized method, but after several JVMs? Two users can simultaneously try to log in to different web applications. How can I prevent this?
* EDIT * The application also uses Memcached if your solution wants to use some transactional caching techniques.
java concurrency synchronized jvm
bluedevil2k
source share