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:

  1. is expected behavior or doing wrong?
  2. 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

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -