java - Using Spring @Transactional with a combination of readOnly and write, when does this entity get committed? -


given

class callingclass {   @autowired someservice someservice;   doit() {     someservice.readonlycall();   } }  @transactional(readonly = true) class someservice {   @autowired anotherservice anotherservice;   readonlycall() {     // logic may do...     anotherservice.writecall();   } }  class anotherservice {   @transactional // not readonly   writecall() {     // save entity   } } 

will entity committed database when writecall method exits or when readonlycall method exits?

and how can modify commits after writecall method?

the entity not committed database in both method exits. because

  • your someservice class has read-only (which have specify). set flushmode.never prevent commit transaction
  • your someservice class , anotherservice class has required propagation type(which default , have not specify other type both class). create new transaction if doesn't exist use current transaction if there is.
  • in case someservice class begin transaction read-only , anotherservice class participates in existing read-only transaction. therefore, no entity committed.

to make commits after writecall method, add setting on writecall method below:

@transactional (readonly = false, propagation =propagation.requires_new) // not readonly writecall() { // save entity } 

these settings have precedence writecall method. suspend current read-only transaction , create new read/write transaction. therefore, commits after writecall method complete transaction.


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -