I have a method:
private synchronized Long generateID (Short company) throws Exception {
IDDAO iDDAO = SpringApplicationContext.getBean (IDDAO.class);
ID iD = iDDAO.findByNaturalKey (new IDNatKey (company);
if (iD != null) {
// Check if ID has reached limit, then reset the ID to the first ID
if (iD.getLatestIDno ().longValue () == iD.getLastIDno ().longValue ()) {
iD.setLatestIDno (iD.getFrstIDno ());
}
// Get next ID
iD.setLatestIDno (iD.getLatestIDno () + 1);
// update database with latest id
iDDAO.update (iD);
return iD.getLatestIDno ();
}
}
In this code, I am updating the value of ID iD.setLatestIDno(iD.getLatestIDno() + 1)
. This is done in synchronized manner so that it never duplicates when accessed from multiple threads.
My question is if making this method synchronized will prevent other threads from accessing it? Or will other threads can access it from different objects? So shall it be made static?
The code is used like this
Long check = generateID (123);
Thanks