Example usage for javax.transaction UserTransaction begin

List of usage examples for javax.transaction UserTransaction begin

Introduction

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

Prototype

void begin() throws NotSupportedException, SystemException;

Source Link

Document

Create a new transaction and associate it with the current thread.

Usage

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 {/*  ww w  .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;
}

From source file:ru.runa.wfe.commons.logic.InitializerLogic.java

/**
 * Initialize database./*  w w  w  .j a  v  a  2 s  . c  o  m*/
 * 
 * @param daoHolder
 *            Helper object for getting DAO's.
 */
protected void initializeDatabase(UserTransaction transaction) {
    log.info("database is not initialized. initializing...");
    SchemaExport schemaExport = new SchemaExport(ApplicationContextFactory.getConfiguration());
    schemaExport.create(true, true);
    try {
        transaction.begin();
        insertInitialData();
        constantDAO.setDatabaseVersion(dbPatches.size());
        transaction.commit();
    } catch (Throwable th) {
        Utils.rollbackTransaction(transaction);
        throw Throwables.propagate(th);
    }
}

From source file:ru.runa.wfe.commons.logic.InitializerLogic.java

/**
 * Apply patches to initialized database.
 *//* www  .  j  a  va2s  .  co  m*/
protected void applyPatches(UserTransaction transaction, int dbVersion) {
    log.info("Database version: " + dbVersion + ", code version: " + dbPatches.size());
    while (dbVersion < dbPatches.size()) {
        DBPatch patch = ApplicationContextFactory.createAutowiredBean(dbPatches.get(dbVersion));
        dbVersion++;
        log.info("Applying patch " + patch + " (" + dbVersion + ")");
        try {
            transaction.begin();
            patch.executeDDLBefore();
            patch.executeDML();
            patch.executeDDLAfter();
            constantDAO.setDatabaseVersion(dbVersion);
            transaction.commit();
            log.info("Patch " + patch.getClass().getName() + "(" + dbVersion
                    + ") is applied to database successfully.");
        } catch (Throwable th) {
            log.error("Can't apply patch " + patch.getClass().getName() + "(" + dbVersion + ").", th);
            Utils.rollbackTransaction(transaction);
            break;
        }
    }
}

From source file:ru.runa.wfe.service.impl.ReceiveMessageBean.java

@Override
public void onMessage(Message jmsMessage) {
    List<ReceiveMessageData> handlers = Lists.newArrayList();
    ObjectMessage message = (ObjectMessage) jmsMessage;
    String messageString = Utils.toString(message, false);
    UserTransaction transaction = context.getUserTransaction();
    ErrorEventData errorEventData = null;
    try {//from   w  ww . ja  v a 2s.  c  om
        log.debug("Received " + messageString);
        errorEventData = ErrorEventData.match(message);
        transaction.begin();
        List<Token> tokens;
        if (SystemProperties.isProcessExecutionMessagePredefinedSelectorEnabled()) {
            Map<String, String> routingData = getRoutingData(message);
            tokens = executionLogic.findTokensForMessageSelector(routingData);
            log.debug("Checking " + tokens.size() + " tokens by routingData = " + routingData);
        } else {
            tokens = tokenDao.findByNodeTypeAndExecutionStatusIsActive(NodeType.RECEIVE_MESSAGE);
            log.debug("Checking " + tokens.size() + " tokens");
        }
        for (Token token : tokens) {
            try {
                ProcessDefinition processDefinition = processDefinitionLoader
                        .getDefinition(token.getProcess().getDeployment().getId());
                BaseMessageNode receiveMessageNode = (BaseMessageNode) token.getNodeNotNull(processDefinition);
                ExecutionContext executionContext = new ExecutionContext(processDefinition, token);
                if (errorEventData != null) {
                    if (receiveMessageNode.getEventType() == MessageEventType.error
                            && receiveMessageNode.getParentElement() instanceof Node) {
                        Long processId = token.getProcess().getId();
                        String nodeId = ((Node) receiveMessageNode.getParentElement()).getNodeId();
                        if (processId.equals(errorEventData.processId)
                                && nodeId.equals(errorEventData.nodeId)) {
                            handlers.add(new ReceiveMessageData(executionContext, receiveMessageNode));
                            break;
                        }
                    }
                } else {
                    boolean suitable = true;
                    VariableProvider variableProvider = executionContext.getVariableProvider();
                    for (VariableMapping mapping : receiveMessageNode.getVariableMappings()) {
                        if (mapping.isPropertySelector()) {
                            String selectorValue = message.getStringProperty(mapping.getName());
                            String expectedValue = Utils.getMessageSelectorValue(variableProvider,
                                    receiveMessageNode, mapping);
                            if (!Objects.equal(expectedValue, selectorValue)) {
                                log.debug(message + " rejected in " + token + " due to diff in "
                                        + mapping.getName() + " (" + expectedValue + "!=" + selectorValue
                                        + ")");
                                suitable = false;
                                break;

                            }
                        }
                    }
                    if (suitable) {
                        handlers.add(new ReceiveMessageData(executionContext, receiveMessageNode));
                    }
                }
            } catch (Exception e) {
                log.error("Unable to handle " + token, e);
            }
        }
        transaction.commit();
    } catch (Exception e) {
        log.error("", e);
        Utils.rollbackTransaction(transaction);
        Throwables.propagate(e);
    }
    if (handlers.isEmpty()) {
        if (errorEventData != null) {
            String errorMessage = "Unexpected errorEvent in processId = " + errorEventData.processId
                    + ", nodeId = " + errorEventData.nodeId;
            log.error(errorMessage);
            Errors.addSystemError(new InternalApplicationException(errorMessage));
        } else {
            throw new MessagePostponedException(messageString);
        }
    }
    for (ReceiveMessageData data : handlers) {
        handleMessage(data, message);
    }
}