I have a student table in DB which has name, subject and marks fields in it. 1 student can have multiple records for different subjects in this table.
I have a student DAO class which has an update method like this:
public marks updateStudent(String name, String subject, int marks){
//this method first check if the record of this name and subject is there in DB
if(getStudentRecordFromDB(name, subject){
//then return marks for this student
}else{
//insert the record in DB and return marks
insertRecord(name, subject, marks);
}
}
This method can be called by multiple threads simultaneously for same student name and subject. I want to make it synchronized only in case of multiple threads updating same name and subject. So I thought on synchronizing this method on string of name+subject but as this is a bad practice also not giving me guranteed results due to string constant pool (presence of that string in it), I want to use some better solution. I don't want to synchronize on DAO.class as I want synchronization only in case of same records update. What should be the best way to do this?