0

I have two application First Application will persist into a database in a while loop , the loop will end after a long time (say 10-15 minutes). But The second application needs the data that the first application has already persisted in the database , The second application cannot wait for the first application to finish .It will start just after the first application has started running. I have used an EntityManager.flush() in the first application hoping that the first application will immediately synch the data with the database. So that the second application which is in a different transaction can start working with data .

This is not working , what is the purpose of flush() method than and how can i solve my problem ? Please help !!

Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
web2dev
  • 557
  • 10
  • 28

2 Answers2

6

flush() writes all the pending changes to the database. It executes insert, update and delete statements. It's (by default) automatically called before committing the transaction.

But just because you flushed doesn't mean other transactions will see the changes. This depends on the transaction isolation level, which, in most databases, is READ_COMMITTED. Transactions run in isolation from each other (the I in ACID). So, if your isolation level is READ_COMMITTED, the transaction in the second app won't see any change done in the transaction of the first app until this transaction has committed.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • I have my tx_isolation set to value REPEATABLE-READ , Which Isolation level is most appropriate from the above scenario . – web2dev Jul 02 '12 at 03:18
  • And again , I do not want to lock other transaction from reading and updating the data , what I want is the latest value . – web2dev Jul 02 '12 at 03:50
  • Then either you split between several trnsactions as Stefan suggests, or you change the isolation level of your database connections to READ_UNCOMMITTED. – JB Nizet Jul 02 '12 at 07:21
  • Thanks for mentioning about transaction isolation layer. – kolobok Aug 07 '14 at 11:21
2

It sounds as if you should split the work of the first application into separate transactions. Changes are normally only visible to other transaction after commit (as explained by JB Nizet. You could change that, but you shouldn't).

Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193
  • I cannnot split my transactions as i may need a transaction rollback from the beginning if anything wrong happens .. – web2dev Jul 02 '12 at 03:20
  • How should this work? When you rollback the first transaction, but the second alread read the data and processed it?? – Stefan Steinegger Jul 02 '12 at 05:56
  • Ummmm ... i did not think about it . Then , I have no other solution rather then splitting it . Thanks a lot . – web2dev Jul 02 '12 at 09:48