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.springframework.jdbc.datasource.DataSourceUtils.java

/**
 * Reset the given Connection after a transaction,
 * regarding read-only flag and isolation level.
 * @param con the Connection to reset/*  w  ww.j  a v  a  2  s .  c om*/
 * @param previousIsolationLevel the isolation level to restore, if any
 * @see #prepareConnectionForTransaction
 */
public static void resetConnectionAfterTransaction(Connection con, @Nullable Integer previousIsolationLevel) {
    Assert.notNull(con, "No Connection specified");
    try {
        // Reset transaction isolation to previous value, if changed for the transaction.
        if (previousIsolationLevel != null) {
            if (logger.isDebugEnabled()) {
                logger.debug("Resetting isolation level of JDBC Connection [" + con + "] to "
                        + previousIsolationLevel);
            }
            con.setTransactionIsolation(previousIsolationLevel);
        }

        // Reset read-only flag.
        if (con.isReadOnly()) {
            if (logger.isDebugEnabled()) {
                logger.debug("Resetting read-only flag of JDBC Connection [" + con + "]");
            }
            con.setReadOnly(false);
        }
    } catch (Throwable ex) {
        logger.debug("Could not reset JDBC Connection after transaction", ex);
    }
}

From source file:com.zimbra.cs.db.DbPool.java

public static DbConnection getConnection(Mailbox mbox) throws ServiceException {
    if (!isInitialized()) {
        throw ServiceException.FAILURE("Database connection pool not initialized.", null);
    }//from w w  w .  j  a v a 2s  . c  o m
    Integer mboxId = mbox != null ? mbox.getId() : -1; //-1 == zimbra db and/or initialization where mbox isn't known yet
    try {
        Db.getInstance().preOpen(mboxId);
        long start = ZimbraPerf.STOPWATCH_DB_CONN.start();

        // If the connection pool is overutilized, warn about potential leaks
        PoolingDataSource pool = getPool();
        checkPoolUsage();

        Connection dbconn = null;
        DbConnection conn = null;
        try {
            dbconn = pool.getConnection();

            if (dbconn.getAutoCommit() != false)
                dbconn.setAutoCommit(false);

            // We want READ COMMITTED transaction isolation level for duplicate
            // handling code in BucketBlobStore.newBlobInfo().
            if (Db.supports(Db.Capability.READ_COMMITTED_ISOLATION))
                dbconn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);

            conn = new DbConnection(dbconn, mboxId);
            Db.getInstance().postOpen(conn);
        } catch (SQLException e) {
            try {
                if (dbconn != null && !dbconn.isClosed())
                    dbconn.close();
            } catch (SQLException e2) {
                ZimbraLog.sqltrace.warn("DB connection close caught exception", e);
            }
            throw ServiceException.FAILURE("getting database connection", e);
        }

        // If we're debugging, update the counter with the current stack trace
        if (ZimbraLog.dbconn.isDebugEnabled()) {
            Throwable t = new Throwable();
            conn.setStackTrace(t);

            String stackTrace = SystemUtil.getStackTrace(t);
            synchronized (sConnectionStackCounter) {
                sConnectionStackCounter.increment(stackTrace);
            }
        }
        if (mbox != null)
            Db.registerDatabaseInterest(conn, mbox);

        ZimbraPerf.STOPWATCH_DB_CONN.stop(start);
        return conn;
    } catch (ServiceException se) {
        //if connection open fails unlock
        Db.getInstance().abortOpen(mboxId);
        throw se;
    }
}

From source file:org.wso2.carbon.bpel.b4p.coordination.dao.jpa.AbstractJPAVendorAdapter.java

protected Connection getDBConnection() throws SQLException {
    Connection c = dataSource.getConnection();
    c.setTransactionIsolation(2);
    return c;//from  www .  j  a v  a2s  .  c  om
}

From source file:org.jumpmind.db.sql.JdbcSqlTemplate.java

public static void close(boolean autoCommitValue, int transactionIsolationLevel, Connection c) {
    try {/*w w w  . j  a v  a  2s  . c o m*/
        if (c != null) {
            c.setAutoCommit(autoCommitValue);
            if (c.getTransactionIsolation() != transactionIsolationLevel) {
                c.setTransactionIsolation(transactionIsolationLevel);
            }
        }
    } catch (Throwable ex) {
    } finally {
        close(c);
    }
}

From source file:org.wso2.carbon.attachment.mgt.core.dao.impl.jpa.AbstractJPAVendorAdapter.java

/**
 * Returns the database connection from data-source
 * @return//from   w ww  . j  a va  2s. co m
 * @throws SQLException
 */
protected Connection getDBConnection() throws SQLException {
    Connection c = dataSource.getConnection();
    c.setTransactionIsolation(2);
    return c;
}

From source file:hermes.store.jdbc.JDBCConnectionPool.java

protected Connection makeObject() throws HermesException {
    try {//from   www .ja va 2  s.c om
        Connection connection = DriverManager.getConnection(url);

        connection.setAutoCommit(autoCommit);
        connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
        return connection;
    } catch (Exception e) {
        log.error(e.getMessage(), e);

        throw new HermesException(e);
    }
}

From source file:org.wso2.carbon.identity.thrift.authentication.internal.persistance.ThriftAuthenticationJDBCPersistenceManager.java

/**
 * Returns an database connection for Identity data source.
 *
 * @return Database connection/*from w w w  .  j  a va  2 s  .c  o m*/
 * @throws AuthenticationException Exception occurred when getting the data source.
 */
public Connection getDBConnection() throws AuthenticationException {
    try {
        Connection dbConnection = dataSource.getConnection();
        dbConnection.setAutoCommit(false);
        dbConnection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
        return dbConnection;
    } catch (SQLException e) {
        String errMsg = "Error when getting a database connection object from the Thrift Authentication data source.";
        log.error(errMsg, e);
        throw new AuthenticationException(errMsg);
    }
}

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

/**
 * Returns an database connection for Identity data source.
 *
 * @return Database connection/*  w ww  . j a  v  a2 s. c om*/
 * @throws IdentityException Exception occurred when getting the data source.
 */
public Connection getDBConnection() throws IdentityRuntimeException {
    try {
        Connection dbConnection = dataSource.getConnection();
        dbConnection.setAutoCommit(false);
        dbConnection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
        return dbConnection;
    } catch (SQLException e) {
        String errMsg = "Error when getting a database connection object from the Identity data source.";
        throw new IdentityRuntimeException(errMsg, e);
    }
}

From source file:org.sonar.db.version.BaseDataChange.java

/**
 * Do not forget to close it !//from  ww w .ja  v a 2  s.c om
 */
protected Connection openConnection() throws SQLException {
    Connection connection = db.getDataSource().getConnection();
    connection.setAutoCommit(false);
    if (connection.getMetaData().supportsTransactionIsolationLevel(Connection.TRANSACTION_READ_UNCOMMITTED)) {
        connection.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
    }
    return connection;
}

From source file:org.sonar.server.database.JndiDatabaseConnector.java

public Connection getConnection() throws SQLException {
    if (datasource != null) {
        Connection connection = datasource.getConnection();
        if (getTransactionIsolation() != null) {
            connection.setTransactionIsolation(getTransactionIsolation());
        }/*w w w.  j  a va  2 s .  c o  m*/
        return connection;
    }
    return null;
}