Example usage for javax.persistence EntityManager close

List of usage examples for javax.persistence EntityManager close

Introduction

In this page you can find the example usage for javax.persistence EntityManager close.

Prototype

public void close();

Source Link

Document

Close an application-managed entity manager.

Usage

From source file:Department.java

public static void main(String[] a) throws Exception {
        JPAUtil util = new JPAUtil();

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("ProfessorService");
        EntityManager em = emf.createEntityManager();
        ProfessorService service = new ProfessorService(em);

        em.getTransaction().begin();//from  w  w  w .  java 2 s  .com

        Professor emp = service.createProfessor("empName", 100L);

        Department dept = service.createDepartment("deptName");

        emp = service.setProfessorDepartment(emp.getId(), dept.getId());

        System.out.println(emp.getDepartment() + " with Professors:");
        System.out.println(emp.getDepartment().getProfessors());

        Collection<Professor> emps = service.findAllProfessors();
        if (emps.isEmpty()) {
            System.out.println("No Professors found ");
        } else {
            System.out.println("Found Professors:");
            for (Professor emp1 : emps) {
                System.out.println(emp1);
            }
        }

        Collection<Department> depts = service.findAllDepartments();
        if (depts.isEmpty()) {
            System.out.println("No Departments found ");
        } else {
            System.out.println("Found Departments:");
            for (Department dept1 : depts) {
                System.out.println(
                        dept1 + " with " + dept1.getProfessors().size() + " employees " + dept1.getProfessors());
            }
        }

        util.checkData("select * from Professor");

        util.checkData("select * from Department");
        em.getTransaction().commit();
        em.close();
        emf.close();
    }

From source file:com.impetus.kvapps.runner.AppRunner.java

/**
 * After successful processing close entity manager and it's factory instance.
 * //  w ww.  j a v  a  2s .c om
 * @param emf          entity manager factory instance.
 * @param em           entity manager instance
 */
private static void onDestroyDBResources(EntityManagerFactory emf, EntityManager em) {
    if (emf != null) {
        emf.close();
    }

    if (em != null) {
        em.close();
    }
}

From source file:au.org.ands.vocabs.toolkit.db.TaskUtils.java

/** Get a task by ID.
 * @param id task id//from  w  w w .  jav a 2s  .  c  o  m
 * @return The task
 */
public static Task getTaskById(final int id) {
    EntityManager em = DBContext.getEntityManager();
    Task t = em.find(Task.class, id);
    em.close();
    return t;
}

From source file:au.org.ands.vocabs.toolkit.db.TaskUtils.java

/** Get all tasks.
 * @return A list of Tasks/*ww w  . j  a  v  a2 s  . c  o m*/
 */
public static List<Task> getAllTasks() {
    EntityManager em = DBContext.getEntityManager();
    TypedQuery<Task> query = em.createNamedQuery(Task.GET_ALL_TASKS, Task.class);
    List<Task> tasks = query.getResultList();
    em.close();
    return tasks;
}

From source file:au.org.ands.vocabs.toolkit.db.TaskUtils.java

/** Set the status and data fields for a task.
 * @param task The task being updated//from  w ww . j  ava2  s . co m
 * @param status The updated status information
 * @param response The updated response
 */
public static void setTaskStatusAndData(final Task task, final String status, final String response) {
    EntityManager em = DBContext.getEntityManager();
    em.getTransaction().begin();
    task.setStatus(status);
    task.setResponse(response);
    em.merge(task);
    em.getTransaction().commit();
    em.close();
}

From source file:GestoreAccountLocale.GestoreAccountLocale.java

private static void memorizzaAccount(Account account) {
    EntityManagerFactory emf = javax.persistence.Persistence.createEntityManagerFactory("ClientMDBPU");
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();//from  ww  w  . j a v  a  2  s .c  om
    try {
        em.persist(account);
        em.getTransaction().commit();
    } catch (Exception e) {
        em.getTransaction().rollback();
    } finally {
        em.close();
    }
}

From source file:com.enioka.jqm.tools.Main.java

private static void upgrade() {
    try {/*from  w ww  .j  a  v  a 2 s.  c o  m*/
        Helpers.allowCreateSchema();
        jqmlogger.info("Upgrading database and shared metadata");
        EntityManager em = Helpers.getNewEm();
        Helpers.updateConfiguration(em);
        em.close();
    } catch (Exception e) {
        throw new JqmRuntimeException("Could not upgrade", e);
    }
}

From source file:io.symcpe.hendrix.api.dao.TestTemplateManager.java

@BeforeClass
public static void beforeClass() throws Exception {
    Properties config = new Properties(System.getProperties());
    File db = new File(TARGET_RULES_DB);
    if (db.exists()) {
        FileUtils.deleteDirectory(db);//from w  w  w .  j  a va 2  s  . c  om
    }
    config.setProperty("javax.persistence.jdbc.url", CONNECTION_STRING);
    try {
        emf = Persistence.createEntityManagerFactory("hendrix", config);
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }

    EntityManager em = emf.createEntityManager();

    Tenant tenant = new Tenant();
    tenant.setTenant_id(TENANT_ID_1);
    tenant.setTenant_name(TEST_TENANT);
    TenantManager.getInstance().createTenant(em, tenant);

    em.close();
}

From source file:com.srotya.tau.api.dao.TestTemplateManager.java

@BeforeClass
public static void beforeClass() throws Exception {
    Properties config = new Properties(System.getProperties());
    File db = new File(TARGET_RULES_DB);
    if (db.exists()) {
        FileUtils.deleteDirectory(db);/*from  ww w  .  j a va 2 s . c  o m*/
    }
    config.setProperty("javax.persistence.jdbc.url", CONNECTION_STRING);
    try {
        emf = Persistence.createEntityManagerFactory("tau", config);
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }

    EntityManager em = emf.createEntityManager();

    RuleGroup ruleGroup = new RuleGroup();
    ruleGroup.setRuleGroupId(RULE_GROUP_ID_1);
    ruleGroup.setRuleGroupName(TEST_RULE_GROUP);
    RuleGroupManager.getInstance().createRuleGroup(em, ruleGroup);

    em.close();
}

From source file:com.enioka.jqm.tools.Main.java

private static void createEngine(String nodeName) {
    try {/* w w w .  j  ava2 s . c o  m*/
        Helpers.allowCreateSchema();
        jqmlogger.info("Creating engine node " + nodeName);
        EntityManager em = Helpers.getNewEm();
        Helpers.updateConfiguration(em);
        Helpers.updateNodeConfiguration(nodeName, em);
        em.close();
    } catch (Exception e) {
        throw new JqmRuntimeException("Could not create the engine", e);
    }
}