Example usage for javax.persistence EntityTransaction begin

List of usage examples for javax.persistence EntityTransaction begin

Introduction

In this page you can find the example usage for javax.persistence EntityTransaction begin.

Prototype

public void begin();

Source Link

Document

Start a resource transaction.

Usage

From source file:com.sixsq.slipstream.run.RunNodeResource.java

private Representation addNodeInstancesInTransaction(Representation entity) throws Exception {

    EntityManager em = PersistenceUtil.createEntityManager();
    EntityTransaction transaction = em.getTransaction();
    Run run = Run.loadFromUuid(getUuid(), em);
    List<String> instanceNames = new ArrayList<String>();
    try {/* w ww .j  a v  a  2s .  com*/
        validateRun(run);

        transaction.begin();

        int noOfInst = getNumberOfInstancesToAdd(entity);

        Node node = getNode(run, nodename);
        for (int i = 0; i < noOfInst; i++) {
            instanceNames.add(createNodeInstanceOnRun(run, node));
        }

        run.postEventScaleUp(nodename, instanceNames, noOfInst);

        incrementNodeMultiplicityOnRun(noOfInst, run);
        StateMachine.createStateMachine(run).tryAdvanceToProvisionning();

        if (Configuration.isQuotaEnabled()) {
            User user = User.loadByName(run.getUser());
            Quota.validate(user, run.getCloudServiceUsage(), Vm.usage(user.getName()));
        }

        transaction.commit();
    } catch (Exception ex) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        throw ex;
    } finally {
        em.close();
    }

    getResponse().setStatus(Status.SUCCESS_CREATED);
    return new StringRepresentation(StringUtils.join(instanceNames, ","), MediaType.TEXT_PLAIN);
}

From source file:org.apache.juddi.v3.auth.jboss.JBossAuthenticator.java

/**
  */*from   w  ww  . j a  va 2 s .  c  om*/
  */
public String authenticate(final String userID, final String credential) throws AuthenticationException {
    if (userID == null) {
        throw new UnknownUserException(new ErrorMessage("errors.auth.InvalidUserId", userID));
    }

    EntityManager em = PersistenceManager.getEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {
        // Create a principal for the userID
        Principal principal = new Principal() {
            public String getName() {
                return userID;
            }
        };

        if (!authManager.isValid(principal, credential)) {
            throw new UnknownUserException(new ErrorMessage("errors.auth.InvalidCredentials"));
        } else {
            tx.begin();
            Publisher publisher = em.find(Publisher.class, userID);
            if (publisher == null) {
                publisher = new Publisher();
                publisher.setAuthorizedName(userID);
                publisher.setIsAdmin("false");
                publisher.setIsEnabled("true");
                publisher.setMaxBindingsPerService(199);
                publisher.setMaxBusinesses(100);
                publisher.setMaxServicesPerBusiness(100);
                publisher.setMaxTmodels(100);
                publisher.setPublisherName("Unknown");
                em.persist(publisher);
                tx.commit();
            }
        }
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
    return userID;
}

From source file:org.apache.juddi.api.impl.UDDIInquiryImpl.java

public BusinessList findBusiness(FindBusiness body) throws DispositionReportFaultMessage {
    long startTime = System.currentTimeMillis();
    try {/*from   w ww.j  av a2 s .  c  o m*/
        new ValidateInquiry(null).validateFindBusiness(body);
    } catch (DispositionReportFaultMessage drfm) {
        long procTime = System.currentTimeMillis() - startTime;
        serviceCounter.update(InquiryQuery.FIND_BUSINESS, QueryStatus.FAILED, procTime);
        throw drfm;
    }

    EntityManager em = PersistenceManager.getEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();

        if (isAuthenticated())
            this.getEntityPublisher(em, body.getAuthInfo());

        org.apache.juddi.query.util.FindQualifiers findQualifiers = new org.apache.juddi.query.util.FindQualifiers();
        findQualifiers.mapApiFindQualifiers(body.getFindQualifiers());

        List<?> keysFound = InquiryHelper.findBusiness(body, findQualifiers, em);

        BusinessList result = InquiryHelper.getBusinessListFromKeys(body, findQualifiers, em, keysFound);

        tx.rollback();
        long procTime = System.currentTimeMillis() - startTime;
        serviceCounter.update(InquiryQuery.FIND_BUSINESS, QueryStatus.SUCCESS, procTime);

        return result;
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
}

From source file:org.apache.juddi.api.impl.UDDIInquiryImpl.java

public TModelList findTModel(FindTModel body) throws DispositionReportFaultMessage {
    long startTime = System.currentTimeMillis();
    try {//from  w  w  w  .jav a 2s. c o  m
        new ValidateInquiry(null).validateFindTModel(body, false);
    } catch (DispositionReportFaultMessage drfm) {
        long procTime = System.currentTimeMillis() - startTime;
        serviceCounter.update(InquiryQuery.FIND_TMODEL, QueryStatus.FAILED, procTime);
        throw drfm;
    }

    EntityManager em = PersistenceManager.getEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();

        if (isAuthenticated())
            this.getEntityPublisher(em, body.getAuthInfo());

        org.apache.juddi.query.util.FindQualifiers findQualifiers = new org.apache.juddi.query.util.FindQualifiers();
        findQualifiers.mapApiFindQualifiers(body.getFindQualifiers());

        List<?> keysFound = InquiryHelper.findTModel(body, findQualifiers, em);

        TModelList result = InquiryHelper.getTModelListFromKeys(body, findQualifiers, em, keysFound);

        tx.rollback();
        long procTime = System.currentTimeMillis() - startTime;
        serviceCounter.update(InquiryQuery.FIND_TMODEL, QueryStatus.SUCCESS, procTime);

        return result;
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
}

From source file:org.apache.juddi.api.impl.UDDIInquiryImpl.java

public RelatedBusinessesList findRelatedBusinesses(FindRelatedBusinesses body)
        throws DispositionReportFaultMessage {
    long startTime = System.currentTimeMillis();
    try {//from  ww w  . j  a  v a2s.  c  o m
        new ValidateInquiry(null).validateFindRelatedBusinesses(body, false);
    } catch (DispositionReportFaultMessage drfm) {
        long procTime = System.currentTimeMillis() - startTime;
        serviceCounter.update(InquiryQuery.FIND_RELATEDBUSINESSES, QueryStatus.FAILED, procTime);
        throw drfm;
    }

    EntityManager em = PersistenceManager.getEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();

        if (isAuthenticated())
            this.getEntityPublisher(em, body.getAuthInfo());

        // TODO: findQualifiers aren't really used for this call, except maybe for sorting.  Sorting must be done in Java due to the retrieval method used.  Right now
        // no sorting is performed.
        org.apache.juddi.query.util.FindQualifiers findQualifiers = new org.apache.juddi.query.util.FindQualifiers();
        findQualifiers.mapApiFindQualifiers(body.getFindQualifiers());

        RelatedBusinessesList result = InquiryHelper.getRelatedBusinessesList(body, em);

        tx.rollback();
        long procTime = System.currentTimeMillis() - startTime;
        serviceCounter.update(InquiryQuery.FIND_RELATEDBUSINESSES, QueryStatus.SUCCESS, procTime);

        return result;
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
}

From source file:eu.forgestore.ws.impl.FStoreJpaController.java

public void deleteProduct(int id) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    Product p = entityManager.find(Product.class, id);

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();
    entityManager.remove(p);/*from www .  j a va 2  s  . com*/
    entityTransaction.commit();
}

From source file:eu.forgestore.ws.impl.FStoreJpaController.java

public void deleteCategory(int catid) {

    EntityManager entityManager = entityManagerFactory.createEntityManager();
    Category c = entityManager.find(Category.class, catid);

    EntityTransaction entityTransaction = entityManager.getTransaction();
    entityTransaction.begin();
    entityManager.remove(c);//from  ww w  .j ava 2 s  .c  om
    entityTransaction.commit();

}

From source file:eu.forgestore.ws.impl.FStoreJpaController.java

public void deleteProperty(int propid) {

    EntityManager entityManager = entityManagerFactory.createEntityManager();
    FStoreProperty c = entityManager.find(FStoreProperty.class, propid);

    EntityTransaction entityTransaction = entityManager.getTransaction();
    entityTransaction.begin();
    entityManager.remove(c);//w w w. jav  a  2  s  . c o  m
    entityTransaction.commit();

}

From source file:org.apache.juddi.api.impl.UDDIInquiryImpl.java

public BindingDetail getBindingDetail(GetBindingDetail body) throws DispositionReportFaultMessage {
    long startTime = System.currentTimeMillis();
    try {//from  w w  w. j  a v  a  2s.  c o m
        new ValidateInquiry(null).validateGetBindingDetail(body);
    } catch (DispositionReportFaultMessage drfm) {
        long procTime = System.currentTimeMillis() - startTime;
        serviceCounter.update(InquiryQuery.FIND_TMODEL, QueryStatus.FAILED, procTime);
        throw drfm;
    }

    EntityManager em = PersistenceManager.getEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();

        if (isAuthenticated())
            this.getEntityPublisher(em, body.getAuthInfo());

        BindingDetail result = new BindingDetail();

        List<String> bindingKeyList = body.getBindingKey();
        for (String bindingKey : bindingKeyList) {
            org.apache.juddi.model.BindingTemplate modelBindingTemplate = null;
            try {
                modelBindingTemplate = em.find(org.apache.juddi.model.BindingTemplate.class, bindingKey);
            } catch (ClassCastException e) {
            }
            if (modelBindingTemplate == null)
                throw new InvalidKeyPassedException(
                        new ErrorMessage("errors.invalidkey.BindingTemplateNotFound", bindingKey));

            org.uddi.api_v3.BindingTemplate apiBindingTemplate = new org.uddi.api_v3.BindingTemplate();

            MappingModelToApi.mapBindingTemplate(modelBindingTemplate, apiBindingTemplate);

            result.getBindingTemplate().add(apiBindingTemplate);
        }

        tx.commit();
        long procTime = System.currentTimeMillis() - startTime;
        serviceCounter.update(InquiryQuery.GET_BINDINGDETAIL, QueryStatus.SUCCESS, procTime);

        return result;
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
}

From source file:org.apache.juddi.api.impl.UDDIInquiryImpl.java

public BusinessDetail getBusinessDetail(GetBusinessDetail body) throws DispositionReportFaultMessage {
    long startTime = System.currentTimeMillis();
    try {/*from w w  w.  j a v a2s .co m*/
        new ValidateInquiry(null).validateGetBusinessDetail(body);
    } catch (DispositionReportFaultMessage drfm) {
        long procTime = System.currentTimeMillis() - startTime;
        serviceCounter.update(InquiryQuery.GET_BUSINESSDETAIL, QueryStatus.FAILED, procTime);
        throw drfm;
    }

    EntityManager em = PersistenceManager.getEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();

        if (isAuthenticated())
            this.getEntityPublisher(em, body.getAuthInfo());

        BusinessDetail result = new BusinessDetail();

        List<String> businessKeyList = body.getBusinessKey();
        for (String businessKey : businessKeyList) {
            org.apache.juddi.model.BusinessEntity modelBusinessEntity = null;
            try {
                modelBusinessEntity = em.find(org.apache.juddi.model.BusinessEntity.class, businessKey);
            } catch (ClassCastException e) {
            }
            if (modelBusinessEntity == null)
                throw new InvalidKeyPassedException(
                        new ErrorMessage("errors.invalidkey.BusinessNotFound", businessKey));

            org.uddi.api_v3.BusinessEntity apiBusinessEntity = new org.uddi.api_v3.BusinessEntity();

            MappingModelToApi.mapBusinessEntity(modelBusinessEntity, apiBusinessEntity);

            result.getBusinessEntity().add(apiBusinessEntity);
        }

        tx.commit();
        long procTime = System.currentTimeMillis() - startTime;
        serviceCounter.update(InquiryQuery.GET_BUSINESSDETAIL, QueryStatus.SUCCESS, procTime);

        return result;
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
}