org.eclipse.persistence.sessions.UnitOfWork Java Examples

The following examples show how to use org.eclipse.persistence.sessions.UnitOfWork. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EclipseLinkJpaDialect.java    From spring-analysis-note with MIT License 9 votes vote down vote up
@Override
@Nullable
public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition)
		throws PersistenceException, SQLException, TransactionException {

	if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) {
		// Pass custom isolation level on to EclipseLink's DatabaseLogin configuration
		// (since Spring 4.1.2)
		UnitOfWork uow = entityManager.unwrap(UnitOfWork.class);
		uow.getLogin().setTransactionIsolation(definition.getIsolationLevel());
	}

	entityManager.getTransaction().begin();

	if (!definition.isReadOnly() && !this.lazyDatabaseTransaction) {
		// Begin an early transaction to force EclipseLink to get a JDBC Connection
		// so that Spring can manage transactions with JDBC as well as EclipseLink.
		entityManager.unwrap(UnitOfWork.class).beginEarlyTransaction();
	}

	return null;
}
 
Example #2
Source File: EclipseLinkJpaDialect.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
@Nullable
public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition)
		throws PersistenceException, SQLException, TransactionException {

	if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) {
		// Pass custom isolation level on to EclipseLink's DatabaseLogin configuration
		// (since Spring 4.1.2)
		UnitOfWork uow = entityManager.unwrap(UnitOfWork.class);
		uow.getLogin().setTransactionIsolation(definition.getIsolationLevel());
	}

	entityManager.getTransaction().begin();

	if (!definition.isReadOnly() && !this.lazyDatabaseTransaction) {
		// Begin an early transaction to force EclipseLink to get a JDBC Connection
		// so that Spring can manage transactions with JDBC as well as EclipseLink.
		entityManager.unwrap(UnitOfWork.class).beginEarlyTransaction();
	}

	return null;
}
 
Example #3
Source File: EclipseLinkJpaDialect.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition)
		throws PersistenceException, SQLException, TransactionException {

	if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) {
		// Pass custom isolation level on to EclipseLink's DatabaseLogin configuration
		// (since Spring 4.1.2)
		UnitOfWork uow = entityManager.unwrap(UnitOfWork.class);
		uow.getLogin().setTransactionIsolation(definition.getIsolationLevel());
	}

	entityManager.getTransaction().begin();

	if (!definition.isReadOnly() && !this.lazyDatabaseTransaction) {
		// Begin an early transaction to force EclipseLink to get a JDBC Connection
		// so that Spring can manage transactions with JDBC as well as EclipseLink.
		entityManager.unwrap(UnitOfWork.class).beginEarlyTransaction();
	}

	return null;
}
 
Example #4
Source File: EclipseLinkJpaDialect.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition)
		throws PersistenceException, SQLException, TransactionException {

	if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) {
		// Pass custom isolation level on to EclipseLink's DatabaseLogin configuration
		// (since Spring 4.1.2)
		UnitOfWork uow = entityManager.unwrap(UnitOfWork.class);
		uow.getLogin().setTransactionIsolation(definition.getIsolationLevel());
	}

	entityManager.getTransaction().begin();

	if (!definition.isReadOnly() && !this.lazyDatabaseTransaction) {
		// Begin an early transaction to force EclipseLink to get a JDBC Connection
		// so that Spring can manage transactions with JDBC as well as EclipseLink.
		entityManager.unwrap(UnitOfWork.class).beginEarlyTransaction();
	}

	return null;
}
 
Example #5
Source File: EntityManagerImpl.java    From cuba with Apache License 2.0 6 votes vote down vote up
@Override
public <T extends Entity> T merge(T entity) {
    log.debug("merge {}", entity);

    if (entityStates.isManaged(entity))
        return entity;

    String storeName = support.getStorageName(delegate.unwrap(UnitOfWork.class));
    entityListenerMgr.fireListener(entity, EntityListenerType.BEFORE_ATTACH, storeName);

    if ((entityStates.isNew(entity) || !entityStates.isDetached(entity)) && entity.getId() != null) {
        // if a new instance is passed to merge(), we suppose it is persistent but "not detached"
        Entity destEntity = findOrCreate(entity.getClass(), entity.getId());
        deepCopyIgnoringNulls(entity, destEntity, Sets.newIdentityHashSet());
        if (entityStates.isNew(destEntity)) {
            entityPersistingEventMgr.publishEvent(entity);
        }
        //noinspection unchecked
        return (T) destEntity;
    }

    T merged = internalMerge(entity);
    support.registerInstance(merged, this);
    return merged;
}
 
Example #6
Source File: PersistenceImplSupport.java    From cuba with Apache License 2.0 6 votes vote down vote up
protected void registerInstanceForUnitOfWork(Entity instance, UnitOfWork unitOfWork) {
    if (log.isTraceEnabled())
        log.trace("ContainerResourceHolder.registerInstanceForUnitOfWork: instance = " +
                instance + ", UnitOfWork = " + unitOfWork);

    if (instance instanceof BaseGenericIdEntity) {
        BaseEntityInternalAccess.setManaged((BaseGenericIdEntity) instance, true);
    }

    Set<Entity> instances = unitOfWorkMap.get(unitOfWork);
    if (instances == null) {
        instances = createEntitySet();
        unitOfWorkMap.put(unitOfWork, instances);
    }
    instances.add(instance);
}
 
Example #7
Source File: PersistenceImplSupport.java    From cuba with Apache License 2.0 6 votes vote down vote up
public void detach(EntityManager entityManager, Entity entity) {
    UnitOfWork unitOfWork = entityManager.getDelegate().unwrap(UnitOfWork.class);
    String storeName = getStorageName(unitOfWork);

    if (entity instanceof BaseGenericIdEntity) {
        fireBeforeDetachEntityListener((BaseGenericIdEntity) entity, storeName);

        ContainerResourceHolder container = getInstanceContainerResourceHolder(storeName);
        container.unregisterInstance(entity, unitOfWork);
        if (BaseEntityInternalAccess.isNew((BaseGenericIdEntity) entity)) {
            container.getNewDetachedInstances().add(entity);
        }
    }

    makeDetached(entity);
}
 
Example #8
Source File: DatabaseNetwork.java    From BotLibre with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Return all active vertices.
 */
@SuppressWarnings("unchecked")
public synchronized List<Vertex> allActive() {
	UnitOfWork unitOfWork = this.entityManager.unwrap(JpaEntityManager.class).getUnitOfWork();
	try {
		return unitOfWork.getIdentityMapAccessor().getAllFromIdentityMap(null, BasicVertex.class, null, null);
	} catch (Exception exception) {
		return new ArrayList<Vertex>();
	}
}
 
Example #9
Source File: PersistenceImplSupport.java    From cuba with Apache License 2.0 5 votes vote down vote up
protected Collection<Entity> getInstances(UnitOfWork unitOfWork) {
    HashSet<Entity> set = new HashSet<>();
    Set<Entity> entities = unitOfWorkMap.get(unitOfWork);
    if (entities != null)
        set.addAll(entities);
    return set;
}
 
Example #10
Source File: PersistenceImplSupport.java    From cuba with Apache License 2.0 5 votes vote down vote up
public Collection<Entity> getInstances(EntityManager entityManager) {
    if (!TransactionSynchronizationManager.isActualTransactionActive())
        throw new RuntimeException("No transaction");

    UnitOfWork unitOfWork = entityManager.getDelegate().unwrap(UnitOfWork.class);
    return getInstanceContainerResourceHolder(getStorageName(unitOfWork)).getInstances(unitOfWork);
}
 
Example #11
Source File: PersistenceImplSupport.java    From cuba with Apache License 2.0 5 votes vote down vote up
public void registerInstance(Entity entity, AbstractSession session) {
    // Can be called outside of a transaction when fetching lazy attributes
    if (!TransactionSynchronizationManager.isActualTransactionActive())
        return;

    if (!(session instanceof UnitOfWork))
        throw new RuntimeException("Session is not a UnitOfWork: " + session);

    getInstanceContainerResourceHolder(getStorageName(session)).registerInstanceForUnitOfWork(entity, (UnitOfWork) session);
}
 
Example #12
Source File: PersistenceImplSupport.java    From cuba with Apache License 2.0 5 votes vote down vote up
public void registerInstance(Entity entity, EntityManager entityManager) {
    if (!TransactionSynchronizationManager.isActualTransactionActive())
        throw new RuntimeException("No transaction");

    UnitOfWork unitOfWork = entityManager.getDelegate().unwrap(UnitOfWork.class);
    getInstanceContainerResourceHolder(getStorageName(unitOfWork)).registerInstanceForUnitOfWork(entity, unitOfWork);

    if (entity instanceof BaseGenericIdEntity) {
        BaseEntityInternalAccess.setDetached((BaseGenericIdEntity) entity, false);
    }
}
 
Example #13
Source File: PersistenceImplSupport.java    From cuba with Apache License 2.0 4 votes vote down vote up
public void processFlush(EntityManager entityManager, boolean warnAboutImplicitFlush) {
    UnitOfWork unitOfWork = entityManager.getDelegate().unwrap(UnitOfWork.class);
    String storeName = getStorageName(unitOfWork);
    traverseEntities(getInstanceContainerResourceHolder(storeName), new OnSaveEntityVisitor(storeName), warnAboutImplicitFlush);
}
 
Example #14
Source File: PersistenceImplSupport.java    From cuba with Apache License 2.0 4 votes vote down vote up
protected void unregisterInstance(Entity instance, UnitOfWork unitOfWork) {
    Set<Entity> instances = unitOfWorkMap.get(unitOfWork);
    if (instances != null) {
        instances.remove(instance);
    }
}
 
Example #15
Source File: DatabaseNetwork.java    From BotLibre with Eclipse Public License 1.0 4 votes vote down vote up
public DatabaseNetwork(EntityManager entityManager, boolean isShortTerm) {
	super(isShortTerm);
	this.entityManager = entityManager;
	entityManager.unwrap(UnitOfWork.class).setProperty("network", this);
}
 
Example #16
Source File: DatabaseNetwork.java    From BotLibre with Eclipse Public License 1.0 4 votes vote down vote up
public synchronized void clear() {
	this.entityManager.clear();
	this.verticesByData.clear();
	resetSize();
	this.entityManager.unwrap(UnitOfWork.class).setProperty("network", this);
}
 
Example #17
Source File: CubaEclipseLinkJpaDialect.java    From cuba with Apache License 2.0 4 votes vote down vote up
@Override
public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition) throws PersistenceException, SQLException, TransactionException {
    Object result = super.beginTransaction(entityManager, definition);
    Preconditions.checkState(result == null, "Transactional data should be null for EclipseLink dialect");

    // Read default timeout every time - may be somebody wants to change it on the fly
    int defaultTimeout = 0;
    String defaultTimeoutProp = AppContext.getProperty("cuba.defaultQueryTimeoutSec");
    if (!"0".equals(defaultTimeoutProp) && !StringUtils.isBlank(defaultTimeoutProp)) {
        try {
            defaultTimeout = Integer.parseInt(defaultTimeoutProp);
        } catch (NumberFormatException e) {
            log.error("Invalid cuba.defaultQueryTimeoutSec value", e);
        }
    }

    int timeoutSec = 0;
    if (definition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT)
        timeoutSec = definition.getTimeout();
    else if (defaultTimeout != 0)
        timeoutSec = defaultTimeout;

    if (timeoutSec != 0) {
        log.trace("Applying query timeout {} sec", timeoutSec);
        if (entityManager instanceof JpaEntityManager) {
            UnitOfWork unitOfWork = ((JpaEntityManager) entityManager).getUnitOfWork();
            if (unitOfWork != null) {
                //setup delay in seconds on unit of work
                unitOfWork.setQueryTimeoutDefault(timeoutSec);
            }
        }

        String s = DbmsSpecificFactory.getDbmsFeatures().getTransactionTimeoutStatement();
        if (s != null) {
            Connection connection = entityManager.unwrap(Connection.class);
            try (Statement statement = connection.createStatement()) {
                statement.execute(String.format(s, timeoutSec * 1000));
            }
        }
    }

    return new CubaEclipseLinkTransactionData(entityManager);
}