0

When I do following:

em.persist(entity);
em.getTransaction().commit();

After execution of second line,I can see SQL Insert in traces and a row in DB inserted. But when I do:

em.persist(entity);
em.flush();
em.getTransaction().commit();

In this case, I can see SQL Insert in traces after execution of second line, but no new row is inserted in DB. That happens only after third line is executed.

Shouldn't an INSERT put a row in DB as soon as it is fired?

Mandroid
  • 6,200
  • 12
  • 64
  • 134

1 Answers1

2

Yes and no. Your statements are done in a transaction, which should be isolated from any other sessions until the transaction commits. Once the transaction commits, all modifications should be visible to other sessions.

Chris
  • 20,138
  • 2
  • 29
  • 43
  • I have a single session in my code,and there is no other session. – Mandroid Jan 10 '16 at 05:38
  • 2
    "Any other session" by @Chris is not limited to Hibernate session, it is in general any database session/connection that is opened. Also, even if there are no other "sessions/connections" open/active at that time, i.e., irrespective of sessions/connections opened/active, it is the responsibility of the database to ensure data is not written/visible to other transactions till it is committed. And that is why you don't see it till commit is done. – Madhusudana Reddy Sunnapu Jan 10 '16 at 05:47