Example usage for org.hibernate.resource.transaction.spi TransactionStatus ACTIVE

List of usage examples for org.hibernate.resource.transaction.spi TransactionStatus ACTIVE

Introduction

In this page you can find the example usage for org.hibernate.resource.transaction.spi TransactionStatus ACTIVE.

Prototype

TransactionStatus ACTIVE

To view the source code for org.hibernate.resource.transaction.spi TransactionStatus ACTIVE.

Click Source Link

Document

The transaction has been started, but not yet completed.

Usage

From source file:at.treedb.db.hibernate.DAOhibernate.java

License:Open Source License

@Override
public void endTransaction() {
    if (tx != null && tx.getStatus() == TransactionStatus.ACTIVE) {
        tx.commit();
    }
}

From source file:at.treedb.db.hibernate.DAOhibernate.java

License:Open Source License

@Override
public void rollback() {
    if (tx != null && tx.getStatus() == TransactionStatus.ACTIVE) {
        tx.rollback();
    }
}

From source file:com.quakearts.webapp.hibernate.CurrentSessionContextHelper.java

License:Open Source License

public static void closeOpenSessions() {

    ArrayList<Exception> exceptions = new ArrayList<>();

    for (CurrentSessionContextHelper helper : sessionHelperCache.values()) {
        Session session = helper.getCurrentSessionFromContextAttributes();

        if (session == null)
            return;

        helper.removeCurrentSessionFromContextAttributes();

        Transaction tx = session.getTransaction();

        if (tx == null)
            throw new IllegalStateException("Transaction cannot be found.");

        try {//ww w . j  av  a 2 s.c  o m
            if (tx.getStatus() == TransactionStatus.ACTIVE) {
                tx.commit();
            } else if (tx.getStatus() == TransactionStatus.MARKED_ROLLBACK) {
                tx.rollback();
            } else {
                throw new IllegalStateException("Transaction is not in an epected state");
            }
        } catch (HibernateException e) {
            exceptions.add(new Exception("Exception thrown whiles cleaning up domain: " + helper.domain, e));
        } finally {
            try {//Reclose just in case
                session.close();
            } catch (SessionException e) {
                //Ignore
            } catch (Exception e) {
                exceptions
                        .add(new Exception("Exception thrown whiles cleaning up domain: " + helper.domain, e));
            }
        }
    }

    if (exceptions.size() > 0) {
        for (Exception e : exceptions) {
            log.log(Level.SEVERE, e.getMessage(), e);
        }

        throw new IllegalStateException("Error during session cleanup");
    }
}

From source file:com.tremolosecurity.provisioning.core.WorkflowImpl.java

License:Apache License

@Override
public Map<String, Object> executeWorkflow(User user, Map<String, Object> params, String requesterUID)
        throws ProvisioningException {

    request = new HashMap<String, Object>();

    if (params != null) {
        request.putAll(params);/*from w ww  .  j a  v a  2 s  .  c  o  m*/
        //request = (HashMap<String, Object>) params;
    }

    this.user = user;

    Session session = null;

    try {

        if (this.cfgMgr.getProvisioningEngine().getHibernateSessionFactory() != null) {
            session = this.cfgMgr.getProvisioningEngine().getHibernateSessionFactory().openSession();
            this.userNum = getUserNum(user, session, this.cfgMgr);

            if (requesterUID == null) {
                this.requester = this.user;
                this.requesterNum = this.userNum;
            } else {
                this.requester = getRequester(requesterUID, session, cfgMgr);
                this.requesterNum = getUserNum(requester, session, this.cfgMgr);
            }

            Workflows workflow = session.load(Workflows.class, this.getId());
            workflow.setUsers(session.load(Users.class, this.userNum));
            workflow.setRequester(session.load(Users.class, this.requesterNum));
            workflow.setRequestReason(user.getRequestReason());

            ST st = new ST(this.description, '$', '$');
            for (String key : request.keySet()) {
                st.add(key.replaceAll("[.]", "_"), request.get(key));
            }

            workflow.setDescription(st.render());

            if (this.label != null) {
                st = new ST(this.label, '$', '$');
                for (String key : request.keySet()) {
                    st.add(key.replaceAll("[.]", "_"), request.get(key));
                }

                workflow.setLabel(st.render());

            } else {
                workflow.setLabel("");
            }

            session.beginTransaction();

            session.save(workflow);

            for (String paramName : params.keySet()) {
                WorkflowParameters param = new WorkflowParameters();
                param.setName(paramName);
                Object o = params.get(paramName);
                if (o != null) {
                    param.setValue(o.toString());
                } else {
                    param.setValue("");
                }
                param.setWorkflows(workflow);
                session.save(param);
            }

            session.getTransaction().commit();

        }
    } catch (SQLException e) {
        throw new ProvisioningException("Could not set workflow user key", e);
    } catch (LDAPException e) {
        throw new ProvisioningException("Could not set workflow user key", e);
    } finally {
        if (session != null) {

            if (session.getTransaction() != null
                    && session.getTransaction().getStatus() == TransactionStatus.ACTIVE) {
                session.getTransaction().rollback();
            }

            session.close();
        }
    }

    if (request.get(ProvisioningParams.UNISON_EXEC_TYPE) != null
            && request.get(ProvisioningParams.UNISON_EXEC_TYPE).equals(ProvisioningParams.UNISON_EXEC_SYNC)) {
        Iterator<WorkflowTask> it = this.tasks.iterator();
        while (it.hasNext()) {

            boolean doContinue = it.next().doTask(user, request);
            if (!doContinue) {
                return request;
            }
        }

        this.completeWorkflow();
    } else {
        WorkflowHolder wfHolder = new WorkflowHolder(this, user, request);
        TaskHolder root = new TaskHolder();
        root.setParent(this.tasks);
        root.setPosition(0);
        wfHolder.getWfStack().push(root);

        ((ProvisioningEngineImpl) this.cfgMgr.getProvisioningEngine()).enqueue(wfHolder);
    }

    return request;

}

From source file:com.tremolosecurity.provisioning.core.WorkflowImpl.java

License:Apache License

@Override
public void completeWorkflow() throws ProvisioningException {
    Session session = null;//from   w  ww  . j a  va 2s  . c o  m
    try {

        if (this.cfgMgr.getProvisioningEngine().getHibernateSessionFactory() != null) {
            session = this.cfgMgr.getProvisioningEngine().getHibernateSessionFactory().openSession();
            session.beginTransaction();
            DateTime now = new DateTime();
            Workflows wf = session.load(Workflows.class, this.id);
            wf.setCompleteTs(new Timestamp(now.getMillis()));
            session.save(wf);
            session.getTransaction().commit();

        }
    } finally {
        if (session != null) {

            if (session.getTransaction() != null
                    && session.getTransaction().getStatus() == TransactionStatus.ACTIVE) {
                session.getTransaction().rollback();
            }

            session.close();
        }
    }

}

From source file:com.tremolosecurity.provisioning.tasks.Approval.java

License:Apache License

@Override
public boolean doTask(User user, Map<String, Object> request) throws ProvisioningException {
    if (this.isOnHold()) {
        return runChildTasks(user, request);

    } else {/*  w ww . j av a2  s.  c o m*/
        Session session = this.getConfigManager().getProvisioningEngine().getHibernateSessionFactory()
                .openSession();
        try {
            session.beginTransaction();
            DateTime now = new DateTime();

            Approvals approval = new Approvals();
            approval.setLabel(this.renderTemplate(this.label, request));
            approval.setWorkflow(this.getWorkflow().getFromDB(session));
            approval.setCreateTs(new Timestamp(now.getMillis()));

            session.save(approval);

            this.id = approval.getId();

            //request.put("APPROVAL_ID", Integer.toString(this.id));
            request.put("APPROVAL_ID", this.id);

            if (request.get(Approval.APPROVAL_RESULT) != null) {
                request.remove(Approval.APPROVAL_RESULT);
            }

            this.setOnHold(true);

            Gson gson = new Gson();
            String json = "";
            synchronized (this.getWorkflow()) {
                json = JsonWriter.objectToJson(this.getWorkflow());
            }

            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, this.getConfigManager().getSecretKey(
                    this.getConfigManager().getCfg().getProvisioning().getApprovalDB().getEncryptionKey()));

            byte[] encJson = cipher.doFinal(json.getBytes("UTF-8"));
            String base64d = new String(org.bouncycastle.util.encoders.Base64.encode(encJson));

            Token token = new Token();
            token.setEncryptedRequest(base64d);
            token.setIv(new String(org.bouncycastle.util.encoders.Base64.encode(cipher.getIV())));

            //String base64 = new String(org.bouncycastle.util.encoders.Base64.encode(baos.toByteArray()));

            approval.setWorkflowObj(gson.toJson(token));

            session.save(approval);

            boolean sendNotification = true;
            if (request.containsKey(Approval.SEND_NOTIFICATION)
                    && request.get(Approval.SEND_NOTIFICATION).equals("false")) {
                sendNotification = false;
            }

            String localTemplate = this.renderTemplate(this.emailTemplate, request);

            for (Approver approver : this.approvers) {
                String[] localParams = null;
                localParams = renderCustomParameters(request, approver, localParams);

                String constraintRendered = this.renderTemplate(approver.constraint, request);
                switch (approver.type) {
                case StaticGroup:
                    AzUtils.loadStaticGroupApprovers(approval, localTemplate, this.getConfigManager(), session,
                            id, constraintRendered, sendNotification);
                    break;
                case Filter:
                    AzUtils.loadFilterApprovers(approval, localTemplate, this.getConfigManager(), session, id,
                            constraintRendered, sendNotification);
                    break;
                case DN:
                    AzUtils.loadDNApprovers(approval, localTemplate, this.getConfigManager(), session, id,
                            constraintRendered, sendNotification);
                    break;
                case Custom:
                    AzUtils.loadCustomApprovers(approval, localTemplate, this.getConfigManager(), session, id,
                            constraintRendered, sendNotification, approver.customAz, localParams);
                    break;
                }
            }

            session.getTransaction().commit();

            if (request.get(Approval.IMMEDIATE_ACTION) != null && request.get(Approval.REASON) != null) {
                String reason = (String) request.get(Approval.REASON);
                boolean action = false;
                Object tmp = request.get(Approval.IMMEDIATE_ACTION);
                if (tmp instanceof String) {
                    action = tmp.equals("true");
                } else {
                    action = (boolean) tmp;
                }

                try {
                    GlobalEntries.getGlobalEntries().getConfigManager().getProvisioningEngine()
                            .doApproval(this.id, this.getWorkflow().getRequester().getUserID(), action, reason);
                } catch (ProvisioningException pe) {
                    logger.warn("Could not execute pre-approval", pe);
                }
            }

            return false;

        } catch (IOException e) {
            throw new ProvisioningException("Could not store approval", e);
        } catch (NoSuchAlgorithmException e) {
            throw new ProvisioningException("Could not encrypt workflow object", e);
        } catch (NoSuchPaddingException e) {
            throw new ProvisioningException("Could not encrypt workflow object", e);
        } catch (InvalidKeyException e) {
            throw new ProvisioningException("Could not encrypt workflow object", e);
        } catch (IllegalBlockSizeException e) {
            throw new ProvisioningException("Could not encrypt workflow object", e);
        } catch (BadPaddingException e) {
            throw new ProvisioningException("Could not encrypt workflow object", e);
        } finally {
            if (session != null) {
                if (session.getTransaction() != null
                        && session.getTransaction().getStatus() == TransactionStatus.ACTIVE) {
                    session.getTransaction().rollback();
                }

                session.close();
            }
        }
    }
}

From source file:com.yahoo.elide.datastores.hibernate5.HibernateTransaction.java

License:Apache License

@Override
public void close() throws IOException {
    if (session.isOpen() && session.getTransaction().getStatus() == TransactionStatus.ACTIVE) {
        session.getTransaction().rollback();
        throw new IOException("Transaction not closed");
    }/*from w w  w .j a va 2 s .  com*/
}

From source file:io.dropwizard.sharding.utils.TransactionHandler.java

License:Apache License

private void rollbackTransaction() {
    final Transaction txn = session.getTransaction();
    if (txn != null && txn.getStatus() == TransactionStatus.ACTIVE) {
        txn.rollback();/*from w  w w. j av  a  2s.co m*/
    }
}

From source file:io.dropwizard.sharding.utils.TransactionHandler.java

License:Apache License

private void commitTransaction() {
    final Transaction txn = session.getTransaction();
    if (txn != null && txn.getStatus() == TransactionStatus.ACTIVE) {
        txn.commit();//  w  w  w.j a  v  a  2s .  c o  m
    }
}

From source file:org.apache.ignite.cache.store.hibernate.CacheHibernateStoreSessionListener.java

License:Apache License

/** {@inheritDoc} */
@Override/*w w w  . j  a  va 2  s .  c o  m*/
public void onSessionEnd(CacheStoreSession ses, boolean commit) {
    Session hibSes = ses.attach(null);

    if (hibSes != null) {
        try {
            Transaction tx = hibSes.getTransaction();

            if (commit) {
                if (hibSes.isDirty())
                    hibSes.flush();

                if (tx.getStatus() == TransactionStatus.ACTIVE)
                    tx.commit();
            } else if (tx.getStatus().canRollback())
                tx.rollback();
        } catch (HibernateException e) {
            throw new CacheWriterException("Failed to end store session [tx=" + ses.transaction() + ']', e);
        } finally {
            hibSes.close();
        }
    }
}