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:fr.mby.portal.coreimpl.auth.DbPortalUserAuthenticationProvider.java

@Override
public IAuthentication authenticate(final IAuthentication authentication) throws AuthenticationException {
    Assert.notNull(authentication, "No authentication supplied !");
    Assert.isInstanceOf(PortalUserAuthentication.class, authentication,
            "IAuthentication shoud be a PortalUserAuthentication type !");

    if (authentication.isAuthenticated()) {
        throw new AuthenticationException(authentication,
                "Already authenticated IAuthentication token supplied !");
    }//from   w w  w.j  a v a 2s .  co m

    final PortalUserAuthentication auth = (PortalUserAuthentication) authentication;

    IAuthentication resultingAuth = null;

    final String login = authentication.getName();
    final EntityManager portalUserEm = this.portalUserEmf.createEntityManager();
    final PortalUser portalUser = this.findPortalUserByLogin(portalUserEm, login);
    portalUserEm.close();

    if (portalUser != null) {
        // We found the user in the DB => we can authenticate him
        resultingAuth = this.performAuthentication(auth, portalUser);
    }

    if (resultingAuth == null) {
        // throw new AuthenticationException(authentication, "Unable to authenticate supplied authentication !");
    }

    return resultingAuth;
}

From source file:com.headissue.pigeon.service.AdminSurveyServiceTest.java

@Before
public void setUp() throws Exception {
    injector.injectMembers(this);

    SurveyValue _value = getSurveyValue("testSurvey.json");
    survey = adminSurveyService.createSurvey(_value, CREATE_DATE);
    EntityManager _manager = factory.createEntityManager();
    _manager.getTransaction().begin();// w  ww  . j ava2s.  co  m
    _manager.persist(survey);
    _manager.getTransaction().commit();
    _manager.close();

    UserAnswerValues a = new UserAnswerValues();
    a.setPageKey("testSurvey");
    a.setUserData("gans");
    a.setUserKey("gustav");
    a.setAnswers(new ArrayList<UserAnswerValue>());
    AnswerParameter p = new AnswerParameter();
    p.setSurveyId(survey.getId());
    p.setTimestamp(ANSWER_DATE.getTime());
    p.setValues(a);
    LogUtils.debug(log, "survey id:    %s", survey.getId());
    for (Question q : survey.getQuestions()) {
        LogUtils.debug(log, "querstion id: %s", q.getId());
        UserAnswerValue v = new UserAnswerValue();
        v.setQuestionId(q.getId());
        v.setValues(Arrays.asList(q.getAnswers().get(0).getText()));
        a.getAnswers().add(v);
        for (QuestionText qt : q.getAnswers()) {
            LogUtils.debug(log, "answert id:   %s", qt.getId());
        }
    }

    SessionManager _sessionManager = new SessionManager();
    AnswerHandler _answerHandler = injector.getInstance(AnswerHandler.class);
    _answerHandler.receiveAnswer(_sessionManager.getCurrentSession(), p);
}

From source file:com.soen.smbank.dao.ObjectDao.java

public ArrayList getAllObjectsByCondition(String tableName, String whereString) {
    EntityManager em = this.getEMF().createEntityManager();
    ArrayList entities = null;//from  w  w w  .  ja  v  a 2  s .  co  m
    try {
        entities = (ArrayList) em.createQuery("SELECT tb FROM " + tableName + " tb WHERE " + whereString)
                .getResultList();

        return entities;
    } finally {
        em.close();
    }
}

From source file:com.webbfontaine.valuewebb.action.tt.TtLogger.java

private void persistLog(TtLog ttLog) {
    EntityManager entityManager = null;
    try {//w  ww .j a va  2s.c  o m
        entityManager = Utils.createEntityManager();
        entityManager.persist(ttLog);
        entityManager.flush();
    } finally {
        if (entityManager != null) {
            entityManager.close();
        }
    }
}

From source file:edu.kit.dama.mdm.dataorganization.impl.jpa.DataOrganizerImpl.java

@Override
public IDataOrganizationNode loadNode(NodeId nodeId) {
    PersistenceFacade pf = PersistenceFacade.getInstance();
    EntityManager em = pf.getEntityManagerFactory().createEntityManager();
    DataOrganizationNode node = PersistenceFacade.getInstance().findNodeByNodeId(nodeId, em);
    em.close();
    return node;//from w w w. j av  a2 s.  com
}

From source file:com.soen.ebanking.dao.ObjectDao.java

public ArrayList getAllObjectsByCondition(String tableName, String whereString) {
    EntityManager em = this.getEMF().createEntityManager();
    ArrayList entities = null;// w  w w.  jav  a2  s .  c om
    try {
        entities = (ArrayList) em.createQuery("SELECT t FROM " + tableName + " t WHERE " + whereString)
                .getResultList();

        return entities;
    } finally {
        em.close();
    }
}

From source file:com.epam.training.taranovski.web.project.repository.implementation.AdminRepositoryImplementation.java

@Override
public Admin getById(int id) {
    EntityManager em = entityManagerFactory.createEntityManager();
    Admin admin;//from  ww  w  .  j a v  a  2 s .c om
    try {
        em.getTransaction().begin();
        admin = em.find(Admin.class, id);
        em.getTransaction().commit();
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return admin;
}

From source file:comp.web.core.DataUtil.java

public List<Product> getProds(String cat, String prod, String from, String to) {
    logger.log(Level.FINER, "get prods with filter {0} {1} {2} {3}", new Object[] { cat, prod, from, to });

    if (StringUtils.isBlank(cat) && StringUtils.isBlank(prod) && StringUtils.isBlank(from)
            && StringUtils.isBlank(to)) {
        return Collections.emptyList();
    }/*www .  j ava 2 s.  c  o m*/

    String cat1 = StringUtils.stripToEmpty(cat) + "%";
    String prod1 = StringUtils.stripToEmpty(prod) + "%";
    double from1 = StringUtils.isNumeric(from) ? Double.parseDouble(from) : Double.MIN_VALUE;
    double to1 = StringUtils.isNumeric(to) ? Double.parseDouble(to) : Double.MAX_VALUE;

    EntityManager em = createEM();
    //        EntityTransaction tx = em.getTransaction();
    //        tx.begin();

    List<Product> products = em.createNamedQuery("priceList", Product.class).setParameter("cat", cat1)
            .setParameter("prod", prod1).setParameter("from", from1).setParameter("to", to1).getResultList();

    //        tx.commit();
    em.close();
    logger.log(Level.FINER, "get prods result size {0}", products.size());
    return products;
}

From source file:cz.fi.muni.pa165.dto.PrintedBookDAOTest.java

@Test
public void testFindprintedBookById() {
    EntityManager em = emf.createEntityManager();
    PrintedBookDAOImpl pbDAO = new PrintedBookDAOImpl();
    pbDAO.setManager(em);/* w  w w .ja va2s  .co  m*/
    PrintedBook pb = pbDAO.findPrintedBookById(1);

    em.close();
    assertEquals(1, pb.getIdPrintedBook());
}

From source file:com.sixsq.slipstream.persistence.Run.java

public static Run abort(String abortMessage, String uuid) {
    EntityManager em = PersistenceUtil.createEntityManager();
    Run run = Run.loadFromUuid(uuid, em);
    RuntimeParameter globalAbort = getGlobalAbort(run);
    if (!globalAbort.isSet()) {
        setGlobalAbortState(abortMessage, globalAbort);
    }//from  ww w  .ja  v  a  2  s .c  o  m
    if (run.state == States.Provisioning) {
        setRecoveryMode(run);
    }
    em.close();
    return run;
}