Java Code Examples for javax.persistence.EntityManager#setFlushMode()

The following examples show how to use javax.persistence.EntityManager#setFlushMode() . 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: MCRCategoryDAOImpl.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
private static <T> T withoutFlush(EntityManager entityManager, boolean flushAtEnd,
    Function<EntityManager, T> task) {
    FlushModeType fm = entityManager.getFlushMode();
    entityManager.setFlushMode(FlushModeType.COMMIT);
    try {
        T result = task.apply(entityManager);
        if (flushAtEnd) {
            entityManager.flush();
        }
        return result;
    } catch (RuntimeException e) {
        throw e;
    } finally {
        entityManager.setFlushMode(fm);
    }
}
 
Example 2
Source File: EntityManagerContainerBasic.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
public <T extends JpaObject> EntityManager get(Class<T> cls) throws Exception {
	Class<T> clazz = (Class<T>) entityManagerContainerFactory.assignableFrom(cls);
	EntityManager em = fromEntityManagers(clazz);
	if (null == em) {
		em = entityManagerContainerFactory.createEntityManager(clazz);
		em.setFlushMode(FlushModeType.COMMIT);
		entityManagerMap.put(cls, em);
	}
	return em;
}
 
Example 3
Source File: RestoreStorage.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
private boolean execute() throws Exception {
	final List<String> storageContainerEntityNames = new ArrayList<>();
	storageContainerEntityNames.addAll((List<String>) Config.resource(Config.RESOURCE_STORAGECONTAINERENTITYNAMES));
	List<String> classNames = new ArrayList<>();
	classNames.addAll(this.catalog.keySet());
	classNames = ListTools.includesExcludesWildcard(classNames, Config.dumpRestoreStorage().getIncludes(),
			Config.dumpRestoreStorage().getExcludes());
	logger.print("restore storage find {} to restore.", classNames.size());
	final File persistence = new File(Config.dir_local_temp_classes(), DateTools.compact(this.start) + "_dump.xml");
	PersistenceXmlHelper.write(persistence.getAbsolutePath(), classNames);
	final StorageMappings storageMappings = Config.storageMappings();
	int count = 0;
	for (int i = 0; i < classNames.size(); i++) {
		final Class<StorageObject> cls = (Class<StorageObject>) Class.forName(classNames.get(i));
		final EntityManagerFactory emf = OpenJPAPersistence.createEntityManagerFactory(cls.getName(),
				persistence.getName(), PersistenceXmlHelper.properties(cls.getName(), Config.slice().getEnable()));
		final EntityManager em = emf.createEntityManager();
		em.setFlushMode(FlushModeType.COMMIT);
		try {
			final DumpRestoreStorageCatalogItem item = this.catalog.get(cls.getName());
			logger.print(
					"restore storage({}/{}): {}, count: {}, normal: {} will be restore, invalidStorage: {} and empty: {} will be ignore, size: {}M.",
					(i + 1), classNames.size(), cls.getName(), item.getCount(), item.getNormal(),
					item.getInvalidStorage(), item.getEmpty(), (item.getSize() / 1024 / 1024));
			count += this.store(cls, em, storageMappings);
		} finally {
			em.close();
			emf.close();
		}
		logger.print("restore storage completed, total count: {}, elapsed: {} minutes.", count,
				(System.currentTimeMillis() - start.getTime()) / 1000 / 60);
	}
	return false;
}
 
Example 4
Source File: RestoreData.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public boolean execute() throws Exception {
	List<String> containerEntityNames = new ArrayList<>();
	containerEntityNames.addAll((List<String>) Config.resource(Config.RESOURCE_CONTAINERENTITYNAMES));
	List<String> classNames = new ArrayList<>();
	classNames.addAll(this.catalog.keySet());
	classNames = ListTools.includesExcludesWildcard(classNames, Config.dumpRestoreData().getIncludes(),
			Config.dumpRestoreData().getExcludes());
	classNames = ListTools.includesExcludesWildcard(containerEntityNames, classNames, null);

	logger.print("find: {} data to restore, path: {}.", classNames.size(), this.dir.getAbsolutePath());
	File persistence = new File(Config.dir_local_temp_classes(), DateTools.compact(this.start) + "_dump.xml");
	PersistenceXmlHelper.write(persistence.getAbsolutePath(), classNames);
	long count = 0;
	for (int i = 0; i < classNames.size(); i++) {
		Class<JpaObject> cls = (Class<JpaObject>) Class.forName(classNames.get(i));
		EntityManagerFactory emf = OpenJPAPersistence.createEntityManagerFactory(cls.getName(),
				persistence.getName(), PersistenceXmlHelper.properties(cls.getName(), Config.slice().getEnable()));
		if (emf != null) {
			EntityManager em = emf.createEntityManager();
			em.setFlushMode(FlushModeType.COMMIT);
			try {
				logger.print("restore data({}/{}): {}, count: {}.", (i + 1), classNames.size(), cls.getName(),
						catalog.get(cls.getName()));
				count = count + this.store(cls, em);
			} finally {
				em.close();
				emf.close();
			}
		} else {
			logger.warn("can not create 'EntityManagerFactory' for Entity:[" + cls.getName() + "]");
		}
	}
	logger.print("restore data completed, total count: {}, elapsed: {} minutes.", count,
			(System.currentTimeMillis() - start.getTime()) / 1000 / 60);
	return true;
}
 
Example 5
Source File: EntityManagerHelper.java    From testgrid with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a thread safe {@link EntityManager}.
 *
 * @return thread safe {@link EntityManager}
 */
private static EntityManager getEntityManager(String persistenceUnitName) {
    EntityManager entityManager = entityManagerMap.get(persistenceUnitName);
    if (entityManager == null || !entityManager.isOpen()) {
        EntityManagerFactory entityManagerFactory = getEntityManagerFactory(persistenceUnitName);
        entityManager = entityManagerFactory.createEntityManager();
        entityManager.setFlushMode(FlushModeType.COMMIT); // Flushing will happen on committing the transaction.
        entityManagerMap.put(persistenceUnitName, entityManager);
    }
    return entityManager;
}
 
Example 6
Source File: JpaDb.java    From crushpaper with GNU Affero General Public License v3.0 5 votes vote down vote up
/** Creates a new entity manager that does not auto commit. */
private EntityManager createEntityManager() {
	EntityManager entityManager = entityManagerFactory
			.createEntityManager();
	entityManager.setFlushMode(FlushModeType.COMMIT);
	entityManagerThreadLocal.set(entityManager);
	return entityManager;
}
 
Example 7
Source File: JtaEntityManager.java    From tomee with Apache License 2.0 5 votes vote down vote up
public void setFlushMode(final FlushModeType flushMode) {
    final EntityManager entityManager = getEntityManager();
    try {
        final Timer timer = Op.setFlushMode.start(this.timer, this);
        try {
            entityManager.setFlushMode(flushMode);
        } finally {
            timer.stop();
        }
    } finally {
        closeIfNoTx(entityManager);
    }
}