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:org.jboss.dashboard.database.ExternalDataSource.java

protected boolean getAutoCommit(Connection conn) {
    try {//from   w w  w . j ava  2  s.co  m
        return conn.getAutoCommit();
    } catch (SQLException e) {
        // Ignore problems when trying to get autocommit.
        // In some environments (Presidencia - Informix) when trying to get autocommit an exception is thrown.
        log.debug("Can not get autocommit for datasource: " + name, e);
        return true;
    }
}

From source file:org.mangocube.corenut.commons.db.JdbcTemplate.java

private Connection getConnection() throws SQLException {
    Connection con = conProvider.getConnection();
    if (con != null && con.getAutoCommit()) {
        con.setAutoCommit(false);//w w w.java  2 s.  co  m
    }
    return con;
}

From source file:net.sourceforge.vulcan.spring.jdbc.JdbcSchemaMigrator.java

private void executeSql(final Resource resource) throws IOException {
    final String text = loadResource(resource);
    log.info("Running migration script " + resource.getFilename());

    final String[] commands = text.split(";");

    for (String command : commands) {
        final String trimmed = command.trim();
        if (StringUtils.isBlank(trimmed)) {
            continue;
        }/*from  w w  w. j ava  2 s.co  m*/

        jdbcTemplate.execute(new StatementCallback() {
            public Object doInStatement(Statement stmt) throws SQLException, DataAccessException {
                stmt.execute(trimmed);
                final Connection conn = stmt.getConnection();
                if (!conn.getAutoCommit()) {
                    conn.commit();
                }
                return null;
            }
        });
    }
}

From source file:org.openestate.tool.helloworld.db.hsql.HSqlDbHelloWorldHandler.java

@Override
public void saveObject(Connection c, DbHelloWorldObject object) throws SQLException {
    final boolean oldAutoCommit = c.getAutoCommit();
    NamedCallableStatement saveStatement = null;
    try {/*ww w .j ava  2  s  . co m*/
        c.setAutoCommit(false);
        saveStatement = new NamedCallableStatement(c,
                "CALL " + PROC_SAVE_HELLOWORLD + "(" + ":" + FIELD_HELLOWORLD_ID + ", " + ":"
                        + FIELD_HELLOWORLD_NAME + ", " + ":" + FIELD_HELLOWORLD_NOTES + ", " + ":"
                        + FIELD_ACCESS_OWNER_ID + ", " + ":" + FIELD_ACCESS_GROUP_ID + ", " + ":"
                        + FIELD_ACCESS_PERMISSIONS + ");");
        saveStatement.setLong(FIELD_HELLOWORLD_ID, object.id);
        saveStatement.setString(FIELD_HELLOWORLD_NOTES, object.notes);
        saveStatement.setString(FIELD_HELLOWORLD_NAME, StringUtils.abbreviate(object.name, 100));
        saveStatement.setLong(FIELD_ACCESS_OWNER_ID, object.ownerUserId);
        saveStatement.setLong(FIELD_ACCESS_GROUP_ID, object.ownerGroupId);
        saveStatement.setInt(FIELD_ACCESS_PERMISSIONS,
                (object.permission != null) ? object.permission.getValue() : -1);
        saveStatement.execute();

        // remember ID of the created / update row
        final long referencedId = (object.id < 1) ? saveStatement.getLong(FIELD_HELLOWORLD_ID) : object.id;
        if (referencedId < 1)
            throw new SQLException("Can't determine ID of the saved object!");

        c.commit();

        // store ID of into object
        if (object.id <= 0) {
            object.id = referencedId;
        }
    } catch (SQLException ex) {
        c.rollback();
        throw ex;
    } finally {
        JdbcUtils.closeQuietly(saveStatement);
        c.setAutoCommit(oldAutoCommit);
    }
}

From source file:com.stratelia.silverpeas.silverstatistics.control.SilverStatisticsService.java

/**
 * @param type/*from   www. j  a v a 2  s  .  c  om*/
 * @param data
 */
@Override
public void putStats(StatType type, String data) {
    StrTokenizer stData = new StrTokenizer(data, SEPARATOR);
    List<String> dataArray = stData.getTokenList();
    if (myStatsConfig.isGoodDatas(type, dataArray)) {
        Connection myCon = DBUtil.makeConnection(SILVERSTATISTICS_DATASOURCE);
        try {
            SilverStatisticsDAO.putDataStats(myCon, type, dataArray, myStatsConfig);
            if (!myCon.getAutoCommit()) {
                myCon.commit();
            }
        } catch (SQLException e) {
            SilverTrace.error("silverstatistics", "SilverStatisticsEJB.putStats",
                    "silverstatistics.MSG_ALIMENTATION_BD",
                    "typeOfStats = " + type + ", dataArray = " + dataArray, e);
        } catch (StatisticsRuntimeException e) {
            SilverTrace.error("silverstatistics", "SilverStatisticsEJB.putStats", "MSG_CONNECTION_BD");
        } finally {
            DBUtil.close(myCon);
        }

    } else {
        SilverTrace.error("silverstatistics", "SilverStatisticsEJB.putStats", "MSG_CONFIG_DATAS",
                "data en entree=" + data + " pour " + type);
    }
}

From source file:kenh.xscript.database.elements.Rollback.java

public void process(@Attribute(ATTRIBUTE_REF) java.sql.Connection conn,
        @Attribute(ATTRIBUTE_SAVE_POINT) java.sql.Savepoint savepoint) throws UnsupportedScriptException {
    try {/*w w  w .  ja va 2  s.  c  om*/
        if (!conn.isClosed()) {
            if (!conn.getAutoCommit()) {
                if (savepoint != null) {
                    conn.rollback(savepoint);
                } else {
                    conn.rollback();
                }
            }
        }
    } catch (Exception e) {
        throw new UnsupportedScriptException(this, e);
    }
}

From source file:org.openestate.tool.helloworld.db.hsql.HSqlDbHelloWorldHandler.java

@Override
public void removeObjects(Connection c, long[] ids) throws SQLException {
    if (ids.length < 1)
        return;//w w  w.j a  va 2 s . c o m
    final boolean oldAutoCommit = c.getAutoCommit();
    CallableStatement removeStatement = null;
    try {
        c.setAutoCommit(false);
        removeStatement = c.prepareCall("CALL " + PROC_REMOVE_HELLOWORLD + "(?);");
        for (long id : ids) {
            removeStatement.clearParameters();
            removeStatement.setLong(1, id);
            removeStatement.execute();
        }
        c.commit();
    } catch (SQLException ex) {
        c.rollback();
        throw ex;
    } finally {
        JdbcUtils.closeQuietly(removeStatement);
        c.setAutoCommit(oldAutoCommit);
    }
}

From source file:org.wso2.carbon.identity.authorization.core.dao.GenericDAO.java

public void save(Connection connection, boolean commit) throws UserStoreException {

    log.debug(this.toString());
    try {/*from   w ww .j a  v a  2s . co m*/
        if (connection.getAutoCommit()) {
            connection.setAutoCommit(false);
        }

        deleteObjects(connection);
        if (status == JDBCConstantsDAO.INSERT) {
            insert(connection, false);
        } else if (status == JDBCConstantsDAO.UPDATE) {
            update(connection, false);
        } else if (status == JDBCConstantsDAO.DELETE) {
            delete(connection, false);
        }
        saveDependentModules(connection, false);
        if (commit) {
            connection.commit();
        }

    } catch (SQLException e) {
        String error = "Error while setting the connection to autocommit false ";
        log.error(error, e);
        try {
            connection.rollback();
        } catch (SQLException e1) {
            log.error(e);
        }
        throw new UserStoreException(error, e);
    } finally {
        if (commit) {
            DatabaseUtil.closeConnection(connection);
        }
    }
}

From source file:corner.migration.impl.DBMigrationInitializerTest.java

private void executeSQL(String... sql) {
    try {//from   w  ww.  j  a v a2s.  c om
        Connection con = this.factory.getSettings().getConnectionProvider().getConnection();
        if (sql != null && sql.length > 0) {
            boolean oldAutoCommit = con.getAutoCommit();
            if (!oldAutoCommit) {
                con.setAutoCommit(true);
            }
            try {
                Statement stmt = con.createStatement();
                try {
                    for (int i = 0; i < sql.length; i++) {
                        executeSchemaStatement(stmt, sql[i]);
                    }
                } finally {
                    JdbcUtils.closeStatement(stmt);
                }
            } finally {
                if (!oldAutoCommit) {
                    con.setAutoCommit(false);
                }
            }
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.wso2.carbon.apimgt.hybrid.gateway.common.dao.OnPremiseGatewayDAO.java

/**
 * Adds an API publish/re-publish event to the database
 *
 * @param tenantDomain tenant domain//  www .  j av  a  2  s  .c o m
 * @param apiId        API identifier
 * @throws OnPremiseGatewayException OnPremiseGatewayException
 */
public void addAPIPublishEvent(String tenantDomain, String apiId) throws OnPremiseGatewayException {
    if (log.isDebugEnabled()) {
        log.debug("Adding publish/re-publish event of API: " + apiId);
    }
    Connection conn = null;
    PreparedStatement ps = null;
    boolean isAutoCommitEnabled = false;
    try {
        conn = APIMgtDBUtil.getConnection();
        isAutoCommitEnabled = conn.getAutoCommit();
        conn.setAutoCommit(false);
        ps = conn.prepareStatement(insertIntoAPIPublishEvents);
        ps.setString(1, tenantDomain);
        ps.setString(2, apiId);
        ps.setTimestamp(3, new Timestamp(System.currentTimeMillis()));
        ps.executeUpdate();
        conn.commit();
    } catch (SQLException e) {
        throw new OnPremiseGatewayException("Failed to insert publish/re-publish event of the API " + apiId
                + " of the tenant domain " + tenantDomain, e);
    } finally {
        try {
            conn.setAutoCommit(isAutoCommitEnabled);
        } catch (SQLException e) {
            log.warn("Failed to reset auto commit state of database connection to the previous state."
                    + tenantDomain, e);
        }
        APIMgtDBUtil.closeAllConnections(ps, conn, null);
    }
}