0

I'm working on some legacy code and trying to understand what's going on. EJB 3 + Hibernate 3.2 (pray for my poor soul)

There are 2 EJBs (A and B): EJB-A retrieves an Object1 and modifies some field data.

SessionFactory sf = PersistenceManager.getSessionFactory;
Transaction tx = sessionContext.getUserTransaction;
tx.begin;
Session s = sf.getCurrentSesssion();
ObjectA a = (ObjectA)s.get(ObjectA.class, id);
a.setField(value);
s.update(a);
// other stuff happens and then transaction is committed
tx.commit();
// starts a new transaction?
tx = sessionContext.getUserTransaction();
tx.begin();
// Re initializes object
ObjectA a = (ObjectA)s.get(ObjectA.class, id);

EJB-B b = getService(EJB-b) // sorry pseudo coding this
b.performOperation();

Inside EJB-B, it reads the piece of data set in EJB-A. What I'm seeing is that the data is not updated when EJB-B reads it. I'm a bit confused, I assume that committing the transaction would force a flush and the session to push the data to the database.

Can someone explain to me what could be going on? Is it possible the commit call is performing some async call and is taking longer to actually persist the data than it takes to get to the next EJB and retrieve the data? Is there any way to get around that? Would it be possible to call tx.wasCommitted in a loop or something to wait until proceeding?

Thank you in advance for any insight.

Harley Jackson
  • 233
  • 1
  • 3
  • 13

1 Answers1

0

Hibernate doesn't flush the data instantaneously, it flushes when the internal cache reaches some limit or the transaction is closed. You can use flush here, refer Correct use of flush() in JPA/Hibernate.

  • Correct me if I'm wrong but since I'm explicitly committing my transaction (tx.commit()), that should be synching the data in the session with the database, ending the unit of work. Are you suggesting I should flush the data before I commit? Is this going to impact the data in EJB-B which is creating it's own session? – Harley Jackson Feb 17 '22 at 14:59