Example usage for org.hibernate Transaction getStatus

List of usage examples for org.hibernate Transaction getStatus

Introduction

In this page you can find the example usage for org.hibernate Transaction getStatus.

Prototype

TransactionStatus getStatus();

Source Link

Document

Get the current local status of this transaction.

Usage

From source file:org.jboss.additional.testsuite.jdkall.past.jpa.hibernate.secondlevelcache.SFSB.java

License:Open Source License

public Student getStudent(int id) {
    Student student;/* ww w . j  a v a 2  s  .c o  m*/

    try {
        Session session = sessionFactory.openSession();
        Transaction ormTransaction = session.beginTransaction(); // join the current JTA transaction
        TransactionStatus status = ormTransaction.getStatus();
        if (status.isNotOneOf(TransactionStatus.ACTIVE)) {
            throw new RuntimeException(
                    "Hibernate Transaction is not active after joining Hibernate to JTA transaction: "
                            + status.name());
        }
        student = session.load(Student.class, id);
        session.close();

    } catch (Exception e) {

        e.printStackTrace();
        throw new RuntimeException("Failure while loading student entity", e);

    }
    return student;
}

From source file:org.mycore.frontend.cli.MCRIFS2Commands.java

License:Open Source License

private static void fixMCRFSNODESForDerivate(String content_store, String derivate_id, boolean check_only) {
    // check input
    MCRObjectID mcr_derivate_id;/*from  w  w  w . j  a v a2s  . c  o  m*/
    try {
        mcr_derivate_id = MCRObjectID.getInstance(derivate_id);
    } catch (MCRException e) {
        LOGGER.error("Wrong derivate parameter, it is not a MCRObjectID");
        return;
    }
    if (content_store == null || content_store.length() == 0) {
        LOGGER.error("Empty content store parameter");
        return;
    }
    MCRContentStore store = MCRContentStoreFactory.getStore(content_store);
    if (!(store instanceof MCRCStoreIFS2)) {
        LOGGER.error("The content store is not a IFS2 type");
        return;
    }
    // list all files
    try {
        MCRFileCollection file_collection = ((MCRCStoreIFS2) store).getIFS2FileCollection(mcr_derivate_id);
        File root_node = file_collection.getLocalFile();
        String storage_base = root_node.getAbsolutePath();
        storage_base = storage_base.substring(0, storage_base.length() - derivate_id.length());
        fixMCRFSNODESForNode(root_node, content_store, derivate_id, storage_base, check_only);
    } catch (IOException e) {
        LOGGER.error("Error while list all files of derivate with ID " + mcr_derivate_id.toString());
        e.printStackTrace();
    }
    Session session = MCRHIBConnection.instance().getSession();
    Transaction tx = session.getTransaction();
    if (tx.getStatus().isOneOf(TransactionStatus.ACTIVE)) {
        tx.commit();
    }
}

From source file:org.mycore.frontend.cli.MCRIFS2Commands.java

License:Open Source License

private static void fixDirectoryEntry(File node, String derivate_id, String storage_base, boolean check_only) {
    String name = node.getName();
    LOGGER.debug("fixDirectoryEntry : name = " + name);
    Session session = MCRHIBConnection.instance().getSession();
    Transaction tx = session.getTransaction();
    if (tx.getStatus().isNotOneOf(TransactionStatus.ACTIVE)) {
        tx.begin();//  www .j a v a2  s  .  com
    }
    EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<MCRFSNODES> query = cb.createQuery(MCRFSNODES.class);
    Root<MCRFSNODES> nodes = query.from(MCRFSNODES.class);
    try {
        em.detach(em.createQuery(query.where(cb.equal(nodes.get(MCRFSNODES_.owner), derivate_id),
                cb.equal(nodes.get(MCRFSNODES_.name), name), cb.equal(nodes.get(MCRFSNODES_.type), "D")))
                .getSingleResult());
        LOGGER.debug("Found directory entry for " + name);
        return;
    } catch (NoResultException e) {
        LOGGER.error("Can't find directory entry for " + name);
        if (check_only)
            return;
    } catch (NonUniqueResultException e) {
        LOGGER.error("Non unique directory entry for " + name);
        return;
    } catch (Exception e) {
        e.printStackTrace();
    }
    // fix entry
    LOGGER.info("Fix entry for directory " + name);
    MCRFileMetadataManager fmmgr = MCRFileMetadataManager.instance();
    String id = fmmgr.createNodeID();
    String pid = null;
    try {
        pid = getParentID(node, derivate_id);
    } catch (NoResultException e1) {
        if (!derivate_id.equals(name)) {
            LOGGER.error("Can't find parent id for directory " + name);
            return;
        }
    } catch (NonUniqueResultException e1) {
        LOGGER.error("The directory entry for " + derivate_id + " and " + node.getParentFile().getName()
                + " is not unique!");
        return;
    }
    try {
        MCRFSNODES mcrfsnodes = new MCRFSNODES();
        mcrfsnodes.setId(id);
        mcrfsnodes.setPid(pid);
        mcrfsnodes.setType("D");
        mcrfsnodes.setOwner(derivate_id);
        mcrfsnodes.setName(node.getName());
        mcrfsnodes.setDate(new Date(node.lastModified()));
        em.persist(mcrfsnodes);
        tx.commit();
        LOGGER.debug("Entry " + name + " fixed.");
    } catch (HibernateException he) {
        if (tx != null) {
            tx.rollback();
        }
        he.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.wildfly.extras.db_bootstrap.databasebootstrap.PersonSchema.java

License:Apache License

private static void run(WithHibernateSession callback) {
    Session session = null;/*from w ww  .ja  va  2 s .c o  m*/
    Transaction tx = null;
    try {
        session = HibernateTestUtil.getSessionFactory().openSession();
        tx = session.beginTransaction();
        callback.execute(session);
        tx.commit();
    } finally {
        if (tx.getStatus() == TransactionStatus.ACTIVE) {
            tx.rollback();
        }
        if (session != null) {
            session.close();
        }
    }
}