use of org.eclipse.persistence.internal.sessions.UnitOfWorkImpl in project eclipselink by eclipse-ee4j.
the class WasTransactionBegunPrematurelyRollbackTest method test.
@Override
public void test() {
Person person = new Person();
UnitOfWork uow = getSession().acquireUnitOfWork();
Person clonePerson = (Person) uow.registerObject(person);
clonePerson.id = id;
getAbstractSession().beginTransaction();
((UnitOfWorkImpl) uow).setWasTransactionBegunPrematurely(true);
try {
uow.commit();
} catch (Exception ex) {
exception = ex;
}
}
use of org.eclipse.persistence.internal.sessions.UnitOfWorkImpl in project eclipselink by eclipse-ee4j.
the class ForceWeakReferenceTest method test.
@Override
public void test() {
UnitOfWork uow = getSession().acquireUnitOfWork(ReferenceMode.FORCE_WEAK);
int size = uow.readAllObjects(Employee.class).size();
try {
Long[] arr = new Long[10000000];
for (int i = 0; i < 10000000; ++i) {
arr[i] = (long) i;
}
System.gc();
try {
Thread.sleep(200);
} catch (InterruptedException ex) {
}
System.gc();
} catch (Error er) {
// ignore
}
if (((UnitOfWorkImpl) uow).getCloneMapping().size() == size) {
throw new TestErrorException("Did not release forced weak references.");
}
}
use of org.eclipse.persistence.internal.sessions.UnitOfWorkImpl in project eclipselink by eclipse-ee4j.
the class UnregisterUnitOfWorkTest method test.
@Override
protected void test() {
/**
****************
*/
/* DEEP UNREGISTER */
/**
****************
*/
Employee employee = (org.eclipse.persistence.testing.models.employee.domain.Employee) (new org.eclipse.persistence.testing.models.employee.domain.EmployeePopulator()).employeeExample1();
UnitOfWorkImpl uow = (UnitOfWorkImpl) getSession().acquireUnitOfWork();
Employee workingCopy = (Employee) uow.registerObject(employee);
workingCopy.setFirstName("firstName");
uow.deepUnregisterObject(workingCopy);
if (!uow.getCloneMapping().isEmpty()) {
throw new TestErrorException("Deep unregister object did not work");
}
if (!uow.getNewObjectsOriginalToClone().isEmpty()) {
throw new TestErrorException("Deep unregister object did not work");
}
if (!uow.getNewObjectsCloneToOriginal().isEmpty()) {
throw new TestErrorException("Deep unregister object did not work");
}
uow.commit();
uow = (UnitOfWorkImpl) getSession().acquireUnitOfWork();
Vector workingCopies = uow.readAllObjects(Employee.class);
for (Enumeration enumtr = workingCopies.elements(); enumtr.hasMoreElements(); ) {
uow.deepUnregisterObject(enumtr.nextElement());
}
if (!uow.getCloneMapping().isEmpty()) {
throw new TestErrorException("Deep unregister object did not work");
}
if (!uow.getNewObjectsOriginalToClone().isEmpty()) {
throw new TestErrorException("Deep unregister object did not work");
}
if (!uow.getNewObjectsCloneToOriginal().isEmpty()) {
throw new TestErrorException("Deep unregister object did not work");
}
uow.commit();
/**
****************
*/
/* UNREGISTER */
/**
****************
*/
uow = (UnitOfWorkImpl) getSession().acquireUnitOfWork();
workingCopy = (Employee) uow.registerObject(employee);
workingCopy.getAddress();
workingCopy.getProjects();
workingCopy.setFirstName("firstName");
uow.unregisterObject(workingCopy);
Employee newWorkingCopy = (Employee) uow.registerObject(employee);
if (workingCopy == newWorkingCopy) {
throw new TestErrorException("Cascade private parts unregister object did not work");
}
if (workingCopy.getAddress() == newWorkingCopy.getAddress()) {
throw new TestErrorException("Cascade private parts unregister object did not work");
}
for (Enumeration enumtr = workingCopy.getProjects().elements(); enumtr.hasMoreElements(); ) {
if (!newWorkingCopy.getProjects().contains(enumtr.nextElement())) {
throw new TestErrorException("Cascade private parts unregister object did not work");
}
}
uow.commit();
/**
*******************
*/
/* SHALLOW UNREGISTER */
/**
*******************
*/
uow = (UnitOfWorkImpl) getSession().acquireUnitOfWork();
workingCopy = (Employee) uow.registerObject(employee);
workingCopy.getAddress();
workingCopy.setFirstName("firstName");
uow.shallowUnregisterObject(workingCopy);
newWorkingCopy = (Employee) uow.registerObject(employee);
if (workingCopy == newWorkingCopy) {
throw new TestErrorException("Shallow unregister object did not work");
}
if (workingCopy.getAddress() != newWorkingCopy.getAddress()) {
throw new TestErrorException("Shallow unregister object did not work");
}
uow.commit();
}
use of org.eclipse.persistence.internal.sessions.UnitOfWorkImpl in project eclipselink by eclipse-ee4j.
the class HardReferenceTest method test.
@Override
public void test() {
UnitOfWork uow = getSession().acquireUnitOfWork();
int size = uow.readAllObjects(ALCTEmployee.class).size();
try {
Long[] arr = new Long[100000];
for (int i = 0; i < 100000; ++i) {
arr[i] = (long) i;
}
} catch (Error er) {
// ignore
}
if (((UnitOfWorkImpl) uow).getCloneMapping().size() != size) {
throw new TestErrorException("Did not hold onto all references.");
}
}
use of org.eclipse.persistence.internal.sessions.UnitOfWorkImpl in project eclipselink by eclipse-ee4j.
the class ObjectBuilder method buildWorkingCopyCloneNormally.
/**
* buildWorkingCopyCloneFromRow is an alternative to this which is the
* normal behavior.
* A row is read from the database, an original is built/refreshed/returned
* from the shared cache, and the original is registered/conformed/reverted
* in the UnitOfWork.
* <p>
* This default behavior is only safe when the query is executed on a read
* connection, otherwise uncommitted data might get loaded into the shared
* cache.
* <p>
* Represents the way TopLink has always worked.
*/
protected Object buildWorkingCopyCloneNormally(ObjectBuildingQuery query, AbstractRecord databaseRow, UnitOfWorkImpl unitOfWork, Object primaryKey, CacheKey preFetchedCacheKey, ClassDescriptor concreteDescriptor, JoinedAttributeManager joinManager) throws DatabaseException, QueryException {
// First check local unit of work cache.
CacheKey unitOfWorkCacheKey = unitOfWork.getIdentityMapAccessorInstance().acquireLock(primaryKey, concreteDescriptor.getJavaClass(), concreteDescriptor, query.isCacheCheckComplete());
Object clone = unitOfWorkCacheKey.getObject();
boolean found = clone != null;
Object original = null;
try {
// Only check parent cache if not in unit of work, or if a refresh is required.
if (!found || query.shouldRefreshIdentityMapResult() || // Need to build original to cache it.
query.shouldCacheQueryResults() || query.shouldRetrieveBypassCache() || (concreteDescriptor.hasFetchGroupManager() && concreteDescriptor.getFetchGroupManager().isPartialObject(clone))) {
// This is normal case when we are not in transaction.
// Pass the query off to the parent. Let it build the object and
// cache it normally, then register/refresh it.
AbstractSession session = unitOfWork.getParentIdentityMapSession(query);
// forwarding queries to different sessions is now as simple as setting
// the session on the query.
query.setSession(session);
if (session.isUnitOfWork()) {
// If a nested unit of work, recurse.
original = buildObjectInUnitOfWork(query, joinManager, databaseRow, (UnitOfWorkImpl) session, primaryKey, preFetchedCacheKey, concreteDescriptor);
// GFBug#404 Pass in joinManager or not based on if shouldCascadeCloneToJoinedRelationship is set to true
if (unitOfWork.shouldCascadeCloneToJoinedRelationship()) {
return query.registerIndividualResult(original, primaryKey, unitOfWork, joinManager, concreteDescriptor);
} else {
return query.registerIndividualResult(original, primaryKey, unitOfWork, null, concreteDescriptor);
}
} else {
// PERF: This optimizes the normal case to avoid duplicate cache access.
CacheKey parentCacheKey = (CacheKey) buildObject(true, query, databaseRow, session, primaryKey, preFetchedCacheKey, concreteDescriptor, joinManager);
original = parentCacheKey.getObject();
if (query.shouldCacheQueryResults()) {
query.cacheResult(original);
}
// PERF: Do not register nor process read-only.
if (unitOfWork.isClassReadOnly(original.getClass(), concreteDescriptor)) {
// There is an obscure case where they object could be read-only and pessimistic.
// Record clone if referenced class has pessimistic locking policy.
query.recordCloneForPessimisticLocking(original, unitOfWork);
return original;
}
if (!query.isRegisteringResults()) {
return original;
}
if (clone == null) {
clone = unitOfWork.cloneAndRegisterObject(original, parentCacheKey, unitOfWorkCacheKey, concreteDescriptor);
// TODO-dclarke: At this point the clones do not have their fetch-group specified
// relationship attributes loaded
}
// fetch group manager control fetch group support
if (concreteDescriptor.hasFetchGroupManager()) {
// if the object is already registered in uow, but it's partially fetched (fetch group case)
if (concreteDescriptor.getFetchGroupManager().shouldWriteInto(original, clone)) {
// there might be cases when reverting/refreshing clone is needed.
concreteDescriptor.getFetchGroupManager().writePartialIntoClones(original, clone, unitOfWork.getBackupClone(clone, concreteDescriptor), unitOfWork);
}
}
}
}
query.postRegisterIndividualResult(clone, original, primaryKey, unitOfWork, joinManager, concreteDescriptor);
} finally {
unitOfWorkCacheKey.release();
query.setSession(unitOfWork);
}
return clone;
}
Aggregations