Example usage for java.sql Connection releaseSavepoint

List of usage examples for java.sql Connection releaseSavepoint

Introduction

In this page you can find the example usage for java.sql Connection releaseSavepoint.

Prototype

void releaseSavepoint(Savepoint savepoint) throws SQLException;

Source Link

Document

Removes the specified Savepoint and subsequent Savepoint objects from the current transaction.

Usage

From source file:com.redhat.victims.database.VictimsSqlDB.java

public void synchronize() throws VictimsException {
    Throwable throwable = null;//w  w w  . jav  a2 s .  com
    try {
        Connection connection = getConnection();
        connection.setAutoCommit(false);
        Savepoint savepoint = connection.setSavepoint();

        try {
            VictimsService service = new VictimsService();
            Date since = lastUpdated();

            int removed = remove(connection, service.removed(since));
            int updated = update(connection, service.updates(since));

            if (removed > 0 || updated > 0) {
                cache.purge();
            }

            setLastUpdate(new Date());
        } catch (IOException e) {
            throwable = e;
        } catch (SQLException e) {
            throwable = e;
        } finally {
            if (throwable != null) {
                connection.rollback(savepoint);
            }
            connection.releaseSavepoint(savepoint);
            connection.commit();
            connection.close();
        }
    } catch (SQLException e) {
        throwable = e;
    }

    if (throwable != null) {
        throw new VictimsException("Failed to sync database", throwable);
    }
}

From source file:com.adaptris.jdbc.connection.FailoverDatasourceTest.java

@Test
public void testCommitRollback() throws Exception {
    Connection conn = new MyProxy();

    try {// ww  w . java2 s .c om
        try {
            conn.setAutoCommit(conn.getAutoCommit());
        } catch (SQLException e) {

        }
        try {
            conn.setAutoCommit(false);
            conn.commit();
        } catch (SQLException e) {

        }

        try {
            conn.setAutoCommit(false);
            conn.rollback();
        } catch (SQLException e) {

        }

        try {
            conn.setAutoCommit(false);
            conn.rollback(conn.setSavepoint());
        } catch (SQLException e) {

        }
        try {
            conn.setAutoCommit(false);
            conn.rollback(conn.setSavepoint("test"));
        } catch (SQLException e) {

        }
        try {
            conn.setAutoCommit(false);
            conn.releaseSavepoint(conn.setSavepoint("test2"));
        } catch (SQLException e) {

        }
    } finally {
        JdbcUtil.closeQuietly(conn);

    }
}

From source file:org.openconcerto.sql.utils.SQLUtils.java

/**
 * Execute <code>h</code> in a transaction. Only the outer most call to
 * <code>executeAtomic()</code> commit or roll back a transaction, for recursive calls if
 * <code>continueTx</code> is <code>true</code> then nothing happens, else a save point is
 * created and rolled back if an exception occurs (allowing the caller to catch the exception
 * without loosing the current transaction).
 * <p>/*w  w w.  ja v a 2 s  . c  o m*/
 * NOTE : if <code>continueTx</code> is <code>true</code> and an exception is thrown, the
 * connection might be aborted. So you should notify the caller, e.g. propagate the exception so
 * that he can roll back the transaction.
 * </p>
 * 
 * @param <T> type of return
 * @param <X> type of exception of <code>h</code>
 * @param ds the data source where h should be executed.
 * @param h the code to execute.
 * @param continueTx only relevant if already in a transaction : if <code>true</code> the
 *        handler will just be executed and the connection won't be modified (i.e. the existing
 *        transaction will neither be committed nor rolled back) ; if <code>false</code> a save
 *        point will be used.
 * @return what h returns.
 * @throws SQLException if a problem occurs.
 * @throws X if <code>h</code> throw it.
 */
public static <T, X extends Exception> T executeAtomic(final SQLDataSource ds,
        final ConnectionHandlerNoSetup<T, X> h, final boolean continueTx) throws SQLException, X {
    return ds.useConnection(new ConnectionHandler<T, X>() {

        private Boolean autoCommit = null;
        private Savepoint savePoint = null;

        @Override
        public boolean canRestoreState() {
            return true;
        }

        @Override
        public void setup(Connection conn) throws SQLException {
            this.autoCommit = conn.getAutoCommit();
            if (this.autoCommit) {
                conn.setAutoCommit(false);
            } else if (!continueTx) {
                this.savePoint = conn.setSavepoint();
            }
        }

        @Override
        public T handle(final SQLDataSource ds) throws X, SQLException {
            return h.handle(ds);
        }

        @Override
        public void restoreState(Connection conn) throws SQLException {
            // can be null if getAutoCommit() failed, in that case nothing to do
            final boolean hasStoppedAutoCommit = Boolean.TRUE.equals(this.autoCommit);
            final boolean hasSavePoint = this.savePoint != null;
            // at most one is enough (otherwise change if/else below)
            assert !(hasStoppedAutoCommit && hasSavePoint) : "Begun a transaction and created a save point";
            if (hasStoppedAutoCommit || hasSavePoint) {
                // true if the exception was thrown by get()
                boolean getExn = true;
                try {
                    this.get();
                    getExn = false;
                    if (hasStoppedAutoCommit)
                        conn.commit();
                    // MS SQL cannot release save points
                    // http://technet.microsoft.com/en-us/library/ms378791.aspx
                    else if (ds.getSystem() != SQLSystem.MSSQL)
                        conn.releaseSavepoint(this.savePoint);
                } catch (Exception e) {
                    if (hasStoppedAutoCommit)
                        conn.rollback();
                    else
                        conn.rollback(this.savePoint);
                    // if the exception wasn't generated by get() the caller must be notified
                    if (!getExn)
                        throw new SQLException("Couldn't " + (hasSavePoint ? "release save point" : "commit"),
                                e);
                } finally {
                    if (hasStoppedAutoCommit)
                        conn.setAutoCommit(true);
                }
            }
        }
    });
}