protected Object buildObjectInUnitOfWork(ObjectBuildingQuery query, JoinedAttributeManager joinManager, AbstractRecord databaseRow, UnitOfWorkImpl unitOfWork, Object primaryKey, CacheKey preFetchedCacheKey, ClassDescriptor concreteDescriptor) throws DatabaseException, QueryException {
// When in transaction we are reading via the write connection
// and so do not want to corrupt the shared cache with dirty objects.
// Hence we build and refresh clones directly from the database row.
// PERF: Allow the session cached to still be used after early transaction if isolation setting has been set.
CachePolicy cachePolicy = concreteDescriptor.getCachePolicy();
if (!cachePolicy.shouldUseSessionCacheInUnitOfWorkEarlyTransaction()) {
if (((unitOfWork.hasCommitManager() && unitOfWork.getCommitManager().isActive())
|| unitOfWork.wasTransactionBegunPrematurely()
|| cachePolicy.shouldIsolateObjectsInUnitOfWork()
|| cachePolicy.shouldIsolateProtectedObjectsInUnitOfWork()
|| query.shouldStoreBypassCache())
&& (!unitOfWork.isClassReadOnly(concreteDescriptor.getJavaClass(), concreteDescriptor))) {
// It is easier to switch once to the correct builder here.
return concreteDescriptor.getObjectBuilder().buildWorkingCopyCloneFromRow(query, joinManager, databaseRow, unitOfWork, primaryKey, preFetchedCacheKey);
}