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

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

Introduction

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

Prototype

void rollback();

Source Link

Document

Discards pending batch statements and rolls database connection back.

Usage

From source file:net.cbtltd.server.RazorServer.java

/**
 * Executes the specified action on the action.service() service.
 *
 * @param action the action to be executed.
 * @return the response.//ww w . jav a2 s . co m
 * @throws SerializationException the serialization exception
 */
public HasResponse execute(HasService action) throws SerializationException {

    HasResponse response = null;
    LOG.debug("\n\nRazorServer execute " + action.getClass().getName() + "\naction " + action);
    String classname = action.service().classname();

    try {
        Class<?> c = Class.forName(classname); // say AccountService
        Method[] allMethods = c.getDeclaredMethods(); // can be static
        for (Method m : allMethods) {
            String mname = m.getName();
            if (mname.equals("execute")) {
                Type[] pType = m.getGenericParameterTypes();
                if (pType[1].toString().equals("class " + action.getClass().getName())) {
                    SqlSession sqlSession = openSession();
                    try {
                        m.setAccessible(true);
                        Object t = getService(action.service());
                        response = (HasResponse) m.invoke(t, sqlSession, action);
                        sqlSession.commit();
                    } catch (IllegalAccessException x) {
                        LOG.error("IllegalAccessException " + action.getClass().getName() + "\n"
                                + x.getMessage());
                    } catch (InvocationTargetException x) {
                        LOG.error("InvocationTargetException exception " + action.getClass().getName() + "\n"
                                + x.getMessage());
                    } catch (Throwable x) {
                        sqlSession.rollback();
                        MonitorService.log(x);
                    } finally {
                        sqlSession.close();
                    }
                }
            }
        }
    } catch (ClassNotFoundException x) {
        MonitorService.log(x);
        LOG.error("ClassNotFoundException " + action.getClass().getName() + "\n" + x.getMessage());
    }
    return response;
}

From source file:net.cbtltd.server.ReservationService.java

License:Open Source License

/**
 * Executes the OfflineRead action to read an off line Reservation instance.
 * Reads the reservation for its name, state, arrival and departure dates, prices and currency.
 *
 * @param sqlSession the current SQL session.
 * @param action the action to be executed.
 * @return the response./*w w  w.  j a  va  2  s  .co m*/
 */
public final Reservation execute(SqlSession sqlSession, OfflineRead action) {
    Date timestamp = new Date();
    try {
        return sqlSession.getMapper(ReservationMapper.class).offlineread(action.getId());
    } catch (Throwable x) {
        sqlSession.rollback();
        MonitorService.log(x);
    }
    MonitorService.monitor("OfflineRead", timestamp);
    return action;
}

From source file:net.cbtltd.server.ReservationService.java

License:Open Source License

public static final Reservation offline(SqlSession sqlSession, Reservation action, boolean accept) {
    Date timestamp = new Date();
    try {//  ww w . jav  a2 s.c om
        //LOG.debug("OfflineAccept " + action);
        Reservation reservation = sqlSession.getMapper(ReservationMapper.class).read(action.getId());
        if (reservation.hasState(Reservation.State.Initial.name())) {
            reservation.setState(accept ? Reservation.State.Confirmed.name() : Reservation.State.Final.name());
            //            reservationUpdate(sqlSession, reservation);
            if (reservation.hasCollisions()) {
                reservation.setState(Reservation.State.Final.name());
            }
            sqlSession.getMapper(ReservationMapper.class).update(reservation);
            if (reservation.hasState(Reservation.State.Final.name())) {
                EmailService.rejectedReservation(sqlSession, reservation);
            } else {
                EmailService.acceptedReservation(sqlSession, reservation);
            }
            MonitorService.update(sqlSession, Data.Origin.CONSOLE, NameId.Type.Reservation, reservation);
        }
    } catch (Throwable x) {
        sqlSession.rollback();
        MonitorService.log(x);
    }
    MonitorService.monitor("Offline", timestamp);
    return action;
}

From source file:net.cbtltd.server.ReservationService.java

License:Open Source License

/**
 * Executes the ReservationWidget action to create a Reservation instance if the manager is not known.
 *
 * @param sqlSession the current SQL session.
 * @param action the action to be executed.
 * @return the response.//from  ww  w.java 2 s.  c  o m
 */
public final Reservation execute(SqlSession sqlSession, ReservationWidget action) {
    Date timestamp = new Date();
    try {
        Product product = sqlSession.getMapper(ProductMapper.class).read(action.getProductid());
        action.setOrganizationid(product.getSupplierid());
        action.setName(SessionService.pop(sqlSession, action.getOrganizationid(), Serial.RESERVATION));
        sqlSession.getMapper(ReservationMapper.class).create(action);
        MonitorService.update(sqlSession, Data.Origin.CONSOLE, NameId.Type.Reservation, action);
    } catch (Throwable x) {
        sqlSession.rollback();
        MonitorService.log(x);
    }
    MonitorService.monitor("ReservationWidget", timestamp);
    return action;
}

From source file:net.cbtltd.server.ReservationService.java

License:Open Source License

/**
 * Executes the ReservationCreate action to create a Reservation instance.
 *
 * @param sqlSession the current SQL session.
 * @param action the action to be executed.
 * @return the response./*from w w  w.  java  2  s.  co m*/
 */
public final Reservation execute(SqlSession sqlSession, ReservationCreate action) {
    Date timestamp = new Date();
    //LOG.debug("ReservationCreate in " + action);
    try {
        action.setName(SessionService.pop(sqlSession, action.getOrganizationid(), Serial.RESERVATION));
        sqlSession.getMapper(ReservationMapper.class).create(action);
        MonitorService.update(sqlSession, Data.Origin.CONSOLE, NameId.Type.Reservation, action);
    } catch (Throwable x) {
        sqlSession.rollback();
        MonitorService.log(x);
    }
    //LOG.debug("ReservationCreate out " + action);
    MonitorService.monitor("ReservationCreate", timestamp);
    return action;
}

From source file:net.cbtltd.server.ReservationService.java

License:Open Source License

/**
 * Executes the ReservationRead action to read a Reservation instance.
 *
 * @param sqlSession the current SQL session.
 * @param action the action to be executed.
 * @return the response.// w w  w . j a va2 s.com
 */
public final Reservation execute(SqlSession sqlSession, ReservationRead action) {
    Date timestamp = new Date();
    //LOG.debug("ReservationRead in " + action);
    Reservation reservation = null;
    try {
        reservation = sqlSession.getMapper(ReservationMapper.class).read(action.getId());
        reservation.setQuotedetail(sqlSession.getMapper(PriceMapper.class).quotedetail(action.getId()));
        reservation.setTasks(TaskService.readbyparentid(sqlSession, action.getId()));
        reservation.setOldstate(reservation.getState());
        getWorkflow(sqlSession, reservation);
        //LOG.debug("ReservationRead out " + reservation);
    } catch (Throwable x) {
        sqlSession.rollback();
        MonitorService.log(x);
    }
    MonitorService.monitor("ReservationRead", timestamp);
    return reservation;
}

From source file:net.cbtltd.server.ReservationService.java

License:Open Source License

/**
 * Executes the ReservationUndo action to return a Reservation instance to its previous state.
 *
 * @param sqlSession the current SQL session.
 * @param action the action to be executed.
 * @return the response./*from w  w w  . j a  va2s.co m*/
 */
public final Reservation execute(SqlSession sqlSession, ReservationUndo action) {
    try {
        sqlSession.getMapper(ReservationMapper.class).deleteoldstate(action);
    } catch (Throwable x) {
        sqlSession.rollback();
        MonitorService.log(x);
    }
    return reservationUpdate(sqlSession, action);
}

From source file:net.cbtltd.server.ReservationService.java

License:Open Source License

/**
 * Updates the specified Reservation instance if it does not collide with any other reservations.
 * The collision test is done for the product itself and for parent and child properties if the 
 * reserved product is part of a composite property. Text is updated and the next and previous
 * states for the new state are set using he work flow rules of the organization.
 *
 * @param sqlSession the current SQL session.
 * @param reservation the reservation to be updated.
 * @return the updated reservation./*w  w w  .ja  v  a2s  .c o  m*/
 */
public static final Reservation reservationUpdate(SqlSession sqlSession, Reservation reservation) {
    Date timestamp = new Date();
    LOG.debug("reservationUpdate in " + reservation);
    try {
        reservation.setCollisions(getCollisions(sqlSession, reservation));

        if (reservation.noCollisions()) {
            Product product = sqlSession.getMapper(ProductMapper.class).read(reservation.getProductid());
            if (product == null) {
                throw new ServiceException(Error.product_id, reservation.getProductid());
            }
            reservation.setAltpartyid(product.getAltpartyid());
            reservation.setOrganizationid(product.getSupplierid());
            if (reservation.noActorid()) {
                reservation.setActorid(Party.NO_ACTOR);
            }

            //TODO: extend for other APIs
            if (product.hasAltpartyid(PartyIds.PARTY_INTERHOME_ID)) {
                Party agent = sqlSession.getMapper(PartyMapper.class).read(reservation.getAgentid());
                if (agent == null) {
                    throw new ServiceException(Error.party_id, reservation.getAgentid());
                }
                agent.setValues(
                        RelationService.read(sqlSession, Relation.PARTY_VALUE, reservation.getAgentid(), null));
                String interhome = agent.getValue(Party.Value.Interhome.name());
                if (interhome == null || interhome.isEmpty()) {
                    throw new ServiceException(Error.reservation_api,
                            "You need an Interhome retailer code to book this property - request for a code for RAZOR at partners@interhome.com");
                }
                reservation.setAgentname(interhome);
            }

            TextService.update(sqlSession, reservation.getTexts());

            if (reservation.hasState(Reservation.State.Initial.name())) {
                sqlSession.getMapper(ReservationMapper.class).update(reservation);
                EmailService.offlineReservation(sqlSession, reservation, null,
                        RazorHostsConfig.getOfflineUrl());
            } else {
                TaskService.update(sqlSession, reservation.getId(), reservation.getTasks());
                getWorkflow(sqlSession, reservation);
                sqlSession.getMapper(ReservationMapper.class).update(reservation);
                createQuotedetail(sqlSession, reservation);
                sqlSession.getMapper(SpecialMapper.class).deletequotecollision(reservation);
                onStateChange(sqlSession, reservation, product);
                if (reservation.hasState(Reservation.State.Provisional.name())) {
                    if (reservation.hasAltpartyid()) {
                        PartnerService.createReservation(sqlSession, reservation);
                    }
                    if (reservation.isActive() && product.noAltpartyid()) {
                        EmailService.provisionalReservation(sqlSession, reservation);
                    }
                } else if (reservation.hasState(Reservation.State.Departed.name())) {
                    EmailService.departedReservation(sqlSession, reservation);
                } else if (reservation.hasAltpartyid()
                        && reservation.hasState(Reservation.State.Confirmed.name())) {
                    PartnerService.confirmReservation(sqlSession, reservation);
                } else if (reservation.hasAltpartyid()
                        && reservation.hasState(Reservation.State.Cancelled.name())) {
                    PartnerService.cancelReservation(sqlSession, reservation);
                }
            }
            sqlSession.getMapper(ReservationMapper.class).update(reservation);
            MonitorService.update(sqlSession, Data.Origin.CONSOLE, NameId.Type.Reservation, reservation);
        }
    } catch (Throwable x) {
        sqlSession.rollback();
        reservation.setMessage(x.getMessage());
        MonitorService.log(x);
    }
    //LOG.debug("reservationUpdate out " + reservation);
    MonitorService.monitor("ReservationUpdate", timestamp);
    return reservation;
}

From source file:net.cbtltd.server.ReservationService.java

License:Open Source License

/**
 * Executes the ReservationDelete action to delete a Reservation instance.
 * This sets its state to final but does not delete the instance.
 *
 * @param sqlSession the current SQL session.
 * @param action the action to be executed.
 * @return the response./*  www.  java 2  s. c o m*/
 */
public final Reservation execute(SqlSession sqlSession, ReservationDelete action) {
    Date timestamp = new Date();
    try {
        action.setState(Reservation.State.Final.name());
        sqlSession.getMapper(ReservationMapper.class).update(action);
        if (action.hasAltpartyid()) {
            PartnerService.cancelReservation(sqlSession, action);
        }
        MonitorService.update(sqlSession, Data.Origin.CONSOLE, NameId.Type.Reservation, action);
    } catch (Throwable x) {
        sqlSession.rollback();
        MonitorService.log(x);
    }
    MonitorService.monitor("ReservationDelete", timestamp);
    return null;
}

From source file:net.cbtltd.server.ReservationService.java

License:Open Source License

/**
 * Executes the NameIdAction action to read a list of reservation NameId instances.
 *
 * @param sqlSession the current SQL session.
 * @param action the action to be executed.
 * @return the response./*from   w w  w .  j ava2  s. com*/
 */
public final Table<NameId> execute(SqlSession sqlSession, NameIdAction action) {
    Date timestamp = new Date();
    Table<NameId> table = new Table<NameId>();
    try {
        if (action.isSuggested()) {
            table.setValue(sqlSession.getMapper(ReservationMapper.class).nameidbyid(action));
        } else {
            table.setValue(sqlSession.getMapper(ReservationMapper.class).nameidbyname(action));
        }
    } catch (Throwable x) {
        sqlSession.rollback();
        MonitorService.log(x);
    }
    MonitorService.monitor("Reservation NameIdAction", timestamp);
    return table;
}