public static EntityManagerFactory getEMF() {
   if (emf == null) {
     emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
     System.out.println(emf.isOpen());
   }
   return emf;
 }
 public static void reset() {
   if (emf != null && emf.isOpen()) {
     emf.close();
   }
   emf = Persistence.createEntityManagerFactory("org.drools.grid");
   whitePages = new JpaWhitePages(emf);
 }
 private void close() {
   if (!entityManager.isOpen()) {
     entityManager.close();
   }
   if (factory.isOpen()) {
     factory.close();
   }
 }
  private void open() {

    if (!factory.isOpen()) {
      factory = Persistence.createEntityManagerFactory("Introducao_a_Design_PatternsPU");
    }
    if (!entityManager.isOpen()) {
      entityManager = factory.createEntityManager();
    }
  }
 /** @see contact.service.DaoFactory#shutdown() */
 @Override
 public void shutdown() {
   try {
     if (em != null && em.isOpen()) em.close();
     if (emf != null && emf.isOpen()) emf.close();
   } catch (IllegalStateException ex) {
     logger.log(Level.SEVERE, ex.getMessage());
   }
 }
 public static void closeEntityManagerFactory(String persistenceUnitName) {
   EntityManagerFactory emfNamedPersistenceUnit =
       emfNamedPersistenceUnits.get(persistenceUnitName);
   if (emfNamedPersistenceUnit != null) {
     if (emfNamedPersistenceUnit.isOpen()) {
       emfNamedPersistenceUnit.close();
     }
     emfNamedPersistenceUnits.remove(persistenceUnitName);
   }
 }
 public void setUp(boolean overrideEMFCachingForTesting) {
   super.setUp();
   initialize();
   ServerSession session = JUnitTestCase.getServerSession(PERSISTENCE_UNIT_NAME);
   // if(!isDatabaseSchemaCreated | overrideEMFCachingForTesting) {
   if (null == entityManagerFactory
       || (null != entityManagerFactory && !entityManagerFactory.isOpen())) {
     new MetamodelTableCreator().replaceTables(session);
     isDatabaseSchemaCreated = true;
   }
 }
 public void resetEntityManagerFactory() {
   try {
     if (null != entityManagerFactory && entityManagerFactory.isOpen()) {
       entityManagerFactory.close();
     }
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     entityManagerFactory = null;
   }
 }
 public EntityManagerFactory initialize(boolean overrideEMFCachingForTesting) {
   try {
     if (null == entityManagerFactory
         || overrideEMFCachingForTesting
         || !entityManagerFactory.isOpen()) {
       entityManagerFactory = getEntityManagerFactory(PERSISTENCE_UNIT_NAME);
     }
   } catch (Exception e) {
     e.printStackTrace();
   }
   return entityManagerFactory;
 }
 @AfterClass
 public static void tearDown() {
   if (emf != null && emf.isOpen()) {
     EntityManager em = emf.createEntityManager();
     em.getTransaction().begin();
     em.createNativeQuery("DROP TABLE SIMPLE_TYPE_B").executeUpdate();
     em.createNativeQuery("DROP TABLE SIMPLE_TYPE_A").executeUpdate();
     em.getTransaction().commit();
     em.close();
     emf.close();
   }
 }
  /** Closes this instance. */
  public void close() {
    if (em != null && em.isOpen()) {
      rollbackTx();
      em.close();
    }

    if (emf != null && emf.isOpen()) {
      emf.close();
    }

    removeInstance();
  }
  // Will only get calls for the methods on the EntityManagerFactory interface
  @SuppressWarnings({"unchecked"})
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

    Object result = null;
    try {
      debug("EMFProxy invocation on method ", method.getName());

      /* NOTE: Allow close() to pass through in order for users to close the EMF. */
      /* This means that any user that closes an EMF will cause it to be closed   */
      /* for all other service references of the EMF service.                     */

      // Invoke these methods on the actual proxy (not the object it's proxying)
      if (method.getName().equals("hashCode")) return this.hashCode();
      if (method.getName().equals("toString")) return this.toString();

      /*===========================================================================*/
      /* ** NOTE: What if the provider supports multiple EMFs for the same punit?  */
      /*          Should we ignore the cache and just call the provider each time? */
      /*          Not right now.                                                   */
      /*===========================================================================*/

      // Get the EMF (setting a new one if one does not already exist)
      EntityManagerFactory emf = syncGetEMFAndSetIfAbsent(false, new HashMap<String, Object>());
      // Invoke the EMF method that was called
      result = method.invoke(emf, args);

      // If it was a getProperties() method then add in a PUnitInfo entry
      if (method.getName().equals("getProperties")) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.putAll((Map<String, Object>) result);
        resultMap.put("PUnitInfo", pUnitInfo.toMap());
        result = resultMap;
      }

      // If the operation was to close the EMF then chuck our reference away
      if (!emf.isOpen()) {
        syncUnsetEMF();
      }
      // Bug #401944 Catch InvocationTargetEx and get cause. Rethrow exception.
    } catch (InvocationTargetException itEx) {
      warning(
          "EMFProxy invocation on target method " + method.getName() + " failed with cause: ",
          itEx.getCause());
      throw itEx.getCause();
    } catch (Throwable t) {
      warning("EMFProxy invocation on method " + method.getName() + " failed: ", t);
      throw t;
    }
    return result;
  }
  @Override
  public EntityManager getEntityManager() throws IllegalStateException {
    if (entityManagerFactory == null || !entityManagerFactory.isOpen()) {
      throw new IllegalStateException(
          "EntityManagerFactory is closed. You must now dispose of this instance.");
    }

    try {
      EntityManager em = threadLocal.get();
      if (em == null) {
        em = entityManagerFactory.createEntityManager();
        threadLocal.set(em);
      }
      return em;
    } catch (Exception e) {
      Logger.error("Error creating entity manager: %s", e.getMessage());
      throw new JpaException(e, "Error creating entity manager: %s", e.getMessage());
    }
  }
  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();
    }
  }
Exemple #15
0
 public static void closeConexao() {
   if (emf.isOpen()) {
     emf.close();
   }
 }
 public static boolean doesEntityManagerFactoryExist(String persistenceUnitName) {
   EntityManagerFactory emf = emfNamedPersistenceUnits.get(persistenceUnitName);
   return emf != null && emf.isOpen();
 }
Exemple #17
0
 @Override
 public void close() {
   if (factory != null && factory.isOpen()) {
     factory.close();
   }
 }
  // Will only get calls for the method on the EntityManagerFactoryBuilder interface
  @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

    debug("EMFBuilderProxy invocation on method ", method.getName());

    if (method.getName().equals("hashCode")) return this.hashCode();
    if (method.getName().equals("equals")) return this.equals(args[0]);
    if (method.getName().equals("toString")) return this.toString();

    // Must be a createEntityManagerFactory(Map) call

    // The first arg should be the properties Map
    Map<String, Object> props = null;
    try {
      props = (Map<String, Object>) args[0];
    } catch (ClassCastException ccEx) {
      // Was some unexpected call, return null
      warning("EMFBuilderProxy cannot handle method ", method.getName());
      return null;
    }

    // If EclipseLink SESSION_NAME property specified then just create and return an EMF
    // NOTE: This will return an EMF that is not managed by Gemini.
    //       The caller will be responsible for managing it.
    if (EclipseLinkProvider.containsSessionName(props)) {
      // Let EclipseLink do its thing and create an EMF. Just return it.
      debug(
          "EMFBuilder found Eclipselink session name property - returning unmanaged EMF for props ",
          props);
      return super.createEMF(props);
    }

    // If we have an EMF and it has already been closed, discard it
    EntityManagerFactory emf;
    synchronized (pUnitInfo) {
      emf = pUnitInfo.getEmf();
      if ((emf != null) && (!emf.isOpen())) {
        syncUnsetEMF();
        emfProps.clear();
        emf = null;
      }
    }

    // Check if an EMF already exists
    if (emf != null) {

      // Verify the JDBC properties match the ones that may have been previously passed in
      verifyJDBCProperties(
          (String) emfProps.get(JPA_JDBC_DRIVER_PROPERTY),
          (String) emfProps.get(JPA_JDBC_URL_PROPERTY),
          props);

      // If it was created using the builder service then just return it
      if (pUnitInfo.isEmfSetByBuilderService()) {
        return emf;
      }

      // It was created using the EMF service.
      // Verify the JDBC properties match the ones in the descriptor.
      verifyJDBCProperties(pUnitInfo.getDriverClassName(), pUnitInfo.getDriverUrl(), props);
      return emf;
    } else {
      // No EMF service existed; create another
      return syncGetEMFAndSetIfAbsent(true, props);
    }
  }
 @PreDestroy
 public void shutdownEmf() {
   if (emf != null && emf.isOpen()) {
     emf.close();
   }
 }
Exemple #20
0
 public static EntityManagerFactory getConexao() throws Exception {
   if ((emf == null) || (!emf.isOpen())) {
     emf = Persistence.createEntityManagerFactory("IFSUL_CRUDPU");
   }
   return emf;
 }
 @After
 public void tearDown() throws Exception {
   if (entityManagerComment.isOpen()) entityManagerComment.close();
   if (entityManagerFactory.isOpen()) entityManagerFactory.close();
   if (entityManagerNews.isOpen()) entityManagerNews.close();
 }