0

I have 2 nodes of application and both of them updates the same db table. However, updated columns are different. In here the problem occurs and secondly running application updates the whole row instead single column. Let's deep dive into problem explanation.

The table name is "student" and it has 3 columns. Assuming ID, column1,column2

initial state of the row is ID=1, column1="abc", column2="xyz" The problematic scenario steps are below

  • App-instance-1 : reads the row and keep it in persistent state.
  • App-instance-2 : reads the row and keep it in persistent state.
  • App-instance-1 : updates the pojo attribute column1 as to be "ABC" (column1="ABC", column2="xyz").
  • App-instance-2 : updates the pojo attribute column2 as to be "XYZ" (column1="abc", column2="XYZ").
  • App-instance-1 : merges the pojo into db table and the row looks like ID=1, column1="ABC", column2="xyz"
  • App-instance-2 : merges the pojo into db table and the row looks like ID=1, column1="abc", column2="XYZ"

Finally, App-instances override the each other' s columns. I want to see the final row as column1="ABC", column2="XYZ". However, it is column1="abc", column2="XYZ".

My question is that why App-instance-1 updates the column2 even if no changes on the column2. column2 is not dirty on the instance-1. How can i overcome this issue?

@Entity
@Table("student")
public class Student {
    private Long ID;
    private String column1;
    private String column2;
    
    // Assuming other definations are ok.
}

Thanks in advance.

Okay Atalay
  • 71
  • 1
  • 9

1 Answers1

0

As far as my knowledge goes you could try one of those 2 methods :

  • Use optimistic locking (ex. @Version provided by Hibernate)
  • Create custom queries to update only one field instead of updating whole entity.
  • I will have a look for @Version. For the custom query, what happen when app tries to update both column1 and column2. It is not possible to understand which column is dirty for me. Hibernate should handle this one. When i change column1 only, the generated sql should be like update student set column1=? where ID=? . Because i just touch the column1. – Okay Atalay Jun 02 '21 at 18:59
  • Isn't it something that would solve your problem? https://stackoverflow.com/questions/2674516/hibernate-dirty-checking-and-only-update-of-dirty-attributes – Maciej Niedźwiedź Jun 02 '21 at 19:01
  • Absolutely the same issue:) i hope dynamic-update will solve my problem. Thank you so much – Okay Atalay Jun 02 '21 at 19:10