use of org.hibernate.SessionBuilder in project hibernate-orm by hibernate.
the class TxUtil method withTxSession.
public static void withTxSession(JtaPlatform jtaPlatform, SessionBuilder sessionBuilder, ThrowingConsumer<Session, Exception> consumer) throws Exception {
if (jtaPlatform != null) {
TransactionManager tm = jtaPlatform.retrieveTransactionManager();
final SessionBuilder sb = sessionBuilder;
Caches.withinTx(tm, () -> {
withSession(sb, s -> {
consumer.accept(s);
s.flush();
});
return null;
});
} else {
withSession(sessionBuilder, s -> withResourceLocalTx(s, consumer));
}
}
use of org.hibernate.SessionBuilder in project hibernate-orm by hibernate.
the class QueryCacheTest method testDelayedLoad.
// @Test
// public void testGetByCompositeIdNoCache() {
// Query query = em.createQuery("FROM EntityWithCompositeKey e WHERE e.pk = :pk");
// query.setParameter("pk", PK);
// assertEquals(1, query.getResultList().size());
// }
//
// @Test
// public void testGetByEntityIself() {
// Query query = em.createQuery("FROM EntityWithCompositeKey e WHERE e = :ent");
// query.setParameter("ent", new EntityWithCompositeKey(PK));
// assertEquals(1, query.getResultList().size());
// }
@Test
@TestForIssue(jiraKey = "HHH-9962")
public /* Test courtesy of Giambattista Bloisi */
void testDelayedLoad() throws InterruptedException, ExecutionException {
DelayLoadOperations interceptor = new DelayLoadOperations();
final SessionBuilder sessionBuilder = sessionFactory().withOptions().interceptor(interceptor);
Item item1 = new Item();
item1.setName("Item1");
item1.setDescription("Washington");
Session s1 = sessionBuilder.openSession();
Transaction tx1 = s1.beginTransaction();
s1.persist(item1);
tx1.commit();
s1.close();
Item item2 = new Item();
item2.setName("Item2");
item2.setDescription("Chicago");
Session s2 = sessionBuilder.openSession();
Transaction tx2 = s2.beginTransaction();
s2.persist(item2);
tx2.commit();
s2.close();
interceptor.blockOnLoad();
Future<Item> fetchedItem = executor.submit(new Callable<Item>() {
public Item call() throws Exception {
return findByDescription(sessionBuilder, "Washington");
}
});
// wait for the onLoad listener to be called
interceptor.waitOnLoad();
Session s3 = sessionBuilder.openSession();
Transaction tx3 = s3.beginTransaction();
item1.setDescription("New York");
item2.setDescription("Washington");
s3.update(item1);
s3.update(item2);
tx3.commit();
s3.close();
interceptor.unblockOnLoad();
// the concurrent query was executed beforeQuery the data was amended so
// let's expect "Item1" to be returned as living in Washington
Item fetched = fetchedItem.get();
assertEquals("Item1", fetched.getName());
// Query again: now "Item2" is expected to live in Washington
fetched = findByDescription(sessionBuilder, "Washington");
assertEquals("Item2", fetched.getName());
}
use of org.hibernate.SessionBuilder in project hibernate-orm by hibernate.
the class AbstractCurrentSessionContext method baseSessionBuilder.
protected SessionBuilder baseSessionBuilder() {
final SessionBuilder builder = factory.withOptions();
final CurrentTenantIdentifierResolver resolver = factory.getCurrentTenantIdentifierResolver();
if (resolver != null) {
builder.tenantIdentifier(resolver.resolveCurrentTenantIdentifier());
}
return builder;
}
Aggregations