Example usage for javax.transaction UserTransaction getStatus

List of usage examples for javax.transaction UserTransaction getStatus

Introduction

In this page you can find the example usage for javax.transaction UserTransaction getStatus.

Prototype

int getStatus() throws SystemException;

Source Link

Document

Obtain the status of the transaction associated with the current thread.

Usage

From source file:org.ms123.common.ea.BaseEAServiceImpl.java

private Map importZipcodes(String storeId, String basedir) {
    StoreDesc sdesc = StoreDesc.get(storeId);
    PersistenceManager pm = m_nucleusService.getPersistenceManagerFactory(sdesc).getPersistenceManager();
    UserTransaction ut = m_nucleusService.getUserTransaction();
    Class _company = m_nucleusService.getClass(sdesc, "Company");
    Class _contact = m_nucleusService.getClass(sdesc, "Contact");
    Map mapping = initZipcodes();
    try {/*from   ww  w.  j ava  2  s .c  o m*/
        LabeledCSVParser lp = new LabeledCSVParser(
                getCSVParser(new FileInputStream(new File(basedir, "zipcodes.csv"))));
        System.out.println("Persisting zipcodes");
        int num = 0;
        while (lp.getLine() != null) {
            if (ut.getStatus() != Status.STATUS_ACTIVE) {
                ut.begin();
            }
            // Zipcode zipcode = new Zipcode(); 
            Object zipcode = m_nucleusService.getClass(sdesc, "Zipcode").newInstance();

            String gemeindekennziffer = lp.getValueByLabel("Gemeindekennziffer");
            String ortname = lp.getValueByLabel("ORTNAME");
            String plz = lp.getValueByLabel("PLZ");
            String lkz = lp.getValueByLabel("LKZ");
            PropertyUtils.setProperty(zipcode, "lkz", lkz);
            PropertyUtils.setProperty(zipcode, "plz", plz);
            PropertyUtils.setProperty(zipcode, "ortname", ortname);
            PropertyUtils.setProperty(zipcode, "gemeindekennziffer", gemeindekennziffer);

            Collection cl = getContactList(pm, _company, plz);
            if (cl != null) {
                for (Object cx : cl) {
                    PropertyUtils.setProperty(cx, "zipcode", zipcode);
                    PropertyUtils.setProperty(cx, "lkz", lkz);
                }
            }
            cl = getContactList(pm, _contact, plz);
            if (cl != null) {
                for (Object cx : cl) {
                    PropertyUtils.setProperty(cx, "zipcode", zipcode);
                    PropertyUtils.setProperty(cx, "lkz", lkz);
                }
            }
            pm.makePersistent(zipcode);
            if ((num % 1000) == 1) {
                System.out.println(num + ":\t" + new Date().getTime());
                ut.commit();
            }
            num++;
        }
        if (ut.getStatus() == Status.STATUS_ACTIVE) {
            ut.commit();
        }
        System.out.println("Contact and Book have been persisted");
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    } finally {
        pm.close();
    }
    return null;
}

From source file:org.ms123.common.ea.EACompanyContactImporter.java

private void doImport() throws Exception {
    LabeledCSVParser lp = new LabeledCSVParser(
            new ExcelCSVParser(new FileInputStream(new File(m_basedir, "ea.csv"))));
    int status;//w  w w  .  j  a v a2s  .c  om
    PersistenceManager pm = m_nucleusService.getPersistenceManagerFactory(m_storeDesc).getPersistenceManager();
    UserTransaction ut = m_nucleusService.getUserTransaction();
    int num = 0;
    Object company = null;
    String lastCompanyId = null;
    while (lp.getLine() != null) {
        String type = lp.getValueByLabel("type");
        String companyId = lp.getValueByLabel("companyId");
        if (type.startsWith("nok"))
            continue;
        if (ut.getStatus() != Status.STATUS_ACTIVE) {
            ut.begin();
        }
        String s[] = getStateAndEntity(type);
        Object obj = populate(lp, s[0], s[1]);

        if (!isEmpty(companyId)) {
            if (!companyId.equals(lastCompanyId)) {
                company = obj;
                lastCompanyId = companyId;
            }
            if (s[0].equals("contact")) {
                Set cl = (Set) PropertyUtils.getProperty(company, "contact_list");
                if (cl == null) {
                    cl = new HashSet();
                    PropertyUtils.setProperty(company, "contact_list", cl);
                }
                cl.add(obj);
            }
        }
        pm.makePersistent(obj);
        if ((num % 1000) == 1) {
            System.out.println(num + ":\t" + new Date().getTime());
            ut.commit();
        }
        num++;
    }
    if (ut.getStatus() == Status.STATUS_ACTIVE) {
        ut.commit();
    }
}

From source file:org.ms123.common.workflow.tasks.TaskScriptExecutor.java

private void _execute(TaskContext tc, Map addVars) {
    m_js.prettyPrint(true);/*  ww  w . j  a va  2s  .  c  om*/
    log(tc, "TaskScriptExecutor._execute:" + tc.getScript());
    printInfo(tc);
    showVariablenNames(tc);
    SessionContext sc = getSessionContext(tc);
    Map<String, Object> vars = new HashMap(tc.getExecution().getVariables());
    log(tc, "TaskScriptExecutor.vars:" + m_js.deepSerialize(vars));
    if (addVars != null) {
        vars.putAll(addVars);
    }
    Map<String, Object> lvars = new HashMap();
    Map<String, Object> gvars = new HashMap();
    vars.put("lvars", lvars);
    vars.put("gvars", gvars);
    vars.put("execution", tc.getExecution());
    GroovyTaskDsl dsl = getGroovyTaskDsl(tc, sc, vars);
    UserTransaction tx = sc.getUserTransaction();
    Object ret = null;
    try {
        log(tc, "transaction.status:" + tx.getStatus() + "/"
                + org.ms123.common.system.thread.ThreadContext.getThreadContext());
        ret = dsl.eval(tc.getScript());
        log(tc, "TaskScriptExecutor.gvars:" + m_js.deepSerialize(gvars));
        tc.getExecution().setVariables(gvars);
        tc.getExecution().setVariablesLocal(lvars);
        for (Object o : dsl.getCreatedObjects()) {
            log(tc, "createdObject:" + o);
            sc.retrieve(o);
        }
        for (Object o : dsl.getQueriedObjects()) {
            log(tc, "queriedObject:" + o);
            sc.retrieve(o);
        }
    } catch (Exception e) {
        sc.handleException(tx, e);
    } finally {
        sc.handleFinally(tx);
    }
}

From source file:org.ms123.common.workflow.TaskScriptExecutor.java

private void _execute(TaskContext tc, Map addVars) {
    m_js.prettyPrint(true);/*from  w w w  . j  a  v a  2 s .c o  m*/
    log(tc, "TaskScriptExecutor._execute:" + tc.getScript());
    printInfo(tc);
    showVariablenNames(tc);
    SessionContext sc = getSessionContext(tc);
    Map<String, Object> vars = new HashMap(tc.getExecution().getVariables());
    log(tc, "TaskScriptExecutor.vars:" + m_js.deepSerialize(vars));
    if (addVars != null) {
        vars.putAll(addVars);
    }
    Map<String, Object> lvars = new HashMap();
    Map<String, Object> gvars = new HashMap();
    vars.put("lvars", lvars);
    vars.put("gvars", gvars);
    vars.put("execution", tc.getExecution());
    GroovyTaskDsl dsl = getGroovyTaskDsl(tc, sc, vars);
    UserTransaction tx = sc.getUserTransaction();
    Object ret = null;
    try {
        log(tc, "transaction.status:" + tx.getStatus() + "/"
                + org.ms123.common.system.ThreadContext.getThreadContext());
        ret = dsl.eval(tc.getScript());
        log(tc, "TaskScriptExecutor.gvars:" + m_js.deepSerialize(gvars));
        tc.getExecution().setVariables(gvars);
        tc.getExecution().setVariablesLocal(lvars);
        for (Object o : dsl.getCreatedObjects()) {
            log(tc, "createdObject:" + o);
            sc.retrieve(o);
        }
        for (Object o : dsl.getQueriedObjects()) {
            log(tc, "queriedObject:" + o);
            sc.retrieve(o);
        }
    } catch (Exception e) {
        sc.handleException(tx, e);
    } finally {
        sc.handleFinally(tx);
    }
}

From source file:org.nuxeo.runtime.transaction.TransactionHelper.java

/**
 * Gets the transaction status./*from   ww w  .ja v a  2s. c o m*/
 *
 * @return the transaction {@linkplain Status status}, or -1 if there is no transaction manager
 * @since 8.4
 * @see Status
 */
public static int getTransactionStatus() {
    UserTransaction ut = NuxeoContainer.getUserTransaction();
    if (ut == null) {
        return -1;
    }
    try {
        return ut.getStatus();
    } catch (SystemException e) {
        throw new TransactionRuntimeException("Cannot get transaction status", e);
    }
}

From source file:org.nuxeo.runtime.transaction.TransactionHelper.java

/**
 * Commits or rolls back the User Transaction depending on the transaction status.
 *///from w ww.  j a v a2s .c  o m
public static void commitOrRollbackTransaction() {
    UserTransaction ut = NuxeoContainer.getUserTransaction();
    if (ut == null) {
        return;
    }
    try {
        int status = ut.getStatus();
        if (status == Status.STATUS_ACTIVE) {
            if (log.isDebugEnabled()) {
                log.debug("Commiting transaction");
            }
            ut.commit();
        } else if (status == Status.STATUS_MARKED_ROLLBACK) {
            if (log.isDebugEnabled()) {
                log.debug("Cannot commit transaction because it is marked rollback only");
            }
            ut.rollback();
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Cannot commit transaction with unknown status: " + status);
            }
        }
    } catch (SystemException | RollbackException | HeuristicMixedException | HeuristicRollbackException
            | IllegalStateException | SecurityException e) {
        String msg = "Unable to commit/rollback";
        String message = e.getMessage();
        if (e instanceof RollbackException) {
            switch (message) {
            case "Unable to commit: transaction marked for rollback":
                // don't log as error, this happens if there's a ConcurrentModificationException
                // at transaction end inside VCS
            case "Unable to commit: Transaction timeout":
                // don't log either
                log.debug(msg, e);
                break;
            default:
                log.error(msg, e);
            }
        } else {
            log.error(msg, e);
        }
        throw new TransactionRuntimeException(msg + ": " + message, e);
    }
}

From source file:org.quartz.plugins.SchedulerPluginWithUserTransactionSupport.java

/**
 * If the given UserTransaction is not null, it is committed/rolledback,
 * and then returned to the UserTransactionHelper.
 *//*  www. j a va 2 s .c o  m*/
private void resolveUserTransaction(UserTransaction userTransaction) {
    if (userTransaction != null) {
        try {
            if (userTransaction.getStatus() == Status.STATUS_MARKED_ROLLBACK) {
                userTransaction.rollback();
            } else {
                userTransaction.commit();
            }
        } catch (Throwable t) {
            getLog().error("Failed to resolve UserTransaction for plugin: " + getName(), t);
        } finally {
            UserTransactionHelper.returnUserTransaction(userTransaction);
        }
    }
}

From source file:org.tolven.web.servlet.InternalErrorHandleServlet.java

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //check if a transaction is already started, if not redirect to the servlet
    UserTransaction ut = null;
    try {// w  w  w  . j  av  a2 s . c  o  m
        ut = (UserTransaction) ctx.lookup("UserTransaction");
        if (ut.getStatus() != Status.STATUS_ACTIVE) {
            ut.begin();
        }
        //check if the error.email is configured. If found send an email
        Object accountUser = TolvenRequest.getInstance().getAccountUser();
        if (accountUser != null) {
            String errorEmail = ((AccountUser) accountUser).getAccount().getProperty()
                    .get("org.tolven.error.email.to");
            if (!StringUtils.isBlank(errorEmail)) {
                System.out.println("Send an email to " + errorEmail);
            }
        }
        if (ut != null) {
            ut.commit();
        }
        RequestDispatcher dispatcher = this.config.getServletContext().getRequestDispatcher("/error.jsp");
        if (dispatcher != null)
            dispatcher.forward(req, resp);
    } catch (Exception e) {
        throw new ServletException("Exception thrown in InternalErrorHandleServlet", e);
    } finally {
        if (ut != null) {
            try {
                ut.rollback();
            } catch (Exception e) {
                throw new ServletException("Exception thrown in InternalErrorHandleServlet", e);
            }
        }
    }
}

From source file:org.xchain.namespaces.jta.TransactionCommand.java

public boolean execute(JXPathContext context) throws Exception {
    boolean managingTransaction = false;
    boolean result = false;
    UserTransaction transaction = ContainerContext.getJtaLookupStrategy().getUserTransaction();

    if (transaction != null) {
        try {/*w  ww  .  j  a  v  a  2 s . c o  m*/
            // if there is not a transaction running, then start one.
            if (Status.STATUS_ACTIVE != transaction.getStatus()) {
                managingTransaction = true;
                transaction.setTransactionTimeout(this.getTimeout(context));
                transaction.begin();
            }

            // put the transaction into the context.
            ((ScopedQNameVariables) context.getVariables()).declareVariable(getResult(context), transaction,
                    Scope.execution);

            // execute the chain 
            result = super.execute(context);

            // roll back the transaction.
            if (managingTransaction) {
                transaction.commit();
            }
        } catch (Exception e) {
            if (managingTransaction) {
                if (transaction.getStatus() != Status.STATUS_NO_TRANSACTION) {
                    transaction.rollback();
                }
            }
            throw e;
        } finally {
            // TODO: If we defined the transaction variable, then we should remove it.
        }
    } else {
        // TODO: Should this throw an IllegalStateException?  Returning true seems like the wrong behavior.
        result = true;
    }
    return result;
}