Example usage for java.sql Connection getAutoCommit

List of usage examples for java.sql Connection getAutoCommit

Introduction

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

Prototype

boolean getAutoCommit() throws SQLException;

Source Link

Document

Retrieves the current auto-commit mode for this Connection object.

Usage

From source file:Main.java

public static void main(String[] args) {
    Connection conn = null;
    Statement stmt = null;//from   w w  w  .ja va2 s  .c o m
    boolean executeResult;
    try {
        String driver = "oracle.jdbc.driver.OracleDriver";
        Class.forName(driver).newInstance();
        System.out.println("Connecting to database...");
        String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL";
        conn = DriverManager.getConnection(jdbcUrl, "yourName", "mypwd");
        stmt = conn.createStatement();
        conn.setAutoCommit(false);
        if (!conn.getAutoCommit())
            System.out.println("Auto-commit is set to false");
        String sql = "INSERT INTO Location VALUES(715,'Houston')";
        stmt.executeUpdate(sql);
        sql = "INSERT INTO Employees VALUES" + "(8,'K','4351',{d '2000-02-00'},715)";
        stmt.executeUpdate(sql);
        conn.commit();
    } catch (SQLException se) {
        String msg = se.getMessage();
        msg = "SQLException occured with message: " + msg;
        System.out.println(msg);
        System.out.println("Starting rollback operations...");
        try {
            conn.rollback();
        } catch (SQLException se2) {
            se2.printStackTrace();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.pinterest.deployservice.db.DatabaseUtil.java

public static void transactionalUpdate(BasicDataSource dataSource, List<UpdateStatement> updateStatements)
        throws Exception {
    QueryRunner queryRunner = new QueryRunner();
    Connection connection = dataSource.getConnection();
    boolean autoStatus = connection.getAutoCommit();
    connection.setAutoCommit(false);//w  w w.j a  v  a2  s.c o  m
    try {
        for (UpdateStatement updateStatement : updateStatements) {
            queryRunner.update(connection, updateStatement.getStatement(), updateStatement.getValueArray());
        }
        connection.commit();
    } catch (SQLException e) {
        connection.rollback();
        throw e;
    } finally {
        connection.setAutoCommit(autoStatus);
        DbUtils.closeQuietly(connection);
    }
}

From source file:org.nuxeo.runtime.datasource.TestDataSourceComponent.java

protected static void checkDataSourceOk(String name, boolean autocommit) throws Exception {
    DataSource ds = DataSourceHelper.getDataSource(name);
    Connection conn = ds.getConnection();
    assertEquals(autocommit, conn.getAutoCommit());
    Statement st = conn.createStatement();
    ResultSet rs = st.executeQuery("SELECT 123");
    assertNotNull(rs);/*from   ww w  . j av  a 2s .c  om*/
    assertTrue(rs.next());
    assertEquals(123, rs.getInt(1));
    st.close();
    conn.close();
}

From source file:com.adaptris.core.util.JdbcUtil.java

/**
 * Commit any pending transactions on the Connection.
 * <p>//from   w  ww.  ja v a2  s. c o  m
 * If {@link Connection#getAutoCommit()} is true, then this operation does nothing.
 * </p>
 *
 * @param sqlConnection the SQL Connection
 * @throws SQLException if the commit fails.
 */
public static void commit(Connection sqlConnection) throws SQLException {
    if (sqlConnection == null) {
        return;
    }
    if (!sqlConnection.getAutoCommit()) {
        sqlConnection.commit();
    }
}

From source file:com.adaptris.core.util.JdbcUtil.java

/**
 * Rollback the connection./*from   w  w w . j av  a  2s  . co m*/
 * <p>
 * If {@link Connection#getAutoCommit()} is true, then this operation does nothing.
 * </p>
 *
 * @param sqlConnection the database connection.
 */
public static void rollback(Connection sqlConnection) {
    if (sqlConnection == null) {
        return;
    }
    try {
        if (sqlConnection.getAutoCommit()) {
            return;
        } else {
            sqlConnection.rollback();
        }
    } catch (Exception ignoredIntentionally) {
    }
}

From source file:com.adaptris.core.util.JdbcUtil.java

/**
 * Rollback to the stored savepoint.//from  www .  ja va 2  s . c o m
 * <p>
 * If {@link Connection#getAutoCommit()} is true, then this operation does nothing.
 * </p>
 *
 * @param svp the savepoint (if null, no rollback occurs).
 * @param sqlConnection the database connection.
 */
public static void rollback(Savepoint svp, Connection sqlConnection) {
    if (sqlConnection == null) {
        return;
    }
    try {
        if (sqlConnection.getAutoCommit()) {
            return;
        }
        if (svp != null) {
            sqlConnection.rollback(svp);
        }
    } catch (Exception ignoredIntentionally) {
    }
}

From source file:com.adaptris.core.util.JdbcUtil.java

/**
 * Create a Savepoint on the connection/*from w w  w. j  av  a  2 s.  c  om*/
 * <p>
 * If {@link Connection#getAutoCommit()} is true, then this operation returns null.
 * </p>
 *
 * @param sqlConnection the SQL Connection
 * @return a created Savepoint or null if the connection does not require it.
 * @throws SQLException if the operation fails.
 */
public static Savepoint createSavepoint(Connection sqlConnection) throws SQLException {
    if (sqlConnection == null) {
        return null;
    }
    if (!sqlConnection.getAutoCommit()) {
        return sqlConnection.setSavepoint();
    }
    return null;
}

From source file:com.hangum.tadpole.engine.manager.TadpoleSQLTransactionManager.java

/**
 * transaction commit/*w w w . j a  va  2  s  .co m*/
 * 
 * @param userId
 * @param userDB
 */
public static void commit(final String userId, final UserDBDAO userDB) {
    if (logger.isDebugEnabled()) {
        logger.debug("=============================================================================");
        logger.debug("\t commit [userId]" + getKey(userId, userDB));
        logger.debug("=============================================================================");
    }

    synchronized (dbManager) {
        TransactionDAO transactionDAO = dbManager.get(getKey(userId, userDB));

        if (transactionDAO != null) {
            Connection conn = transactionDAO.getConn();
            try {
                logger.debug("\tIs auto commit " + conn.getAutoCommit());
                conn.commit();
            } catch (Exception e) {
                logger.error("commit exception", e);
            } finally {
                try {
                    if (conn != null)
                        conn.close();
                } catch (Exception e) {
                }
            }

            removeInstance(userId, userDB);
        }
    } // end synchronized
}

From source file:org.jahia.utils.DatabaseUtils.java

public static void executeStatements(List<String> statements) throws SQLException {
    Connection conn = null;
    try {/*from   w  w w. ja  v  a2 s.c  o m*/
        conn = getDatasource().getConnection();
        DatabaseScripts.executeStatements(statements, conn);
        if (!conn.getAutoCommit()) {
            conn.commit();
        }
    } catch (SQLException e) {
        if (conn != null && !conn.getAutoCommit()) {
            conn.rollback();
        }
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
                // ignore
            }
        }
    }
}

From source file:com.hangum.tadpole.engine.manager.TadpoleSQLTransactionManager.java

/**
 * connection rollback//from  ww w.j  ava2  s  .co m
 * 
 * @param userId
 * @param userDB
 */
public static void rollback(final String userId, final UserDBDAO userDB) {
    if (logger.isDebugEnabled()) {
        logger.debug("=============================================================================");
        logger.debug("\t rollback [userId]" + getKey(userId, userDB));
        logger.debug("=============================================================================");
    }

    synchronized (dbManager) {
        TransactionDAO transactionDAO = dbManager.get(getKey(userId, userDB));

        if (transactionDAO != null) {
            Connection conn = transactionDAO.getConn();
            try {
                if (logger.isDebugEnabled())
                    logger.debug("\tIs auto commit " + conn.getAutoCommit());
                conn.rollback();
            } catch (Exception e) {
                logger.error("rollback exception", e);
            } finally {
                try {
                    if (conn != null)
                        conn.close();
                } catch (Exception e) {
                }
            }

            removeInstance(userId, userDB);
        }
    } // end synchronized
}