Skip to content

Commit d92d15c

Browse files
authored
Merge pull request #33594 from njr-11/reproduce-33544-with-entitymanager
Reproduce #33544 with EntityManager only
2 parents 27f213c + bbc1ce3 commit d92d15c

File tree

2 files changed

+91
-2
lines changed
  • dev
    • io.openliberty.data.internal.persistence/src/io/openliberty/data/internal/persistence
    • io.openliberty.data.internal_fat_jpa/test-applications/DataJPATestApp/src/test/jakarta/data/jpa/web/hibernate

2 files changed

+91
-2
lines changed

dev/io.openliberty.data.internal.persistence/src/io/openliberty/data/internal/persistence/RepositoryImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -603,9 +603,10 @@ else if ("toString".equals(methodName))
603603
Tr.debug(this, tc, "flush");
604604
em.flush();
605605
// TODO 1.1 only detach if a stateless repository
606-
if (entityInfo != null && !entityInfo.isHibernate) {
606+
if (!entityInfo.isHibernate && // TODO remove this condition once #33544 is fixed
607+
entityInfo != null) {
607608
// Only valid if flush writes to the database,
608-
// and Hibernate does not seem to honor flush.
609+
// and Hibernate does not seem to honor flush. #33544
609610
if (trace && tc.isDebugEnabled())
610611
Tr.debug(this, tc, "clear");
611612
em.clear();

dev/io.openliberty.data.internal_fat_jpa/test-applications/DataJPATestApp/src/test/jakarta/data/jpa/web/hibernate/DataJPAHibernateServlet.java

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,94 @@ public void testCountQuery_WHERE_SELECT_ORDER() {
146146
assertEquals(10, page1.totalElements());
147147
}
148148

149+
/**
150+
* Reproduces an issue where updates are made and a flush is requested
151+
* during a transaction but not honored, so if the entity is detached
152+
* after that point, the updates are lost.
153+
*/
154+
@Test
155+
public void testFlushAndDetachDuringTransaction() throws Exception {
156+
EntityManagerFactory emf = InitialContext
157+
.doLookup("java:comp/env/persistence/HibernatePersistenceUnitRef");
158+
UserTransaction tx = InitialContext
159+
.doLookup("java:comp/UserTransaction");
160+
161+
SimpleEntity entity = new SimpleEntity();
162+
entity.id = 2;
163+
entity.value = "new";
164+
165+
EntityManager em = null;
166+
167+
tx.begin();
168+
try {
169+
em = emf.createEntityManager();
170+
em.setCacheRetrieveMode(CacheRetrieveMode.BYPASS);
171+
assertEquals(true, em.isJoinedToTransaction());
172+
173+
em.persist(entity);
174+
} finally {
175+
if (tx.getStatus() == Status.STATUS_ACTIVE)
176+
tx.commit();
177+
else
178+
tx.rollback();
179+
em.clear();
180+
em.close();
181+
}
182+
183+
tx.begin();
184+
try {
185+
em = emf.createEntityManager();
186+
em.setCacheRetrieveMode(CacheRetrieveMode.BYPASS);
187+
assertEquals(true, em.isJoinedToTransaction());
188+
189+
entity = em.find(SimpleEntity.class, 2);
190+
entity.value = "flushed";
191+
192+
entity = em.merge(entity);
193+
194+
// JPA 3.2 spec states:
195+
// "The flush method can be used by the application to force synchronization."
196+
// EntityManager Javadoc states:
197+
// "The client may force an immediate flush to occur by calling flush()."
198+
// EntityManager.flush is defined as:
199+
// "Synchronize changes held in the persistence context to the underlying database."
200+
201+
em.flush(); // this is ignored
202+
203+
// EntityManager.detach Javadoc says:
204+
// "Unflushed changes made to the entity, if any, including deletion
205+
// of the entity, will never be synchronized to the database."
206+
// However, we did invoke flush.
207+
//em.detach(entity);
208+
209+
// EntityManager.clear Javadoc says:
210+
// "Changes made to entities that have not already been flushed to
211+
// the database will never be made persistent."
212+
// However, we did invoke flush.
213+
em.clear();
214+
} finally {
215+
if (tx.getStatus() == Status.STATUS_ACTIVE)
216+
tx.commit();
217+
else
218+
tx.rollback();
219+
em.clear();
220+
em.close();
221+
}
222+
223+
try {
224+
em = emf.createEntityManager();
225+
em.setCacheRetrieveMode(CacheRetrieveMode.BYPASS);
226+
227+
entity = em.find(SimpleEntity.class, 2);
228+
} finally {
229+
em.clear();
230+
em.close();
231+
}
232+
233+
// TODO enable once #33544 is fixed
234+
// assertEquals("flushed", entity.value);
235+
}
236+
149237
/**
150238
* Reproduces an issue where a previously detached entity is merged to the
151239
* persistence context in order to make an update, but Hibernate never

0 commit comments

Comments
 (0)