Example usage for java.sql Connection setTransactionIsolation

List of usage examples for java.sql Connection setTransactionIsolation

Introduction

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

Prototype

void setTransactionIsolation(int level) throws SQLException;

Source Link

Document

Attempts to change the transaction isolation level for this Connection object to the one given.

Usage

From source file:org.apache.sqoop.mapreduce.db.SQLServerDBRecordReader.java

/**
 * Configure the provided Connection for record reads.
 *///from w  ww  .j ava2s  .co m
protected void configureConnection(Connection conn) throws IOException {
    try {
        conn.setAutoCommit(false);
        conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
    } catch (SQLException sqlEx) {
        LOG.error("Failed to configure SQL Connection");
        throw new IOException(sqlEx);
    }
}

From source file:iudex.da.ContentUpdater.java

public void update(List<UniMap> references) throws SQLException {
    Connection conn = dataSource().getConnection();
    try {/*from   w ww  . ja v  a2 s . c o  m*/
        conn.setAutoCommit(false);
        conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);

        update(references, conn);

        conn.commit();
    } finally {
        if (conn != null)
            conn.close();
    }
}

From source file:net.sf.jasperreports.data.hibernate.HibernateConnectionProvider.java

@Override
public Connection getConnection() throws SQLException {

    if (log.isTraceEnabled())
        log.trace("total checked-out connections: " + checkedOut);

    synchronized (pool) {
        if (!pool.isEmpty()) {
            int last = pool.size() - 1;
            if (log.isTraceEnabled()) {
                log.trace("using pooled JDBC connection, pool size: " + last);
                checkedOut++;/*from   w  w  w  .  j  av  a 2 s  . c  o m*/
            }
            Connection pooled = pool.remove(last);
            if (isolation != null)
                pooled.setTransactionIsolation(isolation.intValue());
            if (pooled.getAutoCommit() != autocommit)
                pooled.setAutoCommit(autocommit);
            return pooled;
        }
    }

    log.debug("opening new JDBC connection");
    Connection conn = driver.connect(url, connectionProps);
    if (isolation != null)
        conn.setTransactionIsolation(isolation.intValue());
    if (conn.getAutoCommit() != autocommit)
        conn.setAutoCommit(autocommit);

    if (log.isDebugEnabled()) {
        log.debug("created connection to: " + url + ", Isolation Level: " + conn.getTransactionIsolation());
    }
    if (log.isTraceEnabled())
        checkedOut++;

    return conn;
}

From source file:iudex.da.ContentUpdater.java

/**
 * Update first any content REFERENCES and then the content itself.
 *///  w  w  w .  ja  v a  2 s .  c  o m
public void update(UniMap content) throws SQLException {
    Connection conn = dataSource().getConnection();
    try {
        conn.setAutoCommit(false);
        conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
        //FIXME: Correct isolation?

        List<UniMap> refs = content.get(ContentKeys.REFERENCES);
        if (refs != null) {
            update(refs, conn);
        }

        UniMap referer = content.get(ContentKeys.REFERER);
        if (referer != null) {
            //FIXME: Really sufficient as same path as content?
            update(referer, conn);
        }

        update(content, conn);

        conn.commit();
    } finally {
        if (conn != null)
            conn.close();
    }
}

From source file:org.wso2.carbon.identity.application.common.persistence.JDBCPersistenceManager.java

/**
 * Returns a database connection for Identity Application Management data store.
 *
 * @return Database connection/*from www. j  ava 2  s  .  co m*/
 * @throws IdentityApplicationManagementException Error when getting DB connection
 *         on the Identity Provider Management data source
 */
public Connection getDBConnection() throws IdentityApplicationManagementException {

    Connection dbConnection = null;
    try {
        dbConnection = dataSource.getConnection();
        if (dbConnection.getTransactionIsolation() != Connection.TRANSACTION_READ_COMMITTED) {
            dbConnection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
        }
        dbConnection.setAutoCommit(false);
        return dbConnection;
    } catch (SQLException e) {
        String errorMsg = "Error occurred while trying to get a database connection on "
                + "Identity Application Management data source";
        log.error(errorMsg, e);
        if (dbConnection != null) {
            IdentityApplicationManagementUtil.closeConnection(dbConnection);
        }
        throw new IdentityApplicationManagementException(errorMsg);
    }
}

From source file:org.wso2.carbon.identity.user.store.count.jdbc.internal.InternalCountRetriever.java

private Connection getDBConnection() throws SQLException, UserStoreException {

    Connection dbConnection = IdentityDatabaseUtil.getUserDBConnection();
    if (dbConnection == null) {
        throw new UserStoreException("Could not create a database connection to User database");
    }//w w  w . j av a2  s.  c  om
    dbConnection.setAutoCommit(false);
    dbConnection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
    return dbConnection;
}

From source file:org.sonar.server.db.migrations.BaseDataChange.java

@Override
public final void execute() throws SQLException {
    Connection readConnection = null, writeConnection = null;
    try {//  ww w  . jav  a2  s. c  o m
        readConnection = db.getDataSource().getConnection();
        readConnection.setAutoCommit(false);
        if (readConnection.getMetaData()
                .supportsTransactionIsolationLevel(Connection.TRANSACTION_READ_UNCOMMITTED)) {
            readConnection.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
        }

        writeConnection = db.getDataSource().getConnection();
        writeConnection.setAutoCommit(false);
        Context context = new Context(db, readConnection, writeConnection);
        execute(context);

    } finally {
        DbUtils.closeQuietly(readConnection);
        DbUtils.closeQuietly(writeConnection);
    }
}

From source file:org.artifactory.storage.db.util.JdbcHelper.java

/**
 * return coneection isolation level back to read committed
 *
 * @param allowDirtyReads/*  w  w w  .  java 2  s.co  m*/
 * @param con             sql connection
 * @throws SQLException
 */
private void updateConnectionIsolationLevelToReadCommitted(boolean allowDirtyReads, Connection con)
        throws SQLException {
    if (allowDirtyReads && con != null) {
        con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
    }
}

From source file:org.rimudb.pool.DBCPConnectionManager.java

public Connection getDatabaseConnection() throws SQLException {
    Connection connection = DriverManager.getConnection("jdbc:apache:commons:dbcp:" + dbConfig.getDatabaseID());
    // Set the default transaction isolation level
    if (poolConfiguration.getDefaultTransactionIsolation() != TransactionIsolation.TRANSACTION_NOT_SPECIFIED) {
        connection.setTransactionIsolation(poolConfiguration.getDefaultTransactionIsolation().getJdbcValue());
    }// ww  w.  ja  v a  2 s  . c o m
    // Set the default auto-commit value
    if (poolConfiguration.getDefaultAutoCommit() != AutoCommit.AUTO_COMMIT_NONE) {
        connection.setAutoCommit(poolConfiguration.getDefaultAutoCommit().getJdbcValue());
    }

    return connection;
}

From source file:org.artifactory.storage.db.util.JdbcHelper.java

/**
 * @return A transaction aware connection
 *//*from  www .ja v a2  s .c  om*/
private Connection getConnection() throws SQLException {
    Connection connection = DataSourceUtils.doGetConnection(dataSource);
    if (Connection.TRANSACTION_READ_COMMITTED != connection.getTransactionIsolation()) {
        connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
    }
    return connection;
}