use of org.hibernate.engine.spi.SharedSessionContractImplementor in project hibernate-orm by hibernate.
the class CustomLoader method applyLocks.
@Override
protected String applyLocks(String sql, QueryParameters parameters, Dialect dialect, List<AfterLoadAction> afterLoadActions) throws QueryException {
final LockOptions lockOptions = parameters.getLockOptions();
if (lockOptions == null || (lockOptions.getLockMode() == LockMode.NONE && lockOptions.getAliasLockCount() == 0)) {
return sql;
}
// user is request locking, lets see if we can apply locking directly to the SQL...
// some dialects wont allow locking with paging...
afterLoadActions.add(new AfterLoadAction() {
private final LockOptions originalLockOptions = lockOptions.makeCopy();
@Override
public void afterLoad(SharedSessionContractImplementor session, Object entity, Loadable persister) {
((Session) session).buildLockRequest(originalLockOptions).lock(persister.getEntityName(), entity);
}
});
parameters.getLockOptions().setLockMode(LockMode.READ);
return sql;
}
use of org.hibernate.engine.spi.SharedSessionContractImplementor in project hibernate-orm by hibernate.
the class CollectionLoadContext method endLoadingCollections.
/**
* Finish the process of collection-loading for this bound result set. Mainly this
* involves cleaning up resources and notifying the collections that loading is
* complete.
*
* @param persister The persister for which to complete loading.
*/
public void endLoadingCollections(CollectionPersister persister) {
final SharedSessionContractImplementor session = getLoadContext().getPersistenceContext().getSession();
if (!loadContexts.hasLoadingCollectionEntries() && localLoadingCollectionKeys.isEmpty()) {
return;
}
// in an effort to avoid concurrent-modification-exceptions (from
// potential recursive calls back through here as a result of the
// eventual call to PersistentCollection#endRead), we scan the
// internal loadingCollections map for matches and store those matches
// in a temp collection. the temp collection is then used to "drive"
// the #endRead processing.
List<LoadingCollectionEntry> matches = null;
final Iterator itr = localLoadingCollectionKeys.iterator();
while (itr.hasNext()) {
final CollectionKey collectionKey = (CollectionKey) itr.next();
final LoadingCollectionEntry lce = loadContexts.locateLoadingCollectionEntry(collectionKey);
if (lce == null) {
LOG.loadingCollectionKeyNotFound(collectionKey);
} else if (lce.getResultSet() == resultSet && lce.getPersister() == persister) {
if (matches == null) {
matches = new ArrayList<>();
}
matches.add(lce);
if (lce.getCollection().getOwner() == null) {
session.getPersistenceContext().addUnownedCollection(new CollectionKey(persister, lce.getKey(), persister.getOwnerEntityPersister().getEntityMetamodel().getEntityMode()), lce.getCollection());
}
LOG.tracev("Removing collection load entry [{0}]", lce);
// todo : i'd much rather have this done from #endLoadingCollection(CollectionPersister,LoadingCollectionEntry)...
loadContexts.unregisterLoadingCollectionXRef(collectionKey);
itr.remove();
}
}
endLoadingCollections(persister, matches);
if (localLoadingCollectionKeys.isEmpty()) {
// todo : hack!!!
// NOTE : here we cleanup the load context when we have no more local
// LCE entries. This "works" for the time being because really
// only the collection load contexts are implemented. Long term,
// this cleanup should become part of the "close result set"
// processing from the (sandbox/jdbc) jdbc-container code.
loadContexts.cleanup(resultSet);
}
}
use of org.hibernate.engine.spi.SharedSessionContractImplementor in project hibernate-orm by hibernate.
the class CollectionLoadContext method endLoadingCollection.
private void endLoadingCollection(LoadingCollectionEntry lce, CollectionPersister persister) {
LOG.tracev("Ending loading collection [{0}]", lce);
final SharedSessionContractImplementor session = getLoadContext().getPersistenceContext().getSession();
// warning: can cause a recursive calls! (proxy initialization)
final boolean hasNoQueuedAdds = lce.getCollection().endRead();
if (persister.getCollectionType().hasHolder()) {
getLoadContext().getPersistenceContext().addCollectionHolder(lce.getCollection());
}
CollectionEntry ce = getLoadContext().getPersistenceContext().getCollectionEntry(lce.getCollection());
if (ce == null) {
ce = getLoadContext().getPersistenceContext().addInitializedCollection(persister, lce.getCollection(), lce.getKey());
} else {
ce.postInitialize(lce.getCollection());
// if (ce.getLoadedPersister().getBatchSize() > 1) { // not the best place for doing this, moved into ce.postInitialize
// getLoadContext().getPersistenceContext().getBatchFetchQueue().removeBatchLoadableCollection(ce);
// }
}
// add to cache if:
boolean addToCache = // there were no queued additions
hasNoQueuedAdds && // and the role has a cache
persister.hasCache() && // and this is not a forced initialization during flush
session.getCacheMode().isPutEnabled() && !ce.isDoremove();
if (addToCache) {
addCollectionToCache(lce, persister);
}
if (LOG.isDebugEnabled()) {
LOG.debugf("Collection fully initialized: %s", MessageHelper.collectionInfoString(persister, lce.getCollection(), lce.getKey(), session));
}
if (session.getFactory().getStatistics().isStatisticsEnabled()) {
session.getFactory().getStatistics().loadCollection(persister.getRole());
}
}
use of org.hibernate.engine.spi.SharedSessionContractImplementor in project hibernate-orm by hibernate.
the class SynchronizationTypeTest method testImplicitJoining.
@Test
public void testImplicitJoining() throws Exception {
// here the transaction is started before the EM is opened. Because the SynchronizationType is UNSYNCHRONIZED
// though, it should not auto join the transaction
assertFalse("setup problem", JtaStatusHelper.isActive(TestingJtaPlatformImpl.INSTANCE.getTransactionManager()));
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
assertTrue("setup problem", JtaStatusHelper.isActive(TestingJtaPlatformImpl.INSTANCE.getTransactionManager()));
EntityManager entityManager = entityManagerFactory().createEntityManager(SynchronizationType.UNSYNCHRONIZED, null);
SharedSessionContractImplementor session = entityManager.unwrap(SharedSessionContractImplementor.class);
ExtraAssertions.assertTyping(JtaTransactionCoordinatorImpl.class, session.getTransactionCoordinator());
JtaTransactionCoordinatorImpl transactionCoordinator = (JtaTransactionCoordinatorImpl) session.getTransactionCoordinator();
assertFalse("EM was auto joined on creation", transactionCoordinator.isSynchronizationRegistered());
assertTrue("EM was auto joined on creation", transactionCoordinator.isActive());
assertFalse("EM was auto joined on creation", transactionCoordinator.isJoined());
session.getFlushMode();
assertFalse(transactionCoordinator.isSynchronizationRegistered());
assertTrue(transactionCoordinator.isActive());
assertFalse(transactionCoordinator.isJoined());
entityManager.joinTransaction();
assertTrue(JtaStatusHelper.isActive(TestingJtaPlatformImpl.INSTANCE.getTransactionManager()));
assertTrue(transactionCoordinator.isActive());
assertTrue(transactionCoordinator.isSynchronizationRegistered());
assertTrue(transactionCoordinator.isActive());
assertTrue(transactionCoordinator.isJoined());
assertTrue(entityManager.isOpen());
assertTrue(session.isOpen());
entityManager.close();
assertFalse(entityManager.isOpen());
assertFalse(session.isOpen());
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertFalse(entityManager.isOpen());
assertFalse(session.isOpen());
}
use of org.hibernate.engine.spi.SharedSessionContractImplementor in project hibernate-orm by hibernate.
the class CollectionUpdateAction method execute.
@Override
public void execute() throws HibernateException {
final Serializable id = getKey();
final SharedSessionContractImplementor session = getSession();
final CollectionPersister persister = getPersister();
final PersistentCollection collection = getCollection();
final boolean affectedByFilters = persister.isAffectedByEnabledFilters(session);
preUpdate();
if (!collection.wasInitialized()) {
if (!collection.hasQueuedOperations()) {
throw new AssertionFailure("no queued adds");
}
// do nothing - we only need to notify the cache...
} else if (!affectedByFilters && collection.empty()) {
if (!emptySnapshot) {
persister.remove(id, session);
}
} else if (collection.needsRecreate(persister)) {
if (affectedByFilters) {
throw new HibernateException("cannot recreate collection while filter is enabled: " + MessageHelper.collectionInfoString(persister, collection, id, session));
}
if (!emptySnapshot) {
persister.remove(id, session);
}
persister.recreate(collection, id, session);
} else {
persister.deleteRows(collection, id, session);
persister.updateRows(collection, id, session);
persister.insertRows(collection, id, session);
}
getSession().getPersistenceContext().getCollectionEntry(collection).afterAction(collection);
evict();
postUpdate();
if (getSession().getFactory().getStatistics().isStatisticsEnabled()) {
getSession().getFactory().getStatistics().updateCollection(getPersister().getRole());
}
}
Aggregations