Example usage for org.springframework.transaction.support DefaultTransactionDefinition DefaultTransactionDefinition

List of usage examples for org.springframework.transaction.support DefaultTransactionDefinition DefaultTransactionDefinition

Introduction

In this page you can find the example usage for org.springframework.transaction.support DefaultTransactionDefinition DefaultTransactionDefinition.

Prototype

public DefaultTransactionDefinition() 

Source Link

Document

Create a new DefaultTransactionDefinition, with default settings.

Usage

From source file:edu.illinois.ncsa.springdata.Transaction.java

/**
 * Start a new transaction. This will use the readonly flag as a hint for
 * the transaction manager../* w  w  w  . j a v  a  2 s . com*/
 * 
 * @param readonly
 *            a hint for the transaction manager that the transaction should
 *            be used for readonly operations only.
 * @throws Exception
 *             throws an exception if there was a problem creating the
 *             transaction or another transaction is already in use.
 */
public void start(boolean readonly) throws Exception {
    if (status != null) {
        throw (new Exception("Already have transaction open."));
    }
    DefaultTransactionDefinition def = new DefaultTransactionDefinition();
    def.setReadOnly(readonly);
    status = transactionManager.getTransaction(def);

    Map<Transaction, Exception> localtrans = transactions.get(Thread.currentThread());
    if (localtrans == null) {
        localtrans = new HashMap<Transaction, Exception>();
        transactions.put(Thread.currentThread(), localtrans);
    } else {
        if (localtrans.size() > 0) {
            logger.info(getThreadTransaction());
        }
    }
    localtrans.put(this, new Exception());
}

From source file:com.krawler.spring.organizationChart.organizationChartController.java

public ModelAndView insertNode(HttpServletRequest request, HttpServletResponse response) {
    JSONObject jobj = new JSONObject();
    JSONObject jobj1 = new JSONObject();
    String retMsg = "";
    //Create transaction
    DefaultTransactionDefinition def = new DefaultTransactionDefinition();
    def.setName("JE_Tx");
    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
    def.setIsolationLevel(TransactionDefinition.ISOLATION_READ_UNCOMMITTED);
    TransactionStatus status = txnManager.getTransaction(def);
    try {/*from  w ww .  j  a  va2s . co m*/

        String parentid = StringUtil.checkForNull(request.getParameter("fromId"));
        String childid = StringUtil.checkForNull(request.getParameter("userid"));

        HashMap<String, Object> hm = organizationService.insertNode(parentid, childid);

        boolean success = Boolean.parseBoolean(hm.get("success").toString());
        User parent = (User) hm.get("parent");
        User child = (User) hm.get("child");

        if (parent != null && child != null) {
            auditTrailDAOObj.insertAuditLog(AuditAction.ORGANIZATION_CHART_NODE_ASSIGNED,
                    child.getFirstName() + " " + child.getLastName() + " re-assigned to "
                            + parent.getFirstName() + " " + parent.getLastName(),
                    request, "0");
        }

        if (success) {
            retMsg = "{success:true}";
        } else {
            retMsg = "{success:false,msg:\""
                    + messageSource.getMessage("hrms.common.not.assign.parent.node.employee.role", null,
                            "Could not assign because, parent node has Employee role.",
                            RequestContextUtils.getLocale(request))
                    + "\"}";
        }
        jobj.put("data", retMsg);

        jobj1.put("valid", true);
        jobj1.put("data", jobj);
        jobj1.put("success", true);
        txnManager.commit(status);
    } catch (Exception e) {
        logger.warn("General exception in insertNode()", e);
        txnManager.rollback(status);
    }
    return new ModelAndView("jsonView", "model", jobj1.toString());
}

From source file:com.krawler.spring.crm.caseModule.CrmCustomerCaseController.java

public ModelAndView addComment(HttpServletRequest request, HttpServletResponse response) {
    Map model = new HashMap();
    String responseMessage = "";
    DefaultTransactionDefinition def = new DefaultTransactionDefinition();
    def.setName("JE_Tx");
    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
    def.setIsolationLevel(TransactionDefinition.ISOLATION_READ_UNCOMMITTED);
    TransactionStatus status = txnManager.getTransaction(def);
    try {//from  ww w .  j  a  va  2  s  .  c o  m
        if ((String) request.getSession().getAttribute(Constants.SESSION_CUSTOMER_ID) != null) {
            String caseId = request.getParameter("caseid");
            String userId = (String) request.getSession().getAttribute("contactid");
            String comment = request.getParameter("addcomment");
            crmCustomerCaseService.addComment(caseId, userId, comment);
            txnManager.commit(status);
            request.setAttribute("casedetails", "true");
            request.setAttribute("caseid", caseId);
        } else {
            request.setAttribute("logout", "true");
        }
        responseMessage = "usercases/redirect";
    }

    catch (Exception e) {
        logger.warn(e.getMessage(), e);
        txnManager.rollback(status);
    }
    return new ModelAndView(responseMessage, "model", model);
}

From source file:es.upm.fiware.rss.expenditureLimit.dao.impl.tests.DbeExpendControlDaoTest.java

@Transactional(propagation = Propagation.SUPPORTS)
public void testUpdateExpendLimitDataForaUser() {

    BmCurrency bmCurrency = new BmCurrency();
    bmCurrency.setNuCurrencyId(1);//from ww w  .  ja  v a 2  s .  co  m
    List<DbeExpendControl> l = expLimitDao.getExpendDataForUserAppProvCurrency("userId01", "agg123",
            "app123456", bmCurrency);

    Assert.assertTrue("Elements founds", l != null && l.size() == 3);
    Iterator<DbeExpendControl> it = l.iterator();

    DefaultTransactionDefinition def = new DefaultTransactionDefinition();
    def.setPropagationBehavior(Propagation.REQUIRES_NEW.value());
    TransactionStatus status = transactionManager.getTransaction(def);

    while (it.hasNext()) {
        DbeExpendControl el = it.next();
        if (el.getId().getTxElType().equalsIgnoreCase("daily")) {
            el.setFtExpensedAmount(new BigDecimal("0"));
            Date dt = new Date(new Date().getTime() + 23 * 36000 * 1000);
            el.setDtNextPeriodStart(dt);
            el.setTxNotifications("");
        } else {
            el.setFtExpensedAmount(el.getFtExpensedAmount().add(new BigDecimal("22")));
            el.setTxNotifications(el.getTxNotifications() + ", 50");
        }
        expLimitDao.saveDbeExpendControl(el);
    }

    transactionManager.commit(status);

    l = expLimitDao.getExpendDataForUserAppProvCurrency("userId01", "agg123", "app123456", bmCurrency);

    it = l.iterator();
    while (it.hasNext()) {
        DbeExpendControl el = it.next();
        if (el.getId().getTxElType().equalsIgnoreCase("daily")) {
            Assert.assertTrue("Daily accumulate: ", el.getFtExpensedAmount().floatValue() == 0);
            Assert.assertTrue("Notifications: ",
                    el.getTxNotifications() == null || el.getTxNotifications().length() == 0);
        } else {
            Assert.assertTrue("Notifications: ", el.getTxNotifications().contains("50"));
        }
    }
}

From source file:es.tid.fiware.rss.dao.impl.test.DbeSystemPropertiesDaoImplTest.java

@Test
public void testfDelete() {
    DbeSystemProperties c = dbeSystemPropertiesDao.getById("name");
    DefaultTransactionDefinition def = new DefaultTransactionDefinition();
    def.setPropagationBehavior(Propagation.REQUIRES_NEW.value());
    TransactionStatus status = transactionManager.getTransaction(def);
    dbeSystemPropertiesDao.delete(c);/*  w  ww  . ja v a2  s  .c  o  m*/
    transactionManager.commit(status);
    Assert.assertTrue(dbeSystemPropertiesDao.getById("name") == null);
}

From source file:org.ohmage.query.impl.AnnotationQueries.java

@Override
public void createSurveyResponseAnnotation(final UUID annotationId, final String client, final Long time,
        final DateTimeZone timezone, final String annotationText, final UUID surveyId)
        throws DataAccessException {

    // Create the transaction.
    DefaultTransactionDefinition def = new DefaultTransactionDefinition();
    def.setName("Creating a new survey response annotation.");

    try {//from w w  w.ja v a 2s.co m
        // Begin the transaction.
        PlatformTransactionManager transactionManager = new DataSourceTransactionManager(getDataSource());
        TransactionStatus status = transactionManager.getTransaction(def);
        long id = 0;

        try {
            id = insertAnnotation(annotationId, time, timezone, client, annotationText);
        } catch (org.springframework.dao.DataAccessException e) {

            transactionManager.rollback(status);
            throw new DataAccessException("Error while executing SQL '" + SQL_INSERT_ANNOTATION
                    + "' with parameters: " + annotationId + ", " + time + ", " + timezone.getID() + ", "
                    + client + ", " + ((annotationText.length() > 25) ? annotationText.substring(0, 25) + "..."
                            : annotationText),
                    e);
        }

        try {
            // Insert the link between the survey_response and its annotation
            getJdbcTemplate().update(SQL_INSERT_SURVEY_RESPONSE_ANNOTATION, surveyId.toString(), id);

        } catch (org.springframework.dao.DataAccessException e) {

            transactionManager.rollback(status);
            throw new DataAccessException("Error while executing SQL '" + SQL_INSERT_SURVEY_RESPONSE_ANNOTATION
                    + "' with parameters: " + surveyId.toString() + ", " + id, e);
        }

        // Commit the transaction.
        try {
            transactionManager.commit(status);
        } catch (TransactionException e) {
            transactionManager.rollback(status);
            throw new DataAccessException("Error while committing the transaction.", e);
        }

    } catch (TransactionException e) {
        throw new DataAccessException("Error while attempting to rollback the transaction.", e);
    }
}

From source file:com.krawler.spring.hrms.common.hrmsDocumentController.java

public ModelAndView addDocuments(HttpServletRequest request, HttpServletResponse response)
        throws ServletException {
    JSONObject jobj = new JSONObject();
    JSONObject myjobj = new JSONObject();
    List fileItems = null;//from ww w  . j  a v a 2s.co m
    KwlReturnObject kmsg = null;
    String auditAction = "";
    boolean applicant = false;
    String id = java.util.UUID.randomUUID().toString();
    PrintWriter out = null;
    //Create transaction
    DefaultTransactionDefinition def = new DefaultTransactionDefinition();
    def.setName("JE_Tx");
    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
    def.setIsolationLevel(TransactionDefinition.ISOLATION_READ_UNCOMMITTED);
    TransactionStatus status = txnManager.getTransaction(def);
    try {
        response.setContentType("text/html;charset=UTF-8");
        out = response.getWriter();
        String userid = sessionHandlerImplObj.getUserid(request);
        String map = request.getParameter("mapid");
        HashMap<String, String> arrParam = new HashMap<String, String>();
        boolean fileUpload = false;
        String docdesc;
        ArrayList<FileItem> fi = new ArrayList<FileItem>();
        if (request.getParameter("fileAdd") != null) {
            DiskFileUpload fu = new DiskFileUpload();
            fileItems = fu.parseRequest(request);
            documentDAOObj.parseRequest(fileItems, arrParam, fi, fileUpload);
            arrParam.put("IsIE", request.getParameter("IsIE"));
            if (arrParam.get("applicantid").equalsIgnoreCase("applicant")) {
                applicant = true;
                userid = arrParam.get("refid");
            }
            if (StringUtil.isNullOrEmpty((String) arrParam.get("docdesc")) == false) {
                docdesc = (String) arrParam.get("docdesc");
            }
        }
        for (int cnt = 0; cnt < fi.size(); cnt++) {
            String docID;
            if (applicant) {
                kmsg = hrmsExtApplDocsDAOObj.uploadFile(fi.get(cnt), userid, arrParam,
                        profileHandlerDAOObj.getUserFullName(sessionHandlerImplObj.getUserid(request)));
                HrmsDocs doc = (HrmsDocs) kmsg.getEntityList().get(0);
                docID = doc.getDocid();
            } else {
                kmsg = documentDAOObj.uploadFile(fi.get(cnt), userid, arrParam);
                Docs doc = (Docs) kmsg.getEntityList().get(0);
                docID = doc.getDocid();
            }

            String companyID = sessionHandlerImplObj.getCompanyid(request);
            String userID = sessionHandlerImplObj.getUserid(request);
            String refid = arrParam.get("refid");
            jobj.put("userid", userID);
            jobj.put("docid", docID);
            jobj.put("companyid", companyID);
            jobj.put("id", id);
            jobj.put("map", map);
            jobj.put("refid", refid);
            if (arrParam.get("applicantid").equalsIgnoreCase("applicant")) {
                hrmsExtApplDocsDAOObj.saveDocumentMapping(jobj);
            } else {
                documentDAOObj.saveDocumentMapping(jobj);
            }
        }
        myjobj.put("ID", id);
        txnManager.commit(status);
    } catch (Exception e) {
        System.out.println(e.getMessage());
        txnManager.rollback(status);
    } finally {
        out.close();
    }
    return new ModelAndView("jsonView", "model", myjobj.toString());
}

From source file:es.upm.fiware.rss.dao.impl.test.DbeSystemPropertiesDaoImplTest.java

@Transactional(propagation = Propagation.SUPPORTS)
public void testgDeleteById() {
    DefaultTransactionDefinition def = new DefaultTransactionDefinition();
    def.setPropagationBehavior(Propagation.REQUIRES_NEW.value());
    TransactionStatus status = transactionManager.getTransaction(def);
    dbeSystemPropertiesDao.deleteById("name2");
    transactionManager.commit(status);/* www . j a v a2  s .c  o  m*/
    Assert.assertTrue(dbeSystemPropertiesDao.getById("name2") == null);
}

From source file:es.tid.fiware.rss.expenditureLimit.dao.impl.tests.DbeExpendControlDaoTest.java

@Test
public void testUpdateExpendLimitDataForaUser() {

    BmService bmService = new BmService();
    bmService.setNuServiceId(1);/*w w w .j  a  v a 2  s .co m*/
    BmCurrency bmCurrency = new BmCurrency();
    bmCurrency.setNuCurrencyId(1);
    BmObCountry bmObCountry = new BmObCountry();
    bmObCountry.setId(new BmObCountryId(1, 1));
    List<DbeExpendControl> l = expLimitDao.getExpendDataForUserAppProvCurrencyObCountry("userId01", bmService,
            "app123456", bmCurrency, bmObCountry);

    Assert.assertTrue("Elements founds", l != null && l.size() == 3);
    Iterator<DbeExpendControl> it = l.iterator();
    DefaultTransactionDefinition def = new DefaultTransactionDefinition();
    def.setPropagationBehavior(Propagation.REQUIRES_NEW.value());
    TransactionStatus status = transactionManager.getTransaction(def);
    while (it.hasNext()) {
        DbeExpendControl el = it.next();
        if (el.getId().getTxElType().equalsIgnoreCase("daily")) {
            el.setFtExpensedAmount(new BigDecimal("0"));
            Date dt = new Date(new Date().getTime() + 23 * 36000 * 1000);
            el.setDtNextPeriodStart(dt);
            el.setTxNotifications("");
        } else {
            el.setFtExpensedAmount(el.getFtExpensedAmount().add(new BigDecimal("22")));
            el.setTxNotifications(el.getTxNotifications() + ", 50");
        }
        expLimitDao.saveDbeExpendControl(el);
    }
    transactionManager.commit(status);

    l = expLimitDao.getExpendDataForUserAppProvCurrencyObCountry("userId01", bmService, "app123456", bmCurrency,
            bmObCountry);

    it = l.iterator();
    while (it.hasNext()) {
        DbeExpendControl el = it.next();
        if (el.getId().getTxElType().equalsIgnoreCase("daily")) {
            Assert.assertTrue("Daily accumulate: ", el.getFtExpensedAmount().floatValue() == 0);
            Assert.assertTrue("Notifications: ",
                    el.getTxNotifications() == null || el.getTxNotifications().length() == 0);
        } else {
            Assert.assertTrue("Notifications: ", el.getTxNotifications().indexOf("50") > -1);
        }
    }
}

From source file:es.tid.fiware.rss.dao.impl.test.DbeSystemPropertiesDaoImplTest.java

@Test
@Transactional(propagation = Propagation.SUPPORTS)
public void testgDeleteById() {
    DefaultTransactionDefinition def = new DefaultTransactionDefinition();
    def.setPropagationBehavior(Propagation.REQUIRES_NEW.value());
    TransactionStatus status = transactionManager.getTransaction(def);
    dbeSystemPropertiesDao.deleteById("name2");
    transactionManager.commit(status);/*from  w  w  w  .  j  a va 2s  . c o  m*/
    Assert.assertTrue(dbeSystemPropertiesDao.getById("name2") == null);
}