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

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

Introduction

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

Prototype

public static void commitAndCloseQuietly(Connection conn) 

Source Link

Document

Commits a Connection then closes it, avoid closing if null and hide any SQLExceptions that occur.

Usage

From source file:org.sonar.server.db.migrations.v50.FeedFileSourcesTest.java

private void migrate_sources_with_scm_and_coverage_in(String columnName) throws Exception {
    db.prepareDbUnit(getClass(), "before.xml");

    Connection connection = null;
    try {//from   w  w  w.  j a v a2 s.c om
        connection = db.openConnection();

        connection
                .prepareStatement("insert into snapshot_sources " + "(snapshot_id, data, updated_at) "
                        + "values " + "(6, 'class Foo {\r\n  // Empty\r\n}\r\n', '2014-10-31 16:44:02.000')")
                .executeUpdate();

        db.executeUpdateSql("insert into snapshot_sources " + "(snapshot_id, data, updated_at) " + "values "
                + "(7, '', '2014-10-31 16:44:02.000')");

        PreparedStatement revisionStmt = connection.prepareStatement("insert into project_measures "
                + "(metric_id, snapshot_id, " + columnName + ") " + "values " + "(1, 6, ?)");
        revisionStmt.setBytes(1, "1=aef12a;2=abe465;3=afb789;4=afb789".getBytes(Charsets.UTF_8));
        revisionStmt.executeUpdate();

        PreparedStatement authorStmt = connection.prepareStatement("insert into project_measures "
                + "(metric_id, snapshot_id, " + columnName + ") " + "values " + "(2, 6, ?)");
        authorStmt.setBytes(1, "1=alice;2=bob;3=carol;4=carol".getBytes(Charsets.UTF_8));
        authorStmt.executeUpdate();

        PreparedStatement dateStmt = connection.prepareStatement("insert into project_measures "
                + "(metric_id, snapshot_id, " + columnName + ") " + "values " + "(3, 6, ?)");
        dateStmt.setBytes(1,
                "1=2014-04-25T12:34:56+0100;2=2014-07-25T12:34:56+0100;3=2014-03-23T12:34:56+0100;4=2014-03-23T12:34:56+0100"
                        .getBytes(Charsets.UTF_8));
        dateStmt.executeUpdate();

        PreparedStatement utHitsStmt = connection.prepareStatement("insert into project_measures "
                + "(metric_id, snapshot_id, " + columnName + ") " + "values " + "(4, 6, ?)");
        utHitsStmt.setBytes(1, "1=1;3=0".getBytes(Charsets.UTF_8));
        utHitsStmt.executeUpdate();

        PreparedStatement utCondStmt = connection.prepareStatement("insert into project_measures "
                + "(metric_id, snapshot_id, " + columnName + ") " + "values " + "(5, 6, ?)");
        utCondStmt.setBytes(1, "1=4".getBytes(Charsets.UTF_8));
        utCondStmt.executeUpdate();

        PreparedStatement utCoveredCondStmt = connection.prepareStatement("insert into project_measures "
                + "(metric_id, snapshot_id, " + columnName + ") " + "values " + "(6, 6, ?)");
        utCoveredCondStmt.setBytes(1, "1=2".getBytes(Charsets.UTF_8));
        utCoveredCondStmt.executeUpdate();

        PreparedStatement itHitsStmt = connection.prepareStatement("insert into project_measures "
                + "(metric_id, snapshot_id, " + columnName + ") " + "values " + "(7, 6, ?)");
        itHitsStmt.setBytes(1, "1=2;3=0".getBytes(Charsets.UTF_8));
        itHitsStmt.executeUpdate();

        PreparedStatement itCondStmt = connection.prepareStatement("insert into project_measures "
                + "(metric_id, snapshot_id, " + columnName + ") " + "values " + "(8, 6, ?)");
        itCondStmt.setBytes(1, "1=5".getBytes(Charsets.UTF_8));
        itCondStmt.executeUpdate();

        PreparedStatement itCoveredCondStmt = connection.prepareStatement("insert into project_measures "
                + "(metric_id, snapshot_id, " + columnName + ") " + "values " + "(9, 6, ?)");
        itCoveredCondStmt.setBytes(1, "1=3".getBytes(Charsets.UTF_8));
        itCoveredCondStmt.executeUpdate();

        PreparedStatement overallHitsStmt = connection.prepareStatement("insert into project_measures "
                + "(metric_id, snapshot_id, " + columnName + ") " + "values " + "(10, 6, ?)");
        overallHitsStmt.setBytes(1, "1=3;3=0".getBytes(Charsets.UTF_8));
        overallHitsStmt.executeUpdate();

        PreparedStatement overallCondStmt = connection.prepareStatement("insert into project_measures "
                + "(metric_id, snapshot_id, " + columnName + ") " + "values " + "(11, 6, ?)");
        overallCondStmt.setBytes(1, "1=6".getBytes(Charsets.UTF_8));
        overallCondStmt.executeUpdate();

        PreparedStatement overallCoveredCondStmt = connection.prepareStatement("insert into project_measures "
                + "(metric_id, snapshot_id, " + columnName + ") " + "values " + "(12, 6, ?)");
        overallCoveredCondStmt.setBytes(1, "1=4".getBytes(Charsets.UTF_8));
        overallCoveredCondStmt.executeUpdate();

        PreparedStatement duplicationDataStmt = connection.prepareStatement("insert into project_measures "
                + "(metric_id, snapshot_id, " + columnName + ") " + "values " + "(13, 6, ?)");
        duplicationDataStmt.setBytes(1,
                "<duplications><g><b s=\"1\" l=\"1\" r=\"MyProject:src/main/xoo/prj/MyFile.xoo\"/><b s=\"2\" l=\"1\" r=\"MyProject:src/main/xoo/prj/MyFile.xoo\"/><b s=\"3\" l=\"1\" r=\"MyProject:src/main/xoo/prj/AnotherFile.xoo\"/></g></duplications>"
                        .getBytes(Charsets.UTF_8));
        duplicationDataStmt.executeUpdate();
    } finally {
        DbUtils.commitAndCloseQuietly(connection);
    }

    migration.execute();

    db.assertDbUnit(getClass(), "after-with-scm.xml", "file_sources");
}

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

/**
 * <p>/*ww w . ja  v  a2s. 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>/*w w w.  ja  v a 2s . co  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>//  w w  w.  j  a v a 2 s  .  c o m
 * ????
 * </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 w  w .ja  v a  2s  .  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>/*from ww  w  . j av a  2 s  .  c  om*/
 * ???????
 * </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  av a 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);
    }
}

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

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

        activityDao.modifyTitle(conn, activity);
        activityDao.modifyIndexes(conn, activity, category);

        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 .  j  a  v a 2 s.c o  m*/
 * ???categories??indexes????
 * </p>
 * 
 * @param category
 *            Entity
 * @throws TaskletException
 *             DB
 */
public void delete(Category category) throws TaskletException {
    Connection conn = null;
    try {
        conn = DaoFactory.getInstance().getConnection();
        conn.setAutoCommit(false);

        categoryDao.revertIndexes(conn, category);
        categoryDao.deleteCategory(conn, category);

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

    }
}

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

/**
 * <p>//from www.  j a v a2s.co m
 * ???
 * </p>
 * 
 * @param task
 *            Entity
 * @param memo
 *            Entity
 * @exception DB?
 */
public void update(Task task, Memo memo) throws TaskletException {
    Connection conn = null;
    try {
        conn = DaoFactory.getInstance().getConnection();
        conn.setAutoCommit(false);

        if (task.getStatus() == Status.FINISH) {
            // ??????
            taskDao.completeTask(conn, task);
            int count = taskDao.getIncompleteCount(task.getActivityId()).intValue();
            if (count == 0) {
                activityDao.completeActivity(conn, task.getActivityId());
            }
        } else {
            // tasks?
            taskDao.updateTask(conn, task);
            activityDao.updateActivity(conn, task.getActivityId());
        }

        // ????????
        if (!memo.getContents().equals("")) {
            // ?
            int seq = getSeq(task);
            memo.setSeq(seq);

            // ?ID??ID??
            memo.setTaskId(task.getId());

            // memos?
            memoDao.addMemoToMemos(conn, memo);
        }

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