Example usage for org.apache.commons.dbutils DbUtils rollbackAndCloseQuietly

List of usage examples for org.apache.commons.dbutils DbUtils rollbackAndCloseQuietly

Introduction

In this page you can find the example usage for org.apache.commons.dbutils DbUtils rollbackAndCloseQuietly.

Prototype

public static void rollbackAndCloseQuietly(Connection conn) 

Source Link

Document

Performs a rollback on the Connection then closes it, avoid closing if null and hide any SQLExceptions that occur.

Usage

From source file:org.gaixie.jibu.security.service.SettingServiceTest.java

@After
public void tearDown() {
    Connection conn = null;//from  w  w  w. ja v  a 2s . c  o  m
    try {
        conn = ConnectionUtils.getConnection();
        QueryRunner run = new QueryRunner();
        run.update(conn, "DELETE from user_setting_map");
        run.update(conn, "DELETE from settings");
        run.update(conn, "DELETE from userbase");
        DbUtils.commitAndClose(conn);
    } catch (SQLException e) {
        DbUtils.rollbackAndCloseQuietly(conn);
        System.out.println(e.getMessage());
    }
}

From source file:org.gaixie.jibu.security.service.UserServiceTest.java

@After
public void tearDown() {
    Connection conn = null;//from  www  . j  a  va  2 s  .  c o  m
    try {
        conn = ConnectionUtils.getConnection();
        QueryRunner run = new QueryRunner();
        run.update(conn, "DELETE from userbase");
        DbUtils.commitAndClose(conn);
    } catch (SQLException e) {
        DbUtils.rollbackAndCloseQuietly(conn);
        System.out.println(e.getMessage());
    }
}

From source file:org.kaaproject.kaa.server.datamigration.MigrateData.java

/**
 * The entry point of migrate data application.
 *
 * @param args the input options/*from  w  w  w.  j av a2  s . com*/
 */
public static void main(String[] args) {
    Options options = new Options();

    for (int i = 0; i < args.length; i++) {
        String arg = args[i];
        if (arg.charAt(0) == '-') {
            String option = arg.substring(1, arg.length()).trim();
            if (i >= args.length - 1) {
                throw new IllegalArgumentException("Not found value after option -" + option);
            }
            switch (option) {
            case "u":
                options.setUsername(args[i + 1]);
                break;
            case "p":
                options.setPassword(args[i + 1]);
                break;
            case "h":
                options.setHost(args[i + 1]);
                break;
            case "db":
                options.setDbName(args[i + 1]);
                break;
            case "nosql":
                options.setNoSql(args[i + 1]);
                break;
            case "driver":
                options.setDriverClassName(args[i + 1]);
                break;
            case "url":
                options.setJdbcUrl(args[i + 1]);
                break;
            default:
                throw new IllegalArgumentException("No such option: -" + option);
            }
        }
    }

    LOG.debug(options.toString());

    try {
        List<Schema> schemas = new ArrayList<>();
        conn = DataSources.getDataSource(options).getConnection();
        QueryRunner runner = new QueryRunner();
        Long maxId = runner.query(conn, "select max(id) as max_id from base_schems",
                rs -> rs.next() ? rs.getLong("max_id") : null);

        BaseSchemaIdCounter.setInitValue(maxId);

        final UpdateUuidsMigration updateUuidsMigration = new UpdateUuidsMigration(conn, options);

        final EndpointProfileMigration endpointProfileMigration = new EndpointProfileMigration(options);

        List<AbstractCtlMigration> migrationList = new ArrayList<>();
        migrationList.add(new CtlConfigurationMigration(conn));
        migrationList.add(new CtlEventsMigration(conn));
        migrationList.add(new CtlNotificationMigration(conn, options));
        migrationList.add(new CtlLogMigration(conn));

        new EndpointSpecificConfigurationMigration(options.getHost(), options.getDbName(), options.getNoSql())
                .transform();

        final CtlAggregation aggregation = new CtlAggregation(conn);
        final BaseSchemaRecordsCreation recordsCreation = new BaseSchemaRecordsCreation(conn);

        // convert uuids from latin1 to base64
        updateUuidsMigration.transform();
        endpointProfileMigration.transform();

        //before phase
        for (AbstractCtlMigration m : migrationList) {
            m.beforeTransform();
        }

        // transform phase
        for (AbstractCtlMigration m : migrationList) {
            schemas.addAll(m.transform());
        }

        //aggregation phase
        Map<Ctl, List<Schema>> ctlToSchemas = aggregation.aggregate(schemas);

        //base schema records creation phase
        recordsCreation.create(ctlToSchemas);

        //after phase
        for (AbstractCtlMigration m : migrationList) {
            m.afterTransform();
        }

        conn.commit();
    } catch (SQLException | IOException | ConfigurationGenerationException ex) {
        LOG.error("Error: " + ex.getMessage(), ex);
        DbUtils.rollbackAndCloseQuietly(conn);
    } finally {
        DbUtils.rollbackAndCloseQuietly(conn);
    }
}

From source file:org.moneta.dao.SqlSelectExecutor.java

public SearchResult call() {
    SearchResult result = new SearchResult();
    Connection topicConnection = null;
    QueryRunner runner = new QueryRunner();

    try {/*  w ww.  ja v a  2 s  . c  om*/
        topicConnection = MonetaEnvironment.getConfiguration().getConnection(topic.getDataSourceName());

        RecordResultSetHandler handler = new RecordResultSetHandler();
        handler.setMaxRows(this.getMaxRows());
        handler.setStartRow(this.getStartRow());
        handler.getAliasMap().putAll(topic.getAliasMap());

        result.setResultData(runner.query(topicConnection, sqlStmt.getSqlText(), handler,
                sqlStmt.getHostVariableValueList().toArray()));

        if (topicConnection.getAutoCommit()) {
            DbUtils.closeQuietly(topicConnection);
        } else {
            DbUtils.commitAndCloseQuietly(topicConnection);
        }
    } catch (Exception e) {
        result.setErrorCode(500);
        result.setErrorMessage(ExceptionUtils.getStackTrace(e));
        DbUtils.rollbackAndCloseQuietly(topicConnection);
    }

    return result;

}

From source file:org.yukung.tasklet.logic.AccountTxLogic.java

/**
 * <p>//from  w w w.  j a v a2 s  .c o  m
 * ????
 * </p>
 * 
 * @param user
 *            Entity
 * @throws TaskletException
 *             DB?
 */
public void register(User user) throws TaskletException {
    Connection conn = null;
    try {
        conn = DaoFactory.getInstance().getConnection();
        conn.setAutoCommit(false);

        // users?
        userDao.addUser(conn, user);
        // ???ID?
        int userId = userDao.findUserByUserName(user.getUserName()).getId();
        // categories??
        categoryDao.addDefaultCategory(conn, userId);

        DbUtils.commitAndCloseQuietly(conn);
    } catch (SQLException e) {
        DbUtils.rollbackAndCloseQuietly(conn);
        throw new TaskletException(e.getMessage(), e);
    }
}

From source file:org.yukung.tasklet.logic.ActivityTxLogic.java

/**
 * <p>//  ww w.j  av a  2s .  c o  m
 * ????
 * </p>
 * 
 * @param activity
 *            Entity
 * @param userId
 * @throws TaskletException
 *             DB?
 */
public void add(Activity activity, int userId) throws TaskletException {
    Connection conn = null;
    try {
        conn = DaoFactory.getInstance().getConnection();
        conn.setAutoCommit(false);

        // activities?
        activityDao.addActivityToActivities(conn, activity);
        // ???ID?
        activity.setId(activityDao.getLastInsertId(activity.getTitle()).intValue());
        // indexes?
        activityDao.addActivityToIndexes(conn, activity, userId);

        DbUtils.commitAndCloseQuietly(conn);
    } catch (SQLException e) {
        DbUtils.rollbackAndCloseQuietly(conn);
        throw new TaskletException(e.getMessage(), e);
    }

}

From source file:org.yukung.tasklet.logic.ActivityTxLogic.java

/**
 * <p>/*www. j a  v a  2 s.  c om*/
 * ????
 * </p>
 * 
 * @param userId
 *            ID
 * @throws TaskletException
 *             DB?
 */
public void ascend(int userId) throws TaskletException {
    Connection conn = null;
    try {
        conn = DaoFactory.getInstance().getConnection();
        conn.setAutoCommit(false);

        List<Activity> origin = activityDao.getSeqByAscending(userId);
        // ?????
        int index = origin.size();
        for (Activity activity : origin) {
            activity.setSeq(index);
            activityDao.updateSeq(conn, activity);
            index--;
        }

        DbUtils.commitAndCloseQuietly(conn);
    } catch (SQLException e) {
        DbUtils.rollbackAndCloseQuietly(conn);
        throw new TaskletException(e.getMessage(), e);
    }
}

From source file:org.yukung.tasklet.logic.ActivityTxLogic.java

/**
 * <p>//from w  ww. ja  va  2  s  . c o m
 * ?????
 * 
 * @param userId
 *            ID
 * @throws TaskletException
 *             DB?
 */
public void descend(int userId) throws TaskletException {
    Connection conn = null;
    try {
        conn = DaoFactory.getInstance().getConnection();
        conn.setAutoCommit(false);

        List<Activity> origin = activityDao.getSeqByDescending(userId);
        // ?????
        int index = origin.size();
        for (Activity activity : origin) {
            activity.setSeq(index);
            activityDao.updateSeq(conn, activity);
            index--;
        }

        DbUtils.commitAndCloseQuietly(conn);
    } catch (SQLException e) {
        DbUtils.rollbackAndCloseQuietly(conn);
        throw new TaskletException(e.getMessage(), e);
    }
}

From source file:org.yukung.tasklet.logic.ActivityTxLogic.java

/**
 * <p>//w  w w  . j a v  a  2  s  . c  o  m
 * ???????
 * </p>
 * 
 * @param sortedActivities
 *            ??Entity
 * @throws TaskletException
 *             DB?
 */
public void sort(List<Activity> sortedActivities) throws TaskletException {
    Connection conn = null;
    try {
        conn = DaoFactory.getInstance().getConnection();
        for (Activity activity : sortedActivities) {
            activityDao.updateSeq(conn, activity);
        }

        DbUtils.commitAndCloseQuietly(conn);
    } catch (SQLException e) {
        DbUtils.rollbackAndCloseQuietly(conn);
        throw new TaskletException(e.getMessage(), e);
    }
}

From source file:org.yukung.tasklet.logic.ActivityTxLogic.java

/**
 * <p>//from w  w w .  j  a  va 2s. com
 * ????????
 * </p>
 * 
 * @param category
 *            Entity
 * @throws TaskletException
 *             DB
 */
public void addCategory(Category category) throws TaskletException {
    Connection conn = null;
    try {
        conn = DaoFactory.getInstance().getConnection();
        conn.setAutoCommit(false);

        // ??
        Integer count = categoryDao.getMaxSeqOfCategories(category.getUserId());
        category.setSeq(getSeq(count));
        // categories?
        categoryDao.add(conn, category);

        DbUtils.commitAndCloseQuietly(conn);
    } catch (SQLException e) {
        DbUtils.rollbackAndCloseQuietly(conn);
        throw new TaskletException(e.getMessage(), e);
    }
}