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

The following examples show how to use javax.persistence.EntityManager#isOpen() . 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: JpaDb.java    From crushpaper with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Returns an entity manager and creates it if needed. Also recreates it if
 * it has been spontaneously closed which is a real thing that can happen.
 * 
 * @return
 */
private EntityManager getOrCreateEntityManager() {
	buildEntityManagerFactory();

	EntityManager entityManager = entityManagerThreadLocal.get();
	if (entityManager == null) {
		entityManager = createEntityManager();
	}

	if (!entityManager.isOpen()) {
		entityManager = createEntityManager();
	}

	EntityTransaction transaction = entityManager.getTransaction();
	if (!transaction.isActive())
		transaction.begin();

	return entityManager;
}
 
Example 2
Source File: EjbqlApp.java    From jasperreports with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 *
 */
public void fill() throws JRException
{
	long start = System.currentTimeMillis();
	// create entity manager factory for connection with database
	EntityManagerFactory emf = Persistence.createEntityManagerFactory("pu1", new HashMap<Object, Object>());
	EntityManager em = emf.createEntityManager();

	try
	{
		Map<String, Object> parameters = getParameters(em);
		
		JasperFillManager.fillReportToFile("build/reports/JRMDbReport.jasper", parameters);

		em.close();
		
		System.err.println("Filling time : " + (System.currentTimeMillis() - start));
	}
	finally
	{
		if (em.isOpen())
			em.close();
		if (emf.isOpen())
			emf.close();
	}
}
 
Example 3
Source File: Resources.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
public void dispose(@Disposes @Default EntityManager entityManager)
{
    if (entityManager.isOpen())
    {
        entityManager.close();
    }
}
 
Example 4
Source File: EntityManagerUtils.java    From james-project with Apache License 2.0 5 votes vote down vote up
/**
 * Safely close an EntityManager by rolling back any active transaction and then closing it. That is needed to prevent memory leaks.
 * @param entityManager the entity manager to close
 */
public static void safelyClose(EntityManager entityManager) {

    if (entityManager == null) {
        return;
    }

    if (entityManager.isOpen()) {
        if (entityManager.getTransaction().isActive()) {
            entityManager.getTransaction().rollback();
        }
        entityManager.close();
    }

}
 
Example 5
Source File: TestEntityManagerProducer.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
protected void closeFirstEntityManager(@Disposes @First EntityManager entityManager)
{
    if (entityManager.isOpen())
    {
        entityManager.close();
    }
    closeEntityManagerCountFirstEntityManager++;
}
 
Example 6
Source File: TestEntityManagerProducer.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
protected void closeSecondEntityManager(@Disposes @Second EntityManager entityManager)
{
    if (entityManager.isOpen())
    {
        entityManager.close();
    }
    closeEntityManagerCountSecondEntityManager++;
}
 
Example 7
Source File: TestEntityManagerProducer.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
protected void closeDefaultEntityManager(@Disposes @Default EntityManager entityManager)
{
    if (entityManager.isOpen())
    {
        entityManager.close();
    }
    closeEntityManagerCountDefaultEntityManager++;
}
 
Example 8
Source File: TestEntityManagerProducer.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
protected void closeFirstEntityManager(@Disposes @First EntityManager entityManager)
{
    if (entityManager.isOpen())
    {
        entityManager.close();
    }
    closeEntityManagerCountFirstEntityManager++;
}
 
Example 9
Source File: RangerKMSDB.java    From ranger with Apache License 2.0 4 votes vote down vote up
private boolean isDbConnected() {
	EntityManager em = getEntityManager();
	
	return em != null && em.isOpen();
}
 
Example 10
Source File: EntityManagerProducer.java    From HotswapAgentExamples with GNU General Public License v2.0 4 votes vote down vote up
public void close(@Disposes EntityManager em) {
    if (em.isOpen()) {
        em.close();
    }
}
 
Example 11
Source File: DBAuditDestination.java    From ranger with Apache License 2.0 4 votes vote down vote up
private boolean isDbConnected() {
	EntityManager em = getEntityManager();
	return em != null && em.isOpen();
}
 
Example 12
Source File: EntityManagerProducer.java    From jpa-unit with Apache License 2.0 4 votes vote down vote up
public void close(@Disposes final EntityManager em) {
    if (em.isOpen()) {
        em.close();
    }
}
 
Example 13
Source File: EntityManagerProducer.java    From tomee with Apache License 2.0 4 votes vote down vote up
public void dispose(@Disposes @Default EntityManager entityManager) {
    if (entityManager.isOpen()) {
        entityManager.close();
    }
}
 
Example 14
Source File: EntityManagerProducer.java    From jpa-unit with Apache License 2.0 4 votes vote down vote up
public void close(@Disposes final EntityManager em) {
    if (em.isOpen()) {
        em.close();
    }
}
 
Example 15
Source File: EntityManagerProducer.java    From hammock with Apache License 2.0 4 votes vote down vote up
public void closeEM(@Disposes @Any EntityManager entityManager) {
   if(entityManager.isOpen()) {
      entityManager.close();
   }
}
 
Example 16
Source File: JPAPersistenceManager.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void updateRecord(JSONObject aRecord, RegistryConfiguration registryConf) {

	EntityTransaction entityTransaction = null;

	logger.debug("IN");
	EntityManager entityManager = null;
	try {
		Assert.assertNotNull(aRecord, "Input parameter [record] cannot be null");
		Assert.assertNotNull(aRecord, "Input parameter [registryConf] cannot be null");

		logger.debug("New record: " + aRecord.toString(3));
		logger.debug("Target entity: " + registryConf.getEntity());

		entityManager = dataSource.getEntityManager();
		Assert.assertNotNull(entityManager, "entityManager cannot be null");

		entityTransaction = entityManager.getTransaction();

		EntityType targetEntity = getTargetEntity(registryConf, entityManager);
		String keyAttributeName = getKeyAttributeName(targetEntity);
		logger.debug("Key attribute name is equal to " + keyAttributeName);

		Iterator it = aRecord.keys();

		Object keyColumnValue = aRecord.get(keyAttributeName);
		logger.debug("Key of new record is equal to " + keyColumnValue);
		logger.debug("Key column java type equal to [" + targetEntity.getJavaType() + "]");
		Attribute a = targetEntity.getAttribute(keyAttributeName);
		Object obj = entityManager.find(targetEntity.getJavaType(), this.convertValue(keyColumnValue, a));
		logger.debug("Key column class is equal to [" + obj.getClass().getName() + "]");

		while (it.hasNext()) {
			String attributeName = (String) it.next();
			logger.debug("Processing column [" + attributeName + "] ...");

			if (keyAttributeName.equals(attributeName)) {
				logger.debug("Skip column [" + attributeName + "] because it is the key of the table");
				continue;
			}
			Column column = registryConf.getColumnConfiguration(attributeName);
			if (!column.isEditable()) {
				logger.debug("Skip column [" + attributeName + "] because it is not editable");
				continue;
			}
			List columnDepends = new ArrayList();
			if (column.getDependences() != null && !"".equals(column.getDependences())) {
				String[] dependences = column.getDependences().split(",");
				for (int i = 0; i < dependences.length; i++) {
					// get dependences informations
					Column dependenceColumns = getDependenceColumns(registryConf.getColumns(), dependences[i]);
					if (dependenceColumns != null)
						columnDepends.add(dependenceColumns);
				}
			}

			// if column is info column do not update
			if (!column.isInfoColumn()) {
				if (column.getSubEntity() != null) {
					logger.debug("Column [" + attributeName + "] is a foreign key");
					manageForeignKey(targetEntity, column, obj, attributeName, aRecord, columnDepends, entityManager, registryConf.getColumns());
				} else {
					logger.debug("Column [" + attributeName + "] is a normal column");
					manageProperty(targetEntity, obj, attributeName, aRecord);
				}
			}
		}

		if (!entityTransaction.isActive()) {
			entityTransaction.begin();
		}

		entityManager.persist(obj);
		entityManager.flush();
		entityTransaction.commit();

	} catch (Throwable t) {
		if (entityTransaction != null && entityTransaction.isActive()) {
			entityTransaction.rollback();
		}
		logger.error(t);
		throw new SpagoBIRuntimeException("Error saving entity", t);
	} finally {
		if (entityManager != null) {
			if (entityManager.isOpen()) {
				entityManager.close();
			}
		}
		logger.debug("OUT");
	}

}
 
Example 17
Source File: EntityManagerProducer.java    From jersey-jwt with MIT License 4 votes vote down vote up
public void closeEntityManager(@Disposes EntityManager entityManager) {
    if (entityManager.isOpen()) {
        entityManager.close();
    }
}
 
Example 18
Source File: PersonProducer.java    From tomee with Apache License 2.0 4 votes vote down vote up
public void closeEntityManager(@Disposes EntityManager manager) {
    if (manager.isOpen()) {
        manager.close();
    }
}
 
Example 19
Source File: DbAuditProvider.java    From ranger with Apache License 2.0 4 votes vote down vote up
private boolean isDbConnected() {
	EntityManager em = getEntityManager();
	
	return em != null && em.isOpen();
}
 
Example 20
Source File: TxScopedEntityManagerWrapper.java    From kumuluzee with MIT License 3 votes vote down vote up
@Override
public void close() {

    EntityManager nonTxEm = nonTxEmHolder.getEntityManager();

    if (nonTxEm != null && nonTxEm.isOpen()) {

        nonTxEm.close();

        nonTxEmHolder.setEntityManager(null);
    }
}