下面列出了javax.persistence.EntityManager#isOpen ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
*
*/
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();
}
}
/**
* 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;
}
public void dispose(@Disposes @Default EntityManager entityManager)
{
if (entityManager.isOpen())
{
entityManager.close();
}
}
/**
* 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();
}
}
protected void closeSecondEntityManager(@Disposes @Second EntityManager entityManager)
{
if (entityManager.isOpen())
{
entityManager.close();
}
closeEntityManagerCountSecondEntityManager++;
}
protected void closeFirstEntityManager(@Disposes @First EntityManager entityManager)
{
if (entityManager.isOpen())
{
entityManager.close();
}
closeEntityManagerCountFirstEntityManager++;
}
protected void closeDefaultEntityManager(@Disposes @Default EntityManager entityManager)
{
if (entityManager.isOpen())
{
entityManager.close();
}
closeEntityManagerCountDefaultEntityManager++;
}
protected void closeFirstEntityManager(@Disposes @First EntityManager entityManager)
{
if (entityManager.isOpen())
{
entityManager.close();
}
closeEntityManagerCountFirstEntityManager++;
}
public void close(@Disposes EntityManager em) {
if (em.isOpen()) {
em.close();
}
}
private boolean isDbConnected() {
EntityManager em = getEntityManager();
return em != null && em.isOpen();
}
public void closeEntityManager(@Disposes EntityManager manager) {
if (manager.isOpen()) {
manager.close();
}
}
public void closeEntityManager(@Disposes EntityManager entityManager) {
if (entityManager.isOpen()) {
entityManager.close();
}
}
@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");
}
}
public void closeEM(@Disposes @Any EntityManager entityManager) {
if(entityManager.isOpen()) {
entityManager.close();
}
}
public void close(@Disposes final EntityManager em) {
if (em.isOpen()) {
em.close();
}
}
public void dispose(@Disposes @Default EntityManager entityManager) {
if (entityManager.isOpen()) {
entityManager.close();
}
}
public void close(@Disposes final EntityManager em) {
if (em.isOpen()) {
em.close();
}
}
private boolean isDbConnected() {
EntityManager em = getEntityManager();
return em != null && em.isOpen();
}
private boolean isDbConnected() {
EntityManager em = getEntityManager();
return em != null && em.isOpen();
}
@Override
public void close() {
EntityManager nonTxEm = nonTxEmHolder.getEntityManager();
if (nonTxEm != null && nonTxEm.isOpen()) {
nonTxEm.close();
nonTxEmHolder.setEntityManager(null);
}
}