I am learning how to use hibernate (5.4.32.Final), and I found update in my transaction are always reordered by the primary key. After look up document, I know there is a property hibernate.order_updates, which can force hibernate to change the order of udpates. But its default value is false, and even I explictly set it as false, the order of update operations in transaction still changed (not same as the order of persist call).
I know this reorder can help reduce the probability of deadlock under high concurrency, but I still hope I can control the order of update statements without manually flush between them. What should I do?
The following is my code and log.
T1 is a table containing 2 columns id and value, and T2 is a table containing 3 columns id, t1_id and values. They have OneToMany relationship.
And the order of update operations is changed by hibernate.
T1 t1 = getById(session, row_id, false);
List<T2> t2s = t1.getT2();
Collections.shuffle(t2s);
Random random = new Random();
transaction.begin();
for(T2 t2 : t2s){
t2.setValue(random.nextInt(100000));
session.persist(t2);
// session.flush();
}
t1.setValue(random.nextInt(100000));
session.persist(t1);
transaction.commit();
2021-06-30 20:03:41 [Thread-2] DEBUG org.hibernate.SQL - update T1 set value=? where id=?
2021-06-30 20:03:41 [Thread-2] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [1] as [BIGINT] - [63221]
2021-06-30 20:03:41 [Thread-2] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [37]
2021-06-30 20:03:41 [Thread-2] DEBUG org.hibernate.SQL - update T2 set t1_id=?, value=? where id=?
2021-06-30 20:03:41 [Thread-2] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [1] as [BIGINT] - [37]
2021-06-30 20:03:41 [Thread-2] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [74295]
2021-06-30 20:03:41 [Thread-2] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [3] as [BIGINT] - [361]
2021-06-30 20:03:41 [Thread-2] DEBUG org.hibernate.SQL - update T2 set t1_id=?, value=? where id=?
2021-06-30 20:03:41 [Thread-2] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [1] as [BIGINT] - [37]
2021-06-30 20:03:41 [Thread-2] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [16209]
2021-06-30 20:03:41 [Thread-2] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [3] as [BIGINT] - [362]
2021-06-30 20:03:41 [Thread-2] DEBUG org.hibernate.SQL - update T2 set t1_id=?, value=? where id=?
2021-06-30 20:03:41 [Thread-2] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [1] as [BIGINT] - [37]
2021-06-30 20:03:41 [Thread-2] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [31364]
2021-06-30 20:03:41 [Thread-2] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [3] as [BIGINT] - [363]
2021-06-30 20:03:41 [Thread-2] DEBUG org.hibernate.SQL - update T2 set t1_id=?, value=? where id=?
2021-06-30 20:03:41 [Thread-2] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [1] as [BIGINT] - [37]
2021-06-30 20:03:41 [Thread-2] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [17351]
2021-06-30 20:03:41 [Thread-2] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [3] as [BIGINT] - [364]
2021-06-30 20:03:41 [Thread-2] DEBUG org.hibernate.SQL - update T2 set t1_id=?, value=? where id=?
2021-06-30 20:03:41 [Thread-2] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [1] as [BIGINT] - [37]
2021-06-30 20:03:41 [Thread-2] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [50964]
2021-06-30 20:03:41 [Thread-2] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [3] as [BIGINT] - [365]
2021-06-30 20:03:41 [Thread-2] DEBUG org.hibernate.SQL - update T2 set t1_id=?, value=? where id=?
2021-06-30 20:03:41 [Thread-2] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [1] as [BIGINT] - [37]
2021-06-30 20:03:41 [Thread-2] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [25135]
2021-06-30 20:03:41 [Thread-2] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [3] as [BIGINT] - [366]
2021-06-30 20:03:41 [Thread-2] DEBUG org.hibernate.SQL - update T2 set t1_id=?, value=? where id=?
2021-06-30 20:03:41 [Thread-2] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [1] as [BIGINT] - [37]
2021-06-30 20:03:41 [Thread-2] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [79969]
2021-06-30 20:03:41 [Thread-2] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [3] as [BIGINT] - [367]
2021-06-30 20:03:41 [Thread-2] DEBUG org.hibernate.SQL - update T2 set t1_id=?, value=? where id=?
2021-06-30 20:03:41 [Thread-2] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [1] as [BIGINT] - [37]
2021-06-30 20:03:41 [Thread-2] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [53048]
2021-06-30 20:03:41 [Thread-2] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [3] as [BIGINT] - [368]
2021-06-30 20:03:41 [Thread-2] DEBUG org.hibernate.SQL - update T2 set t1_id=?, value=? where id=?
2021-06-30 20:03:41 [Thread-2] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [1] as [BIGINT] - [37]
2021-06-30 20:03:41 [Thread-2] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [23097]
2021-06-30 20:03:41 [Thread-2] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [3] as [BIGINT] - [369]
2021-06-30 20:03:41 [Thread-2] DEBUG org.hibernate.SQL - update T2 set t1_id=?, value=? where id=?
2021-06-30 20:03:41 [Thread-2] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [1] as [BIGINT] - [37]
2021-06-30 20:03:41 [Thread-2] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [71799]
2021-06-30 20:03:41 [Thread-2] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [3] as [BIGINT] - [370]