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.wso2.carbon.repository.core.jdbc.dao.JDBCResourceDAO.java

public void fillChildren(CollectionImpl collection, DataAccessManager dataAccessManager)
        throws RepositoryException {
    if (Transaction.isStarted()) {
        fillChildren(collection, 0, -1, JDBCDatabaseTransaction.getConnection());
    } else {/*from  w  w w.j a  v  a  2 s .c o m*/
        Connection conn = null;
        boolean transactionSucceeded = false;
        try {
            if (!(dataAccessManager instanceof JDBCDataAccessManager)) {
                String msg = "Failed to fill children. Invalid data access manager.";
                log.error(msg);
                throw new RepositoryException(msg);
            }
            conn = ((JDBCDataAccessManager) dataAccessManager).getDataSource().getConnection();

            // If a managed connection already exists, use that instead of a new
            // connection.
            JDBCDatabaseTransaction.ManagedRegistryConnection temp = JDBCDatabaseTransaction
                    .getManagedRegistryConnection(conn);
            if (temp != null) {
                conn.close();
                conn = temp;
            }

            if (conn.getTransactionIsolation() != Connection.TRANSACTION_READ_COMMITTED) {
                conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
            }
            conn.setAutoCommit(false);

            fillChildren(collection, 0, -1, conn);
            transactionSucceeded = true;
        } catch (SQLException e) {

            String msg = "Failed to get child paths of " + collection.getPath() + ". " + e.getMessage();
            log.error(msg, e);
            throw new RepositoryException(msg, e);

        } finally {
            if (transactionSucceeded) {
                try {
                    conn.commit();
                } catch (SQLException e) {
                    log.error("Failed to commit the database connection used in "
                            + "getting child paths of the collection " + collection.getPath());
                }
            } else if (conn != null) {
                try {
                    conn.rollback();
                } catch (SQLException e) {
                    log.error("Failed to rollback the database connection used in "
                            + "getting child paths of the collection " + collection.getPath());
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    log.error("Failed to close the database connection opened in "
                            + "getting the child paths of " + collection.getPath(), e);
                }
            }
        }
    }
}

From source file:org.wso2.carbon.repository.core.jdbc.dao.JDBCResourceDAO.java

public int getChildCount(CollectionImpl collection, DataAccessManager dataAccessManager)
        throws RepositoryException {
    int childCount = -1;
    if (Transaction.isStarted()) {
        childCount = getChildCount(collection, JDBCDatabaseTransaction.getConnection());
    } else {/*  w  ww.  jav a2s  . c  om*/
        Connection conn = null;
        boolean transactionSucceeded = false;
        try {
            if (!(dataAccessManager instanceof JDBCDataAccessManager)) {
                String msg = "Failed to get child count. Invalid data access manager.";
                log.error(msg);
                throw new RepositoryException(msg);
            }
            conn = ((JDBCDataAccessManager) dataAccessManager).getDataSource().getConnection();

            // If a managed connection already exists, use that instead of a new
            // connection.
            JDBCDatabaseTransaction.ManagedRegistryConnection temp = JDBCDatabaseTransaction
                    .getManagedRegistryConnection(conn);
            if (temp != null) {
                conn.close();
                conn = temp;
            }
            if (conn.getTransactionIsolation() != Connection.TRANSACTION_READ_COMMITTED) {
                conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
            }
            conn.setAutoCommit(false);

            childCount = getChildCount(collection, conn);
            transactionSucceeded = true;
        } catch (SQLException e) {
            String msg = "Failed to get the child count of resource " + collection.getPath() + ". "
                    + e.getMessage();
            log.error(msg, e);
            throw new RepositoryException(msg, e);
        } finally {
            if (transactionSucceeded) {
                try {
                    conn.commit();
                } catch (SQLException e) {
                    log.error("Failed to commit the database connection used in "
                            + "getting child count of the collection " + collection.getPath());
                }
            } else if (conn != null) {
                try {
                    conn.rollback();
                } catch (SQLException e) {
                    log.error("Failed to rollback the database connection used in "
                            + "getting child count of the collection " + collection.getPath());
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    log.error("Failed to close the database connection used in "
                            + "getting child count of collection " + collection.getPath());
                }
            }
        }
    }
    return childCount;
}

From source file:org.wso2.carbon.repository.core.jdbc.dao.JDBCResourceDAO.java

public String[] getChildren(CollectionImpl collection, int start, int pageLen,
        DataAccessManager dataAccessManager) throws RepositoryException {
    String[] childPaths = null;/*from  w  w  w .  j av  a 2 s  . co m*/

    if (Transaction.isStarted()) {
        childPaths = getChildren(collection, start, pageLen, JDBCDatabaseTransaction.getConnection());
    } else {
        Connection conn = null;
        boolean transactionSucceeded = false;
        try {
            if (!(dataAccessManager instanceof JDBCDataAccessManager)) {
                String msg = "Failed to get children. Invalid data access manager.";
                log.error(msg);
                throw new RepositoryDBException(msg);
            }
            conn = ((JDBCDataAccessManager) dataAccessManager).getDataSource().getConnection();

            // If a managed connection already exists, use that instead of a new
            // connection.
            JDBCDatabaseTransaction.ManagedRegistryConnection temp = JDBCDatabaseTransaction
                    .getManagedRegistryConnection(conn);

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

            if (conn.getTransactionIsolation() != Connection.TRANSACTION_READ_COMMITTED) {
                conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
            }

            conn.setAutoCommit(false);

            childPaths = getChildren(collection, start, pageLen, conn);
            transactionSucceeded = true;
        } catch (SQLException e) {

            String msg = "Failed to get the child paths " + pageLen + " child paths from " + start
                    + " of resource " + collection.getPath() + ". " + e.getMessage();
            log.error(msg, e);
            throw new RepositoryDBException(msg, e);

        } finally {
            if (transactionSucceeded) {
                try {
                    conn.commit();
                } catch (SQLException e) {
                    log.error("Failed to commit the database connection used in "
                            + "getting child paths of the collection " + collection.getPath());
                }
            } else if (conn != null) {
                try {
                    conn.rollback();
                } catch (SQLException e) {
                    log.error("Failed to rollback the database connection used in "
                            + "getting child paths of the collection " + collection.getPath());
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    log.error("Failed to close the database connection used in "
                            + "getting child paths of the collection " + collection.getPath());
                }
            }
        }
    }
    return childPaths;
}

From source file:org.apache.hadoop.hive.metastore.txn.TxnHandler.java

private Connection getDbConn(int isolationLevel, DataSource connPool) throws SQLException {
    int rc = doRetryOnConnPool ? 10 : 1;
    Connection dbConn = null;
    while (true) {
        try {/*from  ww w. j  a  va  2 s  . c  o  m*/
            dbConn = connPool.getConnection();
            dbConn.setAutoCommit(false);
            dbConn.setTransactionIsolation(isolationLevel);
            return dbConn;
        } catch (SQLException e) {
            closeDbConn(dbConn);
            if ((--rc) <= 0)
                throw e;
            LOG.error("There is a problem with a connection from the pool, retrying(rc=" + rc + "): "
                    + getMessage(e), e);
        }
    }
}

From source file:org.quartz.impl.jdbcjobstore.JobStoreSupport.java

protected Connection getConnection() throws JobPersistenceException {
    Connection conn = null;
    try {//from  ww  w .  j a  va 2s.  co  m
        conn = DBConnectionManager.getInstance().getConnection(getDataSource());
    } catch (SQLException sqle) {
        throw new JobPersistenceException(
                "Failed to obtain DB connection from data source '" + getDataSource() + "': " + sqle.toString(),
                sqle);
    } catch (Throwable e) {
        throw new JobPersistenceException(
                "Failed to obtain DB connection from data source '" + getDataSource() + "': " + e.toString(), e,
                JobPersistenceException.ERR_PERSISTENCE_CRITICAL_FAILURE);
    }

    if (conn == null) {
        throw new JobPersistenceException("Could not get connection from DataSource '" + getDataSource() + "'");
    }

    // Protect connection attributes we might change.
    conn = getAttributeRestoringConnection(conn);

    // Set any connection connection attributes we are to override.
    try {
        if (!isDontSetAutoCommitFalse()) {
            conn.setAutoCommit(false);
        }

        if (isTxIsolationLevelSerializable()) {
            conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
        }
    } catch (SQLException sqle) {
        getLog().warn("Failed to override connection auto commit/transaction isolation.", sqle);
    } catch (Throwable e) {
        try {
            conn.close();
        } catch (Throwable tt) {
        }

        throw new JobPersistenceException("Failure setting up connection.", e);
    }

    return conn;
}

From source file:org.apache.hadoop.hive.metastore.MyXid.java

public static DBRouterInfo getDBRouter(String db) throws MetaException {
    Connection con = null;
    ;/*from ww w.ja v a 2s  .  c  om*/
    Statement ps = null;

    DBRouterInfo route = null;

    db = db.toLowerCase();

    try {
        con = getGlobalConnection();
    } catch (MetaStoreConnectException e1) {
        LOG.error("get db router error, user=" + user + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("get db router error, user=" + user + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    try {
        con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
        ps = con.createStatement();

        String sql = "select db_name, seg_addr, secondary_seg_addr, is_db_split, "
                + "describe from router where db_name='" + db + "'";

        ResultSet rs = ps.executeQuery(sql);

        while (rs.next()) {
            route = new DBRouterInfo();
            route.setDBName(rs.getString(1));
            route.setSegmentDBUrl(rs.getString(2));
            route.setSecondarySegmentDBUrl(rs.getString(3));
            route.setHasTableRouter(rs.getBoolean(4));
            route.setDetail(rs.getString(5));

            LOG.debug("db name is " + route.getDBName() + "\n" + " segment addr is " + route.getSegmentDBUrl()
                    + "\n" + " second segment addr is " + route.getSecondarySlaveDBUrl() + "\n"
                    + " is has table route is " + route.getHasTableRouter() + "\n" + " detail is "
                    + route.getDetail());
        }

        rs.close();
    } catch (SQLException sqlex) {
        LOG.error("get user error, user=" + user + ", msg=" + sqlex.getMessage());
        sqlex.printStackTrace();
        throw new MetaException(sqlex.getMessage());
    } finally {
        closeStatement(ps);
        closeConnection(con);
    }

    return route;
}

From source file:org.apache.hadoop.hive.metastore.MyXid.java

@Override
public List<String> getRolesAll() throws MetaException {
    Connection con = null;
    ;//from  w  w  w  .ja v a2  s.  c  o  m
    Statement ps = null;
    List<String> roles = new ArrayList<String>();

    try {
        con = getGlobalConnection();
    } catch (MetaStoreConnectException e1) {
        LOG.error("get roles error,  msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("get roles error,  msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    try {
        con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
        ps = con.createStatement();

        String sql = "select  role_name from tdwrole";

        ResultSet roleSet = ps.executeQuery(sql);

        while (roleSet.next()) {
            roles.add(roleSet.getString(1));
        }
    } catch (SQLException sqlex) {
        LOG.error("get roles error,  msg=" + sqlex.getMessage());
        sqlex.printStackTrace();
        throw new MetaException(sqlex.getMessage());
    } finally {
        closeStatement(ps);
        closeConnection(con);
    }

    return roles;
}

From source file:org.apache.hadoop.hive.metastore.MyXid.java

@Override
public List<String> getDatabases() throws MetaException {
    Connection con;
    Statement stmt = null;/*from  w  w w . j av a 2  s  . com*/

    List<String> dbNameList = new ArrayList<String>();

    try {
        con = getGlobalConnection();
    } catch (MetaStoreConnectException e1) {
        LOG.error("get databases error msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("get databases error msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    try {
        con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);

        stmt = con.createStatement();
        String sql = "select db_name, owner from router";
        ResultSet ret = stmt.executeQuery(sql);

        dbNameList = new ArrayList<String>();

        while (ret.next()) {
            dbNameList.add(ret.getString(1));
        }
        ret.close();
    } catch (SQLException x) {
        x.printStackTrace();
        try {
            con.rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        LOG.error("get databases error msg=" + x.getMessage());
        throw new MetaException(x.getMessage());
    } finally {
        closeStatement(stmt);
        closeConnection(con);
    }

    return dbNameList;
}

From source file:org.apache.hadoop.hive.metastore.MyXid.java

@Override
public Database getDatabase(String name) throws NoSuchObjectException, MetaException {
    Database db = null;//from w w w  .  ja  v  a2s.c  o  m
    Connection con;
    name = name.toLowerCase();

    try {
        con = getSegmentConnection(name);
    } catch (MetaStoreConnectException e1) {
        LOG.error("get database error, db=" + name + ", msg=" + e1.getMessage());
        throw new NoSuchObjectException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("get database error, db=" + name + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    Statement stmt = null;

    try {
        con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);

        stmt = con.createStatement();
        String sql = "SELECT name, hdfs_schema, description, owner FROM DBS WHERE name='" + name + "'";

        ResultSet dbSet = stmt.executeQuery(sql);
        boolean isDBFind = false;

        while (dbSet.next()) {
            isDBFind = true;
            db = new Database();
            db.setName(dbSet.getString(1));
            db.setHdfsscheme(dbSet.getString(2));
            db.setDescription(dbSet.getString(3));
            db.setOwner(dbSet.getString(4));
            break;
        }

        dbSet.close();

        if (!isDBFind) {
            LOG.error("get database error, db=" + name);
            throw new NoSuchObjectException("database " + name + " does not exist!");
        }
    } catch (SQLException sqlex) {
        sqlex.printStackTrace();
        throw new MetaException(sqlex.getMessage());
    } finally {
        closeStatement(stmt);
        closeConnection(con);
    }

    return db;
}

From source file:org.apache.hadoop.hive.metastore.MyXid.java

@Override
public List<String> getDatabasesWithOwner(String owner) throws MetaException {
    Connection con;
    Statement stmt = null;/* w ww . jav a 2 s  .  com*/
    owner = owner.toLowerCase();

    List<String> dbNameList = new ArrayList<String>();

    try {
        con = getGlobalConnection();
    } catch (MetaStoreConnectException e1) {
        LOG.error("get databases error msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("get databases error msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    try {
        con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);

        stmt = con.createStatement();
        String sql = "select db_name, owner from router where owner='" + owner + "'";
        ResultSet ret = stmt.executeQuery(sql);

        dbNameList = new ArrayList<String>();

        while (ret.next()) {
            dbNameList.add(ret.getString(1));
        }
        ret.close();
    } catch (SQLException x) {
        x.printStackTrace();
        try {
            con.rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        LOG.error("get databases error msg=" + x.getMessage());
        throw new MetaException(x.getMessage());
    } finally {
        closeStatement(stmt);
        closeConnection(con);
    }

    return dbNameList;
}