spring - Hibernate CURRENT_TIMESTAMP and multi-entity transactions in MySQL -
i have 2 different entities (a & b) declared jpa , persisted hibernate (mysql 5.6.30, hibernate 3.6.8 , spring 4.2.1) in same transaction. both entities contains column:
`created_at` datetime(6) default current_timestamp(6)
when create & b in same transaction created_at
different & b.
my test using spring , defined this:
@autowired private transactiontemplate intx; @test public void test() { intx.execute(__ -> { arepository.add(a); brepository.add(b); return null; }); }
i'm quite sure entities in same transaction since add
metod in both arepository
, brepository
defined annotated this:
@transactional(propagation = propagation.mandatory)
i've tried changing test this:
@test public void test() { intx.execute(__ -> { arepository.add(a); thread.sleep(1000); brepository.add(b); return null; }); }
and yields result this:
java.lang.assertionerror: [a , b should have same timestamp] expecting: <[2016-05-09t14:16:28.531, 2016-05-09t14:16:28.532]> contain only: <[2016-05-09t14:16:28.531]> following elements unexpected: <[2016-05-09t14:16:28.532]>
i.e. timestamp close not same. intuition suggest created_at
& b exactly same since part of same transaction. questions are:
- is expected behavior or doing wrong?
- if expected behavior, there way persist transaction end-time instead of entity creation time? (so
created_at
timestamp same entities persisted in same transaction)
that indeed expected behavior. item inserted database value set (if isn't set before hand). hence depending on speed of server there difference.
if want use database control best bet trigger updates times based on transaction ending.
you in java setting current date before inserting. create date instance once , set on entities. way database default won't kick in.
Comments
Post a Comment