java - spring-data postgres transaction isolation issue -
i have 2 entities: message
, session
. message
has @manytoone
relationship session
.
i have spring data repository , action a:
@query("select c messages c c.session.mode=0 , c.field=5") list<messages> findmessages();
then process found data
messages.foreach(message->{ session session = message.getsession(); sessionclose(session); newsessionopen(); })
and in other service class have
session session=findopenedsession();
the question is:
what if action in transaction , after starts , before ends other service request opened session
or try insert record message
table?
in other words have:
- transaction starts
- records being read
session
instances processed in loop -session
usingsessionclose
,newsessionopen
- transaction ends
what if other process request opened session
between 2 , 4 or 2 , 3 or somewhere within? opened session
read? old 1 or new one? use postgres , @transactional
spring annotation.
assuming 'session' in question refers entity created.
according this: https://www.postgresql.org/docs/9.1/static/transaction-iso.html default isolation level of postgres prevents dirty reads. no other transaction able see changes made in first transaction, until transaction commits (i.e. ends).
according same source can set transaction isolation level read uncommited. in such scenario second transaction might able see changes done first transaction, although first transaction not yet commited. note though changes entities not imediately written database, flushed before end of transaction, explicite call flush or if configured, before selects executed. although database settings might allow dirty reads might not experience them due behavior of jpa/hibernate
Comments
Post a Comment