Example usage for org.apache.ibatis.session SqlSession commit

List of usage examples for org.apache.ibatis.session SqlSession commit

Introduction

In this page you can find the example usage for org.apache.ibatis.session SqlSession commit.

Prototype

void commit();

Source Link

Document

Flushes batch statements and commits database connection.

Usage

From source file:jp.escofi.emr.transformer.writer.XmlWriterCallerTestE001_001.java

/**
  * f?[^f??B//from   w w  w.  j a v a2 s .  co m
  *
  * @param dataModel f?[^f
  * @param attGroup ?O?[v
  * @param attElem ??
  * @param attField ??
  * @param attValueList ?lXg
  * @param keyList L?[?lXg
  * @throws IOException
  */
 private void insertDatamodel(TblDataModel dataModel, TblAttributeGroup attGroup, TblAttributeElem attElem,
         TblAttributeField attField, List<TblAttributeValue> attValueList, List<TblKeySolve> keyList)
         throws IOException {
     SqlSession session = null;

     try {
         session = getSqlSession();

         TblMapper dbMapper = session.getMapper(TblMapper.class);

         dbMapper.deleteAllDatamodel();
         dbMapper.deleteAllAttGroup();
         dbMapper.deleteAllKeySolve();
         dbMapper.deleteAllAttElem();
         dbMapper.deleteAllAttField();
         dbMapper.deleteAllAttValue();
         session.commit();

         dbMapper.insertDatamodel(dataModel);
         dbMapper.insertAttGroup(attGroup);
         dbMapper.insertAttElem(attElem);
         dbMapper.insertAttField(attField);

         for (TblAttributeValue attValue : attValueList) {
             dbMapper.insertAttValue(attValue);
         }

         if (keyList != null) {

             for (TblKeySolve key : keyList) {
                 dbMapper.insertKeySolve(key);
             }
         }
         session.commit();
     } finally {

         if (session != null) {
             session.close();
         }
     }
 }

From source file:me.kafeitu.activiti.extra.helper.RuntimeTaskHelper.java

License:Apache License

/**
 * //from   w  w  w  . j  a v a  2  s .  com
 * 
 * @param taskId
 *          ID?
 * @param userIds
 *          
 */
public void addSign(String taskId, String... userIds) {

    // open new session
    SqlSession sqlSession = openSession();
    PreparedStatement ps = null;

    try {
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        String processInstanceId = task.getProcessInstanceId();
        String taskDefinitionKey = task.getTaskDefinitionKey();

        // ?ACT_RU_EXECUTION
        // ???
        ExecutionEntity executionEntityOfMany = (ExecutionEntity) runtimeService.createNativeExecutionQuery()
                .sql("select * from ACT_RU_EXECUTION where PARENT_ID_ = (select ID_ from ACT_RU_EXECUTION where PARENT_ID_ = '"
                        + processInstanceId + "') limit 1")
                .singleResult();
        String processDefinitionId = executionEntityOfMany.getProcessDefinitionId();

        // ?ID
        TablePage listPage = managementService.createTablePageQuery().tableName("ACT_GE_PROPERTY").listPage(2,
                2);
        Map<String, Object> idMap = listPage.getRows().get(0);

        // ?ID
        Long nextId = Long.parseLong(idMap.get("VALUE_").toString());

        Connection connection = sqlSession.getConnection();

        for (String userId : userIds) {

            // ???
            long count = taskService.createTaskQuery().taskDefinitionKey(taskDefinitionKey)
                    .processInstanceId(processInstanceId).taskAssignee(userId).count();
            if (count > 0) {
                logger.warn("??{}, {}", userId, taskDefinitionKey);
                continue;
            }

            // ???
            Long newExecutionId = ++nextId;

            int counter = 1;
            ps = connection.prepareStatement(
                    "insert into ACT_RU_EXECUTION values(?, 1, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
            ps.setString(counter++, newExecutionId.toString()); // ID
            ps.setString(counter++, processInstanceId); // PROC_INST_ID_
            ps.setString(counter++, executionEntityOfMany.getBusinessKey()); // BUSINESS_KEY_
            ps.setString(counter++, executionEntityOfMany.getParentId()); // PARENT_ID_
            ps.setString(counter++, processDefinitionId); // PROC_DEF_ID_
            ps.setString(counter++, executionEntityOfMany.getSuperExecutionId()); // SUPER_EXEC_
            ps.setString(counter++, executionEntityOfMany.getActivityId()); // ACT_ID_
            ps.setString(counter++, "TRUE"); // IS_ACTIVE_
            ps.setString(counter++, "TRUE"); // IS_CONCURRENT_
            ps.setString(counter++, "FALSE"); // IS_SCOPE_
            ps.setString(counter++, "FALSE"); // IS_EVENT_SCOPE_
            ps.setInt(counter++, 1); // SUSPENSION_STATE_
            ps.setInt(counter++, executionEntityOfMany.getCachedEntityState()); // CACHED_ENT_STATE_
            ps.executeUpdate();

            // 
            // runtime task
            Long newTaskId = ++nextId;
            counter = 1;
            ps = connection.prepareStatement(
                    "insert into act_ru_task values(?, 1, ?, ?, ?, ?, null, null, ?, null, ?, null, ?, ?, null, '1')");
            ps.setString(counter++, newTaskId.toString());
            ps.setString(counter++, newExecutionId.toString());
            ps.setString(counter++, processInstanceId);
            ps.setString(counter++, processDefinitionId);
            ps.setString(counter++, task.getName());
            ps.setString(counter++, taskDefinitionKey);
            ps.setString(counter++, userId);
            ps.setInt(counter++, task.getPriority());
            ps.setTimestamp(counter++, new Timestamp(System.currentTimeMillis()));
            ps.executeUpdate();

            // history task
            counter = 1;
            ps = connection.prepareStatement(
                    "insert into act_hi_taskinst values(?, ?, ?, ?, ?, null, ?, null, null, ?, ?, null, null, null, ?, null)");
            ps.setString(counter++, newTaskId.toString());
            ps.setString(counter++, processDefinitionId + "'");
            ps.setString(counter++, taskDefinitionKey + "'");
            ps.setString(counter++, processInstanceId);
            ps.setString(counter++, newExecutionId.toString());
            ps.setString(counter++, task.getName());
            ps.setString(counter++, userId);
            ps.setTimestamp(counter++, new Timestamp(System.currentTimeMillis()));
            ps.setInt(counter++, task.getPriority());
            ps.executeUpdate();

            // ID
            String updateNextId = "update ACT_GE_PROPERTY set VALUE_ = ? where NAME_ = ?";
            ps = connection.prepareStatement(updateNextId);
            ps.setLong(1, nextId);
            ps.setString(2, "next.dbid");

            /*
             * ??
             */
            List<HistoricVariableInstance> list = historyService.createHistoricVariableInstanceQuery()
                    .processInstanceId(processInstanceId).variableNameLike("nrOf%").list();
            Integer nrOfInstances = 0;
            Integer nrOfActiveInstances = 0;
            for (HistoricVariableInstance var : list) {
                if (var.getVariableName().equals("nrOfInstances")) {
                    nrOfInstances = (Integer) var.getValue();
                } else if (var.getVariableName().equals("nrOfActiveInstances")) {
                    nrOfActiveInstances = (Integer) var.getValue();
                }
            }

            // ??
            nrOfInstances++;
            nrOfActiveInstances++;

            String updateVariablesOfMultiinstance = "update ACT_HI_VARINST set LONG_ = ?, TEXT_ = ? where EXECUTION_ID_ = ? and NAME_ = ?";
            ps = connection.prepareStatement(updateVariablesOfMultiinstance);
            ps.setLong(1, nrOfInstances);
            ps.setString(2, String.valueOf(nrOfInstances));
            ps.setString(3, executionEntityOfMany.getParentId());
            ps.setString(4, "nrOfInstances");
            ps.executeUpdate();

            ps.setLong(1, nrOfInstances);
            ps.setString(2, String.valueOf(nrOfActiveInstances));
            ps.setString(3, executionEntityOfMany.getParentId());
            ps.setString(4, "nrOfActiveInstances");
            ps.executeUpdate();

        }

        sqlSession.commit();
    } catch (Exception e) {
        logger.error("failed to add sign for countersign", e);
    } finally {
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                // do nothing
            }
        }
        sqlSession.close();
    }

}

From source file:mybatis.client.MyJFrame.java

public void updateData(String s_id, String s_pwd, String s_name, String s_email, String s_phone) {

    Map<String, String> map = new HashMap<String, String>();

    map.put("id", s_id);
    //map.put("pwd", s_pwd);
    map.put("name", s_name);
    map.put("email", s_email);
    map.put("phone", s_phone);

    SqlSession ss = factory.openSession(true);
    int cnt = ss.update("mem.update", map);

    if (cnt == 1) {
        JOptionPane.showMessageDialog(this, "?? ?.");
        ss.commit();
    } else {/* ww  w. j ava2s  .  c  o m*/
        JOptionPane.showMessageDialog(this, " ");
        ss.rollback();
    }
    ss.close();
}

From source file:net.cbtltd.rest.AbstractAccount.java

/**
 * Gets the events./* w w  w .  ja  va 2s .c o m*/
 *
* @param sqlSession the current SQL session.
 * @param test the test
 * @param items the items
 * @return the events
 */
private static Collection<Event> getEvents(SqlSession sqlSession, Boolean test, ArrayList<EventJournal> items) {
    Collection<Event> event = new ArrayList<Event>();
    Collection<Journal> journal = null;
    String lastId = null;
    int i = 0;
    for (EventJournal item : items) {
        if (!item.hasId(lastId)) {
            lastId = item.getId();
            journal = new ArrayList<Journal>();
            event.add(new Event(item, journal));
            if (!test) {
                sqlSession.getMapper(EventMapper.class).downloaded(lastId);
            }
        }
        journal.add(new Journal(item));
        if (i++ > 10000) {
            break;
        }
    }
    sqlSession.commit();
    return event;
}

From source file:net.cbtltd.rest.AbstractParty.java

/**
 * Gets the by id.//  w  w w . jav  a 2 s.  c o m
 *
 * @param partyid the partyid
 * @param pos the pos
 * @param xsl the xsl
 * @return the by id
 */
protected Party getById(String partyid, String pos, String xsl) {

    Date timestamp = new Date();
    String message = "/party/" + partyid + "?xsl=" + xsl + "?pos=" + pos;
    LOG.debug(message);
    SqlSession sqlSession = RazorServer.openSession();
    Party result = null;
    try {
        String organizationid = getParty(sqlSession, pos).getId();
        result = sqlSession.getMapper(PartyMapper.class).read(partyid);
        if (result == null) {
            throw new ServiceException(Error.party_id, partyid);
        }
        if (sqlSession.getMapper(RelationMapper.class)
                .match(new Relation(Relation.ORG_PARTY_, organizationid, partyid)) == null) {
            result.setEmailaddress(null);
        }
        if (!result.hasState(Constants.CREATED)) {
            throw new ServiceException(Error.party_inactive, partyid);
        }
        if (result.hasLocationid()) {
            net.cbtltd.shared.Location location = sqlSession.getMapper(LocationMapper.class)
                    .read(result.getLocationid());
            result.setCity(location.getName());
            result.setRegion(location.getRegion());
            result.setCountry(location.getCountry());
        }
        result.setLanguage(Language.EN);
        result.setXsl(xsl);
        sqlSession.commit();
    } catch (Throwable x) {
        sqlSession.rollback();
        result = new Party();
        result.setMessage(message + " " + x.getMessage());
        result.setXsl(xsl);
    } finally {
        sqlSession.close();
    }
    LOG.debug(result);
    MonitorService.monitor(message, timestamp);
    return result;
}

From source file:net.cbtltd.rest.AbstractParty.java

/**
 * Gets the attributes.// w  w w. j a v  a2s .c  o  m
 *
 * @param partyid the partyid
 * @param pos the pos
 * @param test the test
 * @param xsl the xsl
 * @return the attributes
 */
protected Attributes getAttributes(String partyid, String pos, Boolean test, String xsl) {

    Date timestamp = new Date();
    String message = "/party/" + partyid + "/attribute" + "?pos=" + pos + "&xsl=" + xsl + "&test=" + test;
    LOG.debug(message);
    SqlSession sqlSession = RazorServer.openSession();
    Attributes result = null;
    try {
        getParty(sqlSession, pos);
        HashMap<String, ArrayList<String>> attributeMap = RelationService.readMap(sqlSession,
                Relation.PARTY_ATTRIBUTE, partyid, Attribute.ACCOMMODATION_SEARCH);
        if (attributeMap == null || attributeMap.isEmpty()) {
            throw new ServiceException(Error.party_attribute, partyid);
        }
        Collection<Attribute> attributes = new ArrayList<Attribute>();
        for (String key : attributeMap.keySet()) {
            ArrayList<String> values = new ArrayList<String>();
            for (String value : attributeMap.get(key)) {
                values.add(getValueName(sqlSession, value));
            }
            if (attributeMap.get(key).size() > 0) {
                attributes.add(new Attribute(getValueName(sqlSession, key), values));
            }
        }
        result = new Attributes(NameId.Type.Party.name(), partyid, null, attributes, xsl);
        sqlSession.commit();
    } catch (Throwable x) {
        sqlSession.rollback();
        result = new Attributes(NameId.Type.Party.name(), partyid, message + " " + x.getMessage(), null, xsl);
    } finally {
        sqlSession.close();
    }
    LOG.debug(result);
    MonitorService.monitor(message, timestamp);
    return result;
}

From source file:net.cbtltd.rest.AbstractParty.java

/**
 * Gets the images by id./*  w w w .  j av a2  s . co  m*/
 *
 * @param partyid the partyid
 * @param pos the pos
 * @param test the test
 * @param xsl the xsl
 * @return the images by id
 */
protected Items getImagesById(String partyid, String pos, Boolean test, String xsl) {

    Date timestamp = new Date();
    String message = "/party/" + partyid + "/image?pos=" + pos + "&xsl=" + xsl + "&test=" + test;
    LOG.debug(message);
    SqlSession sqlSession = RazorServer.openSession();
    Items result = null;
    try {
        getParty(sqlSession, pos);
        result = new Items(NameId.Type.Party.name(), partyid, "Image", null, sqlSession
                .getMapper(TextMapper.class).imagesbynameid(new NameId(NameId.Type.Party.name(), partyid)),
                xsl);
        sqlSession.commit();
    } catch (Throwable x) {
        sqlSession.rollback();
        if (x != null && x.getMessage() != null && !x.getMessage().startsWith(Error.data_unchanged.name())) {
            LOG.error(message + "\n" + x.getMessage());
        }
        result = new Items(NameId.Type.Party.name(), partyid, "Image", message + " " + x.getMessage(), null,
                xsl);
    } finally {
        sqlSession.close();
    }
    LOG.debug(result);
    MonitorService.monitor(message, timestamp);
    return result;
}

From source file:net.cbtltd.rest.AbstractParty.java

/**
 * Sets the party text by id and name./*w  w w .  j  a v  a2  s.  c o m*/
 *
 * @param partyid the party ID
 * @param name the name of the text
 * @param notes the notes value of the text
 * @param pos the pos
 * @param xsl the xsl
 * @return the text by id and name
 */
protected Text setTextByIdName(String partyid, String name, String notes, String pos, String xsl) {
    Date timestamp = new Date();
    String message = "/party/" + partyid + "/text/" + name + "?pos=" + pos + "&notes=" + notes + "&xsl=" + xsl;
    LOG.debug(message);
    SqlSession sqlSession = RazorServer.openSession();
    Text result = null;
    try {
        if (Constants.BLANK.equalsIgnoreCase(notes)) {
            Text text = new Text("Party" + partyid + name, Language.EN);
            result = sqlSession.getMapper(TextMapper.class).readbyexample(text);
        } else {
            //            notes = URLDecoder.decode(notes, "UTF-8");
            Text text = new Text("Party" + partyid + name, name, Text.Type.Text, new Date(), notes,
                    Language.EN);
            Text exists = sqlSession.getMapper(TextMapper.class).readbyexample(text);
            if (exists == null) {
                sqlSession.getMapper(TextMapper.class).create(text);
            } else {
                text.setVersion(new Date());
                sqlSession.getMapper(TextMapper.class).update(text);
            }
            result = text;
            sqlSession.commit();
        }
    } catch (Throwable x) {
        sqlSession.rollback();
        result = new Text("Party" + partyid + name, name, Text.Type.Text, new Date(),
                Error.text_id.getMessage(), Language.EN);
    } finally {
        sqlSession.close();
    }
    LOG.debug(result);
    MonitorService.monitor(message, timestamp);
    return result;
}

From source file:net.cbtltd.rest.AbstractProduct.java

protected synchronized Property getProperty(String productid, String pos, String language, Boolean test,
        String version, String xsl) {

    final SqlSession sqlSession = RazorServer.openSession();
    Property property = getProperty(productid, pos, language, test, version, xsl, sqlSession);
    sqlSession.commit();
    sqlSession.close();/*from  ww w.  j a  v a2 s  .  c o  m*/
    return property;
}

From source file:net.cbtltd.rest.AbstractProduct.java

/**
 * Gets the detail along with reservation.
 *
 * @param productid the productid// w  w  w  .j  a v a 2  s. c o  m
 * @param pos the pos
 * @param language the language
 * @param test the test
 * @param xsl the xsl
 * @return the detail
 */
protected Property getCompleteDetail(String productid, String pos, String language, String date, String toDate,
        String currency, Boolean test, String version, String xsl) {

    Date timestamp = new Date();
    String message = "/product/" + productid + "/fulldetail?pos=" + pos + "&language=" + language + "&xsl="
            + xsl + "&test=" + test;
    LOG.debug(message);
    SqlSession sqlSession = RazorServer.openSession();
    Property result = null;
    try {
        result = getPropertyDetail(sqlSession, productid, pos, language, date, toDate, currency, null, test,
                version);
        ArrayList<ScheduleItem> items = sqlSession.getMapper(ReservationMapper.class)
                .flipkeyavailabilityproduct(productid);
        Collection<Reservation> reservation = new ArrayList<Reservation>();
        if (items != null && !items.isEmpty()) {
            for (ScheduleItem item : items) {
                reservation.add(new Reservation(item));
            }
            result.setReservations(new Reservations(reservation, Constants.NO_XSL));
        }
        result.setXsl(xsl);
        sqlSession.commit();
    } catch (Throwable x) {
        sqlSession.rollback();
        if (x != null && x.getMessage() != null && !x.getMessage().startsWith(Error.data_unchanged.name())) {
            LOG.error(message + "\n" + x.getMessage());
        }
        result = new Property();
        result.setMessage(message + " " + x.getMessage());
        result.setXsl(xsl);
    } finally {
        sqlSession.close();
    }
    LOG.debug(result);
    MonitorService.monitor(message, timestamp);
    return result;
}