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.hadoop.hive.metastore.MyXid.java

public void addPartition(String dbName, String tblName, AddPartitionDesc addPartitionDesc)
        throws InvalidObjectException, MetaException {
    boolean success = false;

    Connection con = null;
    PreparedStatement ps = null;/* ww  w.  j ava2 s .co m*/
    Statement stmt = null;
    dbName = dbName.toLowerCase();
    tblName = tblName.toLowerCase();

    boolean isPathMaked = false;
    ArrayList<Path> pathToMake = new ArrayList<Path>();
    Warehouse wh = new Warehouse(hiveConf);

    long tblID = 0;

    try {
        con = getSegmentConnection(dbName);
    } catch (MetaStoreConnectException e1) {
        LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                + addPartitionDesc.getLevel() + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                + addPartitionDesc.getLevel() + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    try {
        con.setAutoCommit(false);
        con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
        stmt = con.createStatement();

        String tblType = null;
        boolean hasPriPart = false;
        boolean hasSubPart = false;
        String priPartKey = null;
        String subPartKey = null;
        String priPartType = null;
        String subPartType = null;

        String priKeyType = null;
        String subKeyType = null;
        ResultSet tblSet = null;
        boolean isTblFind = false;
        boolean isColFind = false;

        String tblFormat = null;
        String tblLocation = null;

        PrimitiveTypeInfo pti = null;
        ObjectInspector StringIO = null;
        ObjectInspector ValueIO = null;
        ObjectInspectorConverters.Converter converter1 = null;
        ObjectInspectorConverters.Converter converter2 = null;

        ArrayList<String> partToAdd = new ArrayList<String>();
        String sql = null;

        HiveConf hconf = (HiveConf) hiveConf;
        boolean externalPartition = hconf.getBoolVar(HiveConf.ConfVars.HIVESUPPORTEXTERNALPARTITION);

        if (addPartitionDesc.getLevel() == 0) {
            sql = "SELECT tbl_id, tbl_type, pri_part_type, pri_part_key, tbl_format, tbl_location"
                    + " from TBLS where db_name='" + dbName + "' and tbl_name='" + tblName + "'";

            tblSet = stmt.executeQuery(sql);
            isTblFind = false;

            while (tblSet.next()) {
                isTblFind = true;
                tblID = tblSet.getLong(1);
                tblType = tblSet.getString(2);
                priPartKey = tblSet.getString(4);
                priPartType = tblSet.getString(3);
                tblFormat = tblSet.getString(5);
                tblLocation = tblSet.getString(6);

                if (priPartType != null && !priPartType.isEmpty()) {
                    hasPriPart = true;
                }
                break;
            }
            tblSet.close();

            if (!isTblFind) {
                LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                        + addPartitionDesc.getLevel() + ", msg=" + "can not find table " + dbName + ":"
                        + tblName);

                throw new MetaException("can not find table " + dbName + ":" + tblName);
            }

            if (!tblType.equalsIgnoreCase("MANAGED_TABLE")) {
                if (tblType.equalsIgnoreCase("EXTERNAL_TABLE") && tblFormat != null
                        && tblFormat.equalsIgnoreCase("pgdata")) {
                    LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                            + addPartitionDesc.getLevel() + ", msg=" + tblType + ":" + tblFormat
                            + " can not support alter partition");
                    throw new MetaException(tblType + ":" + tblFormat + " can not support alter partition");
                }

                if (externalPartition && tblType.equalsIgnoreCase("EXTERNAL_TABLE")
                        && (tblFormat == null || !tblFormat.equalsIgnoreCase("pgdata"))) {
                } else {
                    LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                            + addPartitionDesc.getLevel() + ", msg=" + tblType
                            + " can not support alter partition");

                    throw new MetaException(tblType + " can not support alter partition");
                }
            }

            if (!hasPriPart) {
                LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                        + addPartitionDesc.getLevel() + ", msg=" + "table " + dbName + ":" + tblName
                        + " is not pri-partitioned");

                throw new MetaException("table " + dbName + ":" + tblName + " is not pri-partitioned");
            }

            sql = "SELECT type_name from COLUMNS where tbl_id=" + tblID + " and column_name='"
                    + priPartKey.toLowerCase() + "'";
            isColFind = false;
            ResultSet colSet = stmt.executeQuery(sql);
            while (colSet.next()) {
                isColFind = true;
                priKeyType = colSet.getString(1);
                break;
            }
            colSet.close();

            if (!isColFind) {
                LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                        + addPartitionDesc.getLevel() + ", msg=" + "table "
                        + "can not find partition key information " + priPartKey);

                throw new MetaException("can not find partition key information " + priPartKey);
            }

            pti = new PrimitiveTypeInfo();
            pti.setTypeName(priKeyType);
            StringIO = PrimitiveObjectInspectorFactory
                    .getPrimitiveJavaObjectInspector(PrimitiveCategory.STRING);
            ValueIO = PrimitiveObjectInspectorFactory
                    .getPrimitiveWritableObjectInspector(pti.getPrimitiveCategory());
            converter1 = ObjectInspectorConverters.getConverter(StringIO, ValueIO);
            converter2 = ObjectInspectorConverters.getConverter(StringIO, ValueIO);

            if ((addPartitionDesc.getPartType().equalsIgnoreCase("RANGE_PARTITION")
                    && !priPartType.equalsIgnoreCase("range"))
                    || (addPartitionDesc.getPartType().equalsIgnoreCase("LIST_PARTITION")
                            && !priPartType.equalsIgnoreCase("list"))) {
                LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                        + addPartitionDesc.getLevel() + ", msg=" + "can not add  a "
                        + addPartitionDesc.getPartType() + " partition, but the pri-partition type is "
                        + priPartType);

                throw new MetaException("can not add  a " + addPartitionDesc.getPartType()
                        + " partition, but the pri-partition type is " + priPartType);
            }

            LinkedHashMap<String, List<String>> partSpaces = new LinkedHashMap<String, List<String>>();
            Set<String> subPartNameSet = new TreeSet<String>();

            sql = "SELECT level, part_name, part_values from PARTITIONS where" + " tbl_id=" + tblID;// + " order by level asc";

            ResultSet partSet = stmt.executeQuery(sql);
            int partLevel = 0;

            while (partSet.next()) {
                partLevel = partSet.getInt(1);

                if (partLevel == 0) {
                    String partName = partSet.getString(2);
                    List<String> valueList = new ArrayList<String>();
                    Array spaceArray = partSet.getArray(3);

                    ResultSet priValueSet = spaceArray.getResultSet();

                    while (priValueSet.next()) {
                        valueList.add(priValueSet.getString(2));
                    }

                    partSpaces.put(partName, valueList);
                } else if (partLevel == 1) {
                    String partName = partSet.getString(2);
                    subPartNameSet.add(partName);
                }
            }
            partSet.close();

            partToAdd = new ArrayList<String>();

            LinkedHashMap<String, List<String>> addPartSpaces = (LinkedHashMap<String, List<String>>) addPartitionDesc
                    .getParSpaces();

            Iterator<String> itr = addPartSpaces.keySet().iterator();

            while (itr.hasNext()) {
                String key = itr.next().toLowerCase();
                if (partSpaces.containsKey(key)) {
                    LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                            + addPartitionDesc.getLevel() + ", msg=" + "table : " + tblName
                            + " have already contain a pri parititon named: " + key);

                    throw new MetaException(
                            "table : " + tblName + " have already contain a pri parititon named: " + key);
                }
                partToAdd.add(key);
            }

            Iterator<List<String>> listItr = addPartSpaces.values().iterator();

            while (listItr.hasNext()) {
                Iterator<String> valueItr = listItr.next().iterator();
                if (valueItr.hasNext()) {
                    String value = valueItr.next();

                    if (converter1.convert(value) == null) {
                        LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                                + addPartitionDesc.getLevel() + ", msg=" + "value : " + value
                                + " should be type of " + priKeyType);

                        throw new MetaException("value : " + value + " should be type of " + priKeyType);
                    }

                    Iterator<List<String>> PartValuesItr = partSpaces.values().iterator();
                    while (PartValuesItr.hasNext()) {
                        if (PartValuesItr.next().contains(value)) {
                            LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                                    + addPartitionDesc.getLevel() + ", msg=" + "table : " + tblName
                                    + " have already contain a pri partition contain value: " + value);

                            throw new MetaException("table : " + tblName
                                    + " have already contain a pri partition contain value: " + value);
                        }
                    }
                }
            }

            ps = con.prepareStatement(
                    "INSERT INTO partitions(level, tbl_id, " + " part_name, part_values) values(?,?,?,?)");

            for (Map.Entry<String, List<String>> entry : addPartSpaces.entrySet()) {
                ps.setInt(1, 0);
                ps.setLong(2, tblID);

                Array spaceArray = con.createArrayOf("varchar", entry.getValue().toArray());
                ps.setArray(4, spaceArray);
                ps.setString(3, entry.getKey());

                ps.addBatch();
            }
            ps.executeBatch();

            if (!tblType.equalsIgnoreCase("EXTERNAL_TABLE")) {
                for (String partName : partToAdd) {
                    if (tblLocation == null || tblLocation.trim().isEmpty()) {
                        pathToMake.addAll(wh.getPriPartitionPaths(dbName, tblName, partName, subPartNameSet));
                    } else {
                        pathToMake.addAll(Warehouse.getPriPartitionPaths(new Path(tblLocation), partName,
                                subPartNameSet));
                    }
                }
            } else {
                for (String partName : partToAdd) {
                    pathToMake.addAll(
                            Warehouse.getPriPartitionPaths(new Path(tblLocation), partName, subPartNameSet));
                }
            }
        } else if (addPartitionDesc.getLevel() == 1) {
            sql = "SELECT tbl_id, tbl_type, sub_part_type, sub_part_key, tbl_format, tbl_location"
                    + " from TBLS where db_name='" + dbName.toLowerCase() + "' and tbl_name='"
                    + tblName.toLowerCase() + "'";

            tblSet = stmt.executeQuery(sql);
            isTblFind = false;

            while (tblSet.next()) {
                isTblFind = true;
                tblID = tblSet.getLong(1);
                tblType = tblSet.getString(2);
                subPartKey = tblSet.getString(4);
                subPartType = tblSet.getString(3);
                tblFormat = tblSet.getString(5);
                tblLocation = tblSet.getString(6);

                if (subPartType != null && !subPartType.isEmpty()) {
                    hasSubPart = true;
                }

                break;
            }

            tblSet.close();
            if (!isTblFind) {
                LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                        + addPartitionDesc.getLevel() + ", msg=" + "can not find table " + dbName + ":"
                        + tblName);

                throw new MetaException("can not find table " + dbName + ":" + tblName);
            }

            if (!tblType.equalsIgnoreCase("MANAGED_TABLE")) {
                if (tblType.equalsIgnoreCase("EXTERNAL_TABLE") && tblFormat != null
                        && tblFormat.equalsIgnoreCase("pgdata")) {
                    LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                            + addPartitionDesc.getLevel() + ", msg=" + tblType + ":" + tblFormat
                            + " can not support alter partition");
                    throw new MetaException(tblType + ":" + tblFormat + " can not support alter partition");
                }

                if (externalPartition && tblType.equalsIgnoreCase("EXTERNAL_TABLE")
                        && (tblFormat == null || !tblFormat.equalsIgnoreCase("pgdata"))) {
                } else {
                    LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                            + addPartitionDesc.getLevel() + ", msg=" + tblType
                            + " can not support alter partition");

                    throw new MetaException(tblType + " can not support alter partition");
                }
            }

            if (!hasSubPart) {
                LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                        + addPartitionDesc.getLevel() + ", msg=" + "table " + dbName + ":" + tblName
                        + " is not sun-partitioned");

                throw new MetaException("table " + dbName + ":" + tblName + " is not sun-partitioned");
            }

            sql = "SELECT type_name from COLUMNS where tbl_id=" + tblID + " and column_name='"
                    + subPartKey.toLowerCase() + "'";

            isColFind = false;
            ResultSet colSet = stmt.executeQuery(sql);
            while (colSet.next()) {
                isColFind = true;
                subKeyType = colSet.getString(1);
                break;
            }

            colSet.close();

            if (!isColFind) {
                LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                        + addPartitionDesc.getLevel() + ", msg=" + "can not find partition key information "
                        + priPartKey);

                throw new MetaException("can not find partition key information " + priPartKey);
            }

            pti = new PrimitiveTypeInfo();
            pti.setTypeName(subKeyType);
            StringIO = PrimitiveObjectInspectorFactory
                    .getPrimitiveJavaObjectInspector(PrimitiveCategory.STRING);
            ValueIO = PrimitiveObjectInspectorFactory
                    .getPrimitiveWritableObjectInspector(pti.getPrimitiveCategory());
            converter1 = ObjectInspectorConverters.getConverter(StringIO, ValueIO);
            converter2 = ObjectInspectorConverters.getConverter(StringIO, ValueIO);

            if ((addPartitionDesc.getPartType().equalsIgnoreCase("RANGE_PARTITION")
                    && !subPartType.equalsIgnoreCase("range"))
                    || (addPartitionDesc.getPartType().equalsIgnoreCase("LIST_PARTITION")
                            && !subPartType.equalsIgnoreCase("list"))) {
                LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                        + addPartitionDesc.getLevel() + ", msg=" + "you can not add  a "
                        + addPartitionDesc.getPartType() + " partition, but the sub-partition type is "
                        + subPartType);

                throw new MetaException("you can not add  a " + addPartitionDesc.getPartType()
                        + " partition, but the sub-partition type is " + subPartType);
            }

            LinkedHashMap<String, List<String>> partSpaces = new LinkedHashMap<String, List<String>>();
            Set<String> partNameSet = new TreeSet<String>();

            sql = "SELECT level,  part_name, part_values from PARTITIONS where" + " tbl_id=" + tblID;// + " order by level asc";

            ResultSet partSet = stmt.executeQuery(sql);
            int partLevel = 0;

            while (partSet.next()) {
                partLevel = partSet.getInt(1);

                if (partLevel == 1) {
                    String partName = partSet.getString(2);
                    List<String> valueList = new ArrayList<String>();
                    Array spaceArray = partSet.getArray(3);

                    ResultSet priValueSet = spaceArray.getResultSet();

                    while (priValueSet.next()) {
                        valueList.add(priValueSet.getString(2));
                    }
                    partSpaces.put(partName, valueList);
                } else if (partLevel == 0) {
                    String partName = partSet.getString(2);
                    partNameSet.add(partName);
                }
            }

            partToAdd = new ArrayList<String>();

            LinkedHashMap<String, List<String>> addPartSpaces = (LinkedHashMap<String, List<String>>) addPartitionDesc
                    .getParSpaces();

            Iterator<String> itr = addPartSpaces.keySet().iterator();

            while (itr.hasNext()) {
                String key = itr.next().toLowerCase();
                if (partSpaces.containsKey(key)) {
                    LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                            + addPartitionDesc.getLevel() + ", msg=" + "table : " + tblName
                            + " have already contain a sub parititon named: " + key);

                    throw new MetaException(
                            "table : " + tblName + " have already contain a sub parititon named: " + key);
                }

                if (key.equalsIgnoreCase("default")) {
                    LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                            + addPartitionDesc.getLevel() + ", msg="
                            + "use : 'alter table tblname add default subpartition' to add default subpartition!");

                    throw new MetaException(
                            "use : 'alter table tblname add default subpartition' to add default subpartition!");
                }
                partToAdd.add(key);
            }

            Iterator<List<String>> listItr = addPartSpaces.values().iterator();

            while (listItr.hasNext()) {
                Iterator<String> valueItr = listItr.next().iterator();
                if (valueItr.hasNext()) {
                    String value = valueItr.next();

                    if (converter1.convert(value) == null) {
                        LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                                + addPartitionDesc.getLevel() + ", msg=" + "value : " + value
                                + " should be type of " + priKeyType);

                        throw new MetaException("value : " + value + " should be type of " + priKeyType);
                    }

                    Iterator<List<String>> PartValuesItr = partSpaces.values().iterator();
                    while (PartValuesItr.hasNext()) {
                        if (PartValuesItr.next().contains(value)) {
                            LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                                    + addPartitionDesc.getLevel() + ", msg=" + "table : " + tblName
                                    + " have already contain a sub partition contain value: " + value);

                            throw new MetaException("table : " + tblName
                                    + " have already contain a sub partition contain value: " + value);
                        }
                    }
                }
            }

            ps = con.prepareStatement(
                    "INSERT INTO partitions(level, tbl_id, " + " part_name, part_values) values(?,?,?,?)");

            for (Map.Entry<String, List<String>> entry : addPartSpaces.entrySet()) {
                ps.setInt(1, 1);
                ps.setLong(2, tblID);

                Array spaceArray = con.createArrayOf("varchar", entry.getValue().toArray());
                ps.setArray(4, spaceArray);
                ps.setString(3, entry.getKey());

                ps.addBatch();
            }
            ps.executeBatch();

            if (!tblType.equalsIgnoreCase("EXTERNAL_TABLE")) {
                for (String partName : partToAdd) {
                    if (tblLocation == null || tblLocation.trim().isEmpty()) {
                        pathToMake.addAll(wh.getSubPartitionPaths(dbName, tblName, partNameSet, partName));
                    } else {
                        pathToMake.addAll(
                                Warehouse.getSubPartitionPaths(new Path(tblLocation), partNameSet, partName));
                    }
                }
            } else {
                for (String partName : partToAdd) {
                    pathToMake.addAll(
                            Warehouse.getSubPartitionPaths(new Path(tblLocation), partNameSet, partName));
                }
            }
        }

        con.commit();
        success = true;
    } catch (SQLException ex) {
        ex.printStackTrace();
        LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                + addPartitionDesc.getLevel() + ", msg=" + ex.getMessage());

        throw new MetaException(ex.getMessage());
    } finally {
        if (!success) {
            try {
                con.rollback();
            } catch (SQLException e) {
            }

            if (isPathMaked) {
                for (Path path : pathToMake) {
                    wh.deleteDir(path, false);
                }
            }
        }

        closeStatement(ps);
        closeConnection(con);
    }

    if (success) {
        boolean mkDirOK = false;
        List<Path> createdPath = new ArrayList<Path>();
        try {
            for (Path path : pathToMake) {
                mkDirOK = wh.mkdirs(path);
                if (!mkDirOK) {
                    break;
                }

                createdPath.add(path);
            }
        } catch (Exception x) {
            mkDirOK = false;
        }

        if (!mkDirOK) {
            dropPartitionMeta(dbName, tblID, addPartitionDesc);
            if (!createdPath.isEmpty()) {
                for (Path path : createdPath) {
                    wh.deleteDir(path, true);
                }
            }

            throw new MetaException("can not create hdfs path, add partition failed");
        }

    }
}

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

@Override
public void createTable(Table tbl) throws InvalidObjectException, MetaException, AlreadyExistsException {

    if (tbl == null) {
        throw new InvalidObjectException("unvalid parameters, tbl is null");
    }// ww  w.  j  a va2 s  .c  o m

    if (tbl.getTableType() == null) {
        tbl.setTableType("MANAGED_TABLE");
    }

    if (tbl.getTableType().equalsIgnoreCase("VIRTUAL_VIEW")) {
        jdbcCreateView(tbl);
        return;
    }

    tbl.setDbName(tbl.getDbName().toLowerCase());
    tbl.setTableName(tbl.getTableName().toLowerCase());

    LOG.debug("first, check the name is valid or not");
    if (!MetaStoreUtils.validateName(tbl.getTableName())
            || !MetaStoreUtils.validateColNames(tbl.getSd().getCols())
            || (tbl.getPriPartition() != null
                    && !MetaStoreUtils.validateName(tbl.getPriPartition().getParKey().getName()))
            || (tbl.getSubPartition() != null
                    && !MetaStoreUtils.validateName(tbl.getSubPartition().getParKey().getName()))) {
        throw new InvalidObjectException(tbl.getTableName() + " is not a valid object name");
    }

    long tblID = genTblID(tbl.getDbName(), tbl.getTableName());

    boolean success = false;

    Connection con;
    PreparedStatement ps = null;
    Statement stmt = null;
    Path tblPath = null;
    Warehouse wh = new Warehouse(hiveConf);
    boolean madeDir = false;

    LOG.debug("2, generate table path ");

    if (tbl.getSd().getLocation() == null || tbl.getSd().getLocation().isEmpty()) {
        tblPath = wh.getDefaultTablePath(tbl.getDbName(), tbl.getTableName());
    } else {
        if (tbl.getTableType().equalsIgnoreCase("EXTERNAL_TABLE")) {
            LOG.warn("Location: " + tbl.getSd().getLocation() + "specified for non-external table:"
                    + tbl.getTableName());
        }

        tblPath = wh.getDnsPath(new Path(tbl.getSd().getLocation()));
    }
    tbl.getSd().setLocation(tblPath.toString());

    try {
        con = getSegmentConnection(tbl.getDbName());
    } catch (MetaStoreConnectException e1) {
        LOG.error("create table error, db=" + tbl.getDbName() + ", table=" + tbl.getTableName() + ", msg="
                + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("create table error, db=" + tbl.getDbName() + ", table=" + tbl.getTableName() + ", msg="
                + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    try {
        con.setAutoCommit(false);
        con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);

        stmt = con.createStatement();

        LOG.debug("1 check the table is exist or not");
        String sql = "select tbl_id from tbls where db_name='" + tbl.getDbName().toLowerCase()
                + "' and tbl_name='" + tbl.getTableName().toLowerCase() + "'";

        boolean isTblFind = false;
        ResultSet checkTblSet = stmt.executeQuery(sql);

        while (checkTblSet.next()) {
            isTblFind = true;
            break;
        }
        checkTblSet.close();

        if (isTblFind) {
            throw new AlreadyExistsException(
                    "table " + tbl.getDbName() + ":" + tbl.getTableName() + " has exist");
        }

        LOG.debug("2 insert into tbls");

        ps = con.prepareStatement("INSERT INTO TBLS(tbl_id, is_compressed, retention, tbl_type, db_name, "
                + "tbl_name, tbl_owner, tbl_format"
                + ", pri_part_type, sub_part_type, pri_part_key, sub_part_key, input_format, output_format"
                + ", serde_name, serde_lib, tbl_location, tbl_comment)"
                + " values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");

        StorageDescriptor sd = tbl.getSd();
        if (sd == null || sd.getSerdeInfo() == null) {
            throw new MetaException("storage descriptor of table " + tbl.getTableName() + " is null");
        }

        SerDeInfo sdInfo = sd.getSerdeInfo();

        ps.setLong(1, tblID);
        ps.setBoolean(2, sd.isCompressed());
        ps.setLong(3, tbl.getRetention());
        if (tbl.getParameters() != null && tbl.getParameters().get("EXTERNAL") != null
                && tbl.getParameters().get("EXTERNAL").equalsIgnoreCase("TRUE")) {
            ps.setString(4, "EXTERNAL_TABLE");
        } else {
            ps.setString(4, tbl.getTableType());
        }
        ps.setString(5, tbl.getDbName());
        ps.setString(6, tbl.getTableName());
        ps.setString(7, tbl.getOwner());

        if (tbl.getParameters() == null) {
            ps.setString(8, null);
        } else {
            ps.setString(8, tbl.getParameters().get("type"));
        }

        Partition priPart = tbl.getPriPartition();
        Partition subPart = tbl.getSubPartition();
        if (priPart != null) {
            ps.setString(11, priPart.getParKey().getName());
            ps.setString(9, priPart.getParType());
        } else {
            ps.setString(11, null);
            ps.setString(9, null);
        }

        if (subPart != null) {
            ps.setString(12, subPart.getParKey().getName());
            ps.setString(10, subPart.getParType());
        } else {
            ps.setString(12, null);
            ps.setString(10, null);
        }

        ps.setString(13, sd.getInputFormat());
        ps.setString(14, sd.getOutputFormat());
        ps.setString(15, sdInfo.getName());
        ps.setString(16, sdInfo.getSerializationLib());
        ps.setString(17, sd.getLocation());

        if (tbl.getParameters() == null) {
            ps.setString(18, null);
        } else {
            ps.setString(18, tbl.getParameters().get("comment"));
        }

        ps.executeUpdate();

        ps.close();

        LOG.debug("3 insert into partitions");
        if (priPart != null) {
            ps = con.prepareStatement(
                    "INSERT INTO PARTITIONS(level, tbl_id," + "part_name, part_values) values(?,?,?,?)");

            Map<String, List<String>> partSpaceMap = priPart.getParSpaces();

            for (Map.Entry<String, List<String>> entry : partSpaceMap.entrySet()) {
                ps.setInt(1, 0);
                ps.setLong(2, tblID);
                ps.setString(3, entry.getKey());
                if (entry.getValue() != null) {
                    Array spaceArray = con.createArrayOf("varchar", entry.getValue().toArray());
                    ps.setArray(4, spaceArray);
                } else {
                    ps.setArray(4, null);
                }

                ps.addBatch();
            }
            ps.executeBatch();
            ps.close();
        }

        if (subPart != null) {
            ps = con.prepareStatement(
                    "INSERT INTO PARTITIONS(level, tbl_id," + "part_name, part_values) values(?,?,?,?)");

            Map<String, List<String>> partSpaceMap = subPart.getParSpaces();

            for (Map.Entry<String, List<String>> entry : partSpaceMap.entrySet()) {
                ps.setInt(1, 1);
                ps.setLong(2, tblID);
                ps.setString(3, entry.getKey());

                if (entry.getValue() != null) {
                    Array spaceArray = con.createArrayOf("varchar", entry.getValue().toArray());
                    ps.setArray(4, spaceArray);
                } else {
                    ps.setArray(4, null);
                }

                ps.addBatch();
            }
            ps.executeBatch();
            ps.close();
        }

        LOG.debug("4 insert into columns");
        ps = con.prepareStatement("INSERT INTO COLUMNS(column_index, tbl_id, column_name, type_name, comment) "
                + " values(?,?,?,?,?)");

        List<FieldSchema> fieldList = sd.getCols();
        int fieldSize = fieldList.size();

        for (int i = 0; i < fieldSize; i++) {
            FieldSchema field = fieldList.get(i);
            ps.setInt(1, i);
            ps.setLong(2, tblID);
            ps.setString(3, field.getName().toLowerCase());
            ps.setString(4, field.getType());
            ps.setString(5, field.getComment());

            ps.addBatch();
        }

        ps.executeBatch();
        ps.close();

        LOG.debug("5  insert into parameters");

        boolean createExtDirIfNotExist = true;
        if (tbl.getParametersSize() > 0) {
            String createExtDirIfNotExistStr = tbl.getParameters().get("hive.exttable.createdir.ifnotexist");
            LOG.info("XXcreateExtDirIfNotExistStr=" + createExtDirIfNotExistStr);
            if (createExtDirIfNotExistStr != null && createExtDirIfNotExistStr.equalsIgnoreCase("false")) {
                createExtDirIfNotExist = false;
            }
            tbl.getParameters().remove("hive.exttable.createdir.ifnotexist");
        }

        if (tbl.getParametersSize() > 0 || sd.getParametersSize() > 0
                || sd.getSerdeInfo().getParametersSize() > 0 || sd.getNumBuckets() > -1) {
            ps = con.prepareStatement("insert into table_params(tbl_id, param_type, param_key, param_value) "
                    + " values(?,?,?,?)");
            if (tbl.getParametersSize() > 0) {

                for (Map.Entry<String, String> entry : tbl.getParameters().entrySet()) {
                    if (entry.getKey().equalsIgnoreCase("type") || entry.getKey().equalsIgnoreCase("comment"))
                        continue;
                    ps.setLong(1, tblID);
                    ps.setString(2, "TBL");
                    ps.setString(3, entry.getKey());
                    ps.setString(4, entry.getValue());

                    ps.addBatch();
                }
            }

            if (sd.getParametersSize() > 0) {
                for (Map.Entry<String, String> entry : sd.getParameters().entrySet()) {
                    ps.setLong(1, tblID);
                    ps.setString(2, "SD");
                    ps.setString(3, entry.getKey());
                    ps.setString(4, entry.getValue());

                    ps.addBatch();
                }
            }

            if (sd.getSerdeInfo().getParametersSize() > 0) {
                for (Map.Entry<String, String> entry : sd.getSerdeInfo().getParameters().entrySet()) {
                    ps.setLong(1, tblID);
                    ps.setString(2, "SERDE");
                    ps.setString(3, entry.getKey());
                    ps.setString(4, entry.getValue());

                    ps.addBatch();
                }
            }

            if (sd.getNumBuckets() > -1) {
                ps.setLong(1, tblID);
                ps.setString(2, "SD");
                ps.setString(3, "NUM_BUCKETS");
                ps.setString(4, String.valueOf(sd.getNumBuckets()));
                ps.addBatch();
            }

            ps.executeBatch();
            ps.close();
        }

        if (tbl.getSd().getBucketCols() != null && !tbl.getSd().getBucketCols().isEmpty()) {
            ps = con.prepareStatement(
                    "insert into bucket_cols(tbl_id, bucket_col_name, col_index) values(?,?,?)");
            int index = 0;
            for (String col : tbl.getSd().getBucketCols()) {
                ps.setLong(1, tblID);
                ps.setString(2, col.toLowerCase());
                ps.setInt(3, index);
                index++;
                ps.addBatch();
            }

            ps.executeBatch();
            ps.close();
        }

        if (tbl.getSd().getSortCols() != null && !tbl.getSd().getSortCols().isEmpty()) {
            ps = con.prepareStatement(
                    "insert into sort_cols(tbl_id, sort_column_name, sort_order, col_index) values(?,?,?,?)");
            int index = 0;
            for (Order o : tbl.getSd().getSortCols()) {
                ps.setLong(1, tblID);
                ps.setString(2, o.getCol());
                ps.setInt(3, o.getOrder());
                ps.setInt(4, index);
                index++;
                ps.addBatch();
            }

            ps.executeBatch();
            ps.close();
        }

        LOG.debug("make hdfs directory for table");

        if (createExtDirIfNotExist && tblPath != null) {
            if (!wh.isDir(tblPath)) {
                if (!wh.mkdirs(tblPath)) {
                    throw new MetaException(tblPath + " is not a directory or unable to create one");
                }
                madeDir = true;
            }

            if (tbl.getPriPartition() != null) {
                Set<String> priPartNames = tbl.getPriPartition().getParSpaces().keySet();

                Set<String> subPartNames = null;
                if (tbl.getSubPartition() != null) {
                    subPartNames = tbl.getSubPartition().getParSpaces().keySet();
                }

                List<Path> partPaths = Warehouse.getPartitionPaths(tblPath, priPartNames, subPartNames);

                for (Path partPath : partPaths) {
                    if (!wh.mkdirs(partPath)) {
                        throw new MetaException(
                                "Partition path " + partPath + " is not a directory or unable to create one.");
                    }
                }
            }
        }

        con.commit();

        success = true;
    } catch (SQLException sqlex) {
        LOG.error("create table error db=" + tbl.getDbName() + ", table=" + tbl.getTableName() + ",msg="
                + sqlex.getMessage());
        sqlex.printStackTrace();
        throw new MetaException(sqlex.getMessage());
    } finally {
        if (!success) {
            try {
                con.rollback();
            } catch (SQLException e) {
            }

            if (madeDir) {
                wh.deleteDir(tblPath, true);
            }
        }

        closeStatement(stmt);
        closeStatement(ps);
        closeConnection(con);
    }

    return;
}

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

@Override
public boolean grantAuthOnDb(String forWho, List<String> privileges, String db)
        throws NoSuchObjectException, InvalidObjectException, MetaException {
    Connection con = null;
    Statement ps = null;//from w  w w.java  2s  .com
    boolean success = false;
    PreparedStatement pss = null;
    forWho = forWho.toLowerCase();
    db = db.toLowerCase();

    try {
        con = getSegmentConnection(db);
    } catch (MetaStoreConnectException e1) {
        LOG.error("grant auth on db error, db=" + db + ", forwho=" + forWho + ", msg=" + e1.getMessage());
        throw new MetaException("can not find db:" + db);
    } catch (SQLException e1) {
        LOG.error("grant auth on db error, db=" + db + ", forwho=" + forWho + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    if (privileges == null) {
        throw new InvalidObjectException("No privileges are given!");
    }

    try {
        ps = con.createStatement();

        String sql = "select name from dbs where name='" + db + "'";

        boolean isDbfind = false;
        ResultSet dbSet = ps.executeQuery(sql);

        while (dbSet.next()) {
            isDbfind = true;
            break;
        }

        dbSet.close();

        if (!isDbfind) {
            throw new NoSuchObjectException("can not find db:" + db);
        }
    } catch (SQLException ex) {
        LOG.error("grant auth on db error, db=" + db + ", forwho=" + forWho + ", msg=" + ex.getMessage());
        ex.printStackTrace();
        throw new MetaException(ex.getMessage());
    } finally {

        closeStatement(ps);
        closeConnection(con);
    }

    try {
        con = getGlobalConnection();
    } catch (MetaStoreConnectException e1) {
        LOG.error("grant auth on db error, db=" + db + ", forwho=" + forWho + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("grant auth on db error, db=" + db + ", forwho=" + forWho + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    try {
        con.setAutoCommit(false);
        con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
        ps = con.createStatement();

        String sql = "select alter_priv,create_priv,createview_priv, "
                + " delete_priv, drop_priv, index_priv, insert_priv, select_priv, showview_priv,"
                + " update_priv from dbpriv where user_name='" + forWho + "' and db_name='" + db + "'";

        boolean isPrivFind = false;

        boolean selPriv = false;
        boolean insertPriv = false;
        boolean createPriv = false;
        boolean dropPriv = false;
        boolean deletePriv = false;
        boolean alterPriv = false;
        boolean updatePriv = false;
        boolean indexPriv = false;
        boolean showViewPriv = false;
        boolean createViewPriv = false;

        ResultSet privSet = ps.executeQuery(sql);

        while (privSet.next()) {
            isPrivFind = true;
            alterPriv = privSet.getBoolean(1);
            createPriv = privSet.getBoolean(2);
            createViewPriv = privSet.getBoolean(3);
            deletePriv = privSet.getBoolean(4);
            dropPriv = privSet.getBoolean(5);
            indexPriv = privSet.getBoolean(6);
            insertPriv = privSet.getBoolean(7);
            selPriv = privSet.getBoolean(8);
            showViewPriv = privSet.getBoolean(9);
            updatePriv = privSet.getBoolean(10);

            break;
        }

        privSet.close();

        for (String priv : privileges) {
            if (priv.equals("TOK_SELECT_PRI")) {
                selPriv = true;
            } else if (priv.equals("TOK_INSERT_PRI")) {
                insertPriv = true;
            } else if (priv.equals("TOK_CREATE_PRI")) {
                createPriv = true;
            } else if (priv.equals("TOK_DROP_PRI")) {
                dropPriv = true;
            } else if (priv.equals("TOK_DELETE_PRI")) {
                deletePriv = true;
            } else if (priv.equals("TOK_ALTER_PRI")) {
                alterPriv = true;
            } else if (priv.equals("TOK_UPDATE_PRI")) {
                updatePriv = true;
            } else if (priv.equals("TOK_INDEX_PRI")) {
                indexPriv = true;
            } else if (priv.equals("TOK_CREATEVIEW_PRI")) {
                createViewPriv = true;
            } else if (priv.equals("TOK_SHOWVIEW_PRI")) {
                showViewPriv = true;
            } else if (priv.equals("TOK_ALL_PRI")) {
                selPriv = true;
                insertPriv = true;
                createPriv = true;
                dropPriv = true;
                deletePriv = true;
                alterPriv = true;
                updatePriv = true;
                indexPriv = true;
                createViewPriv = true;
                showViewPriv = true;
            } else
                throw new InvalidObjectException("Privilege does not exist: " + priv);
        }

        if (!isPrivFind) {
            pss = con.prepareStatement("insert into dbpriv(alter_priv, create_priv, createview_priv,"
                    + "delete_priv, drop_priv, index_priv, insert_priv, select_priv"
                    + ", showview_priv, update_priv, user_name, db_name) values(?,?,?,?,?,?,?,?,?,?,?,?)");

            pss.setBoolean(1, alterPriv);
            pss.setBoolean(2, createPriv);
            pss.setBoolean(3, createViewPriv);
            pss.setBoolean(4, deletePriv);
            pss.setBoolean(5, dropPriv);
            pss.setBoolean(6, indexPriv);
            pss.setBoolean(7, insertPriv);
            pss.setBoolean(8, selPriv);
            pss.setBoolean(9, showViewPriv);
            pss.setBoolean(10, updatePriv);
            pss.setString(11, forWho);
            pss.setString(12, db);

            pss.executeUpdate();
            pss.close();
        } else {
            pss = con.prepareStatement("update dbpriv set alter_priv=?, create_priv=?, createview_priv=?, "
                    + " delete_priv=?, drop_priv=?, index_priv=?, insert_priv=?, select_priv=?, showview_priv=?,"
                    + " update_priv=? where user_name=? and db_name=?");

            pss.setBoolean(1, alterPriv);
            pss.setBoolean(2, createPriv);
            pss.setBoolean(3, createViewPriv);
            pss.setBoolean(4, deletePriv);
            pss.setBoolean(5, dropPriv);
            pss.setBoolean(6, indexPriv);
            pss.setBoolean(7, insertPriv);
            pss.setBoolean(8, selPriv);
            pss.setBoolean(9, showViewPriv);
            pss.setBoolean(10, updatePriv);
            pss.setString(11, forWho);
            pss.setString(12, db);

            pss.executeUpdate();
            pss.close();
        }

        con.commit();
        success = true;
    } catch (SQLException ex) {
        LOG.error("grant auth on db error, db=" + db + ", forwho=" + forWho + ", msg=" + ex.getMessage());
        ex.printStackTrace();
        throw new MetaException(ex.getMessage());
    } finally {
        if (!success) {
            try {
                con.rollback();
            } catch (SQLException e) {
            }
        }

        closeStatement(ps);
        closeStatement(pss);
        closeConnection(con);
    }

    return success;
}

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

@Override
public boolean grantAuthOnTbl(String forWho, List<String> privileges, String db, String tbl)
        throws NoSuchObjectException, InvalidObjectException, MetaException {
    Connection con = null;
    ;/*from  w  ww.  j  ava2  s  . co  m*/
    Statement ps = null;
    boolean success = false;
    PreparedStatement pss = null;

    success = false;
    forWho = forWho.toLowerCase();
    db = db.toLowerCase();
    tbl = tbl.toLowerCase();

    if (privileges == null) {
        throw new InvalidObjectException("No privileges are given!");
    }

    try {
        con = getSegmentConnection(db);
    } catch (MetaStoreConnectException e1) {
        LOG.error("grant auth on db error, forWho=" + forWho + ", db=" + db + ", tbl=" + tbl + ", msg="
                + e1.getMessage());
        throw new MetaException("can not find db:" + db);
    } catch (SQLException e1) {
        LOG.error("grant auth on db error, forWho=" + forWho + ", db=" + db + ", tbl=" + tbl + ", msg="
                + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    Set<String> tblNames = new HashSet<String>();

    try {
        ps = con.createStatement();
        String sql = null;
        StringBuilder sb = new StringBuilder();
        int size = tbl.length();
        for (int i = 0; i < size; i++) {
            if (tbl.charAt(i) != '\'') {
                sb.append(tbl.charAt(i));
            }
        }

        tbl = sb.toString();

        if (tbl == null || tbl.isEmpty() || tbl.equals(".*") || tbl.equals("*")) {
            sql = "select tbl_name from tbls" + " where  tbls.db_name='" + db.toLowerCase() + "'";
        } else {
            tbl = tbl.replace('*', '%');

            sql = "select tbl_name from tbls" + " where  tbls.db_name='" + db + "' and tbls.tbl_name like '"
                    + tbl + "'";
        }

        LOG.debug("SQL is " + sql);

        ResultSet tblSet = ps.executeQuery(sql);
        while (tblSet.next()) {
            String tblName = tblSet.getString(1);
            tblNames.add(tblName);
        }

        tblSet.close();

        con.commit();
        success = true;
    } catch (SQLException ex) {
        ex.printStackTrace();
        LOG.error("grant auth on db error, forWho=" + forWho + ", db=" + db + ", tbl=" + tbl + ", msg="
                + ex.getMessage());
        LOG.error(ex.getMessage());
    } finally {

        closeStatement(ps);
        closeConnection(con);
    }

    if (tblNames.isEmpty()) {
        throw new NoSuchObjectException("Table does not exist: " + tbl + " in db: " + db);
    }

    try {
        con = getGlobalConnection();
    } catch (MetaStoreConnectException e1) {
        LOG.error("grant auth on db error, forWho=" + forWho + ", db=" + db + ", tbl=" + tbl + ", msg="
                + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("grant auth on db error, forWho=" + forWho + ", db=" + db + ", tbl=" + tbl + ", msg="
                + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    try {
        con.setAutoCommit(false);
        con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
        ps = con.createStatement();
        String sql = null;
        StringBuilder sb = new StringBuilder();
        int size = tbl.length();
        for (int i = 0; i < size; i++) {
            if (tbl.charAt(i) != '\'') {
                sb.append(tbl.charAt(i));
            }
        }

        tbl = sb.toString();

        if (tbl == null || tbl.isEmpty() || tbl.equals(".*") || tbl.equals("*")) {
            sql = "select tbl_name, alter_priv, create_priv, delete_priv "
                    + ",drop_priv, index_priv, insert_priv, select_priv, " + " update_priv from tblpriv"
                    + " where db_name='" + db.toLowerCase() + "' and user_name='" + forWho + "'";
        } else {
            tbl = tbl.replace('*', '%');

            sql = "select tbl_name, alter_priv, create_priv, delete_priv "
                    + ",drop_priv, index_priv, insert_priv, select_priv, " + " update_priv from tblpriv"
                    + " where db_name='" + db + "' and tbl_name like '" + tbl + "'" + " and user_name='"
                    + forWho + "'";
        }

        LOG.debug("SQL is " + sql);

        ResultSet tblSet = ps.executeQuery(sql);

        Map<String, TblPrivDesc> tblPrivMap = new HashMap<String, TblPrivDesc>();

        while (tblSet.next()) {
            String tblName = tblSet.getString(1);

            TblPrivDesc privDesc = new TblPrivDesc();
            privDesc.alterPriv = tblSet.getBoolean(2);
            privDesc.createPriv = tblSet.getBoolean(3);
            privDesc.deletePriv = tblSet.getBoolean(4);
            privDesc.dropPriv = tblSet.getBoolean(5);
            privDesc.indexPriv = tblSet.getBoolean(6);
            privDesc.insertPriv = tblSet.getBoolean(7);
            privDesc.selPriv = tblSet.getBoolean(8);
            privDesc.updatePriv = tblSet.getBoolean(9);

            tblPrivMap.put(tblName, privDesc);
        }

        tblSet.close();

        boolean selPriv = false;
        boolean insertPriv = false;
        boolean createPriv = false;
        boolean dropPriv = false;
        boolean deletePriv = false;
        boolean alterPriv = false;
        boolean updatePriv = false;
        boolean indexPriv = false;

        for (String priv : privileges) {
            if (priv.equals("TOK_SELECT_PRI")) {
                selPriv = true;
            } else if (priv.equals("TOK_INSERT_PRI")) {
                insertPriv = true;
            } else if (priv.equals("TOK_CREATE_PRI")) {
                createPriv = true;
            } else if (priv.equals("TOK_DROP_PRI")) {
                dropPriv = true;
            } else if (priv.equals("TOK_DELETE_PRI")) {
                deletePriv = true;
            } else if (priv.equals("TOK_ALTER_PRI")) {
                alterPriv = true;
            } else if (priv.equals("TOK_UPDATE_PRI")) {
                updatePriv = true;
            } else if (priv.equals("TOK_INDEX_PRI")) {
                indexPriv = true;
            } else if (priv.equals("TOK_ALL_PRI")) {
                selPriv = true;
                insertPriv = true;
                createPriv = true;
                dropPriv = true;
                deletePriv = true;
                alterPriv = true;
                updatePriv = true;
                indexPriv = true;
            } else {
                throw new InvalidObjectException("Privilege does not exist: " + priv);
            }
        }

        if (!tblPrivMap.isEmpty()) {
            Collection<TblPrivDesc> tblPrivColl = tblPrivMap.values();
            if (alterPriv) {
                for (TblPrivDesc entry : tblPrivColl) {
                    entry.alterPriv = true;
                }
            }

            if (createPriv) {
                for (TblPrivDesc entry : tblPrivColl) {
                    entry.createPriv = true;
                }
            }

            if (deletePriv) {
                for (TblPrivDesc entry : tblPrivColl) {
                    entry.deletePriv = true;
                }
            }

            if (dropPriv) {
                for (TblPrivDesc entry : tblPrivColl) {
                    entry.dropPriv = true;
                }
            }

            if (indexPriv) {
                for (TblPrivDesc entry : tblPrivColl) {
                    entry.indexPriv = true;
                }
            }

            if (insertPriv) {
                for (TblPrivDesc entry : tblPrivColl) {
                    entry.insertPriv = true;
                }
            }

            if (selPriv) {
                for (TblPrivDesc entry : tblPrivColl) {
                    entry.selPriv = true;
                }
            }

            if (updatePriv) {
                for (TblPrivDesc entry : tblPrivColl) {
                    entry.updatePriv = true;
                }
            }

            pss = con.prepareStatement("update tblpriv set alter_priv=?, create_priv=?,  "
                    + " delete_priv=?, drop_priv=?, index_priv=?, insert_priv=?, select_priv=?,"
                    + " update_priv=? where user_name=? and db_name=? and tbl_name=? ");

            for (Entry<String, TblPrivDesc> entry : tblPrivMap.entrySet()) {

                pss.setBoolean(1, entry.getValue().alterPriv);
                pss.setBoolean(2, entry.getValue().createPriv);

                pss.setBoolean(3, entry.getValue().deletePriv);
                pss.setBoolean(4, entry.getValue().dropPriv);
                pss.setBoolean(5, entry.getValue().indexPriv);
                pss.setBoolean(6, entry.getValue().insertPriv);
                pss.setBoolean(7, entry.getValue().selPriv);

                pss.setBoolean(8, entry.getValue().updatePriv);

                pss.setString(9, forWho);
                pss.setString(10, db);
                pss.setString(11, entry.getKey());

                pss.addBatch();
            }

            pss.executeBatch();
        }

        pss = con.prepareStatement("insert into tblpriv(alter_priv, create_priv,"
                + "delete_priv, drop_priv, index_priv, insert_priv, select_priv,"
                + " update_priv, user_name, db_name, tbl_name) values(?,?,?,?,?,?,?,?,?,?,?)");
        int needInsertCount = 0;

        for (String tblName : tblNames) {
            if (!tblPrivMap.containsKey(tblName)) {
                pss.setBoolean(1, alterPriv);
                pss.setBoolean(2, createPriv);
                pss.setBoolean(3, deletePriv);
                pss.setBoolean(4, dropPriv);
                pss.setBoolean(5, indexPriv);
                pss.setBoolean(6, insertPriv);
                pss.setBoolean(7, selPriv);

                pss.setBoolean(8, updatePriv);
                pss.setString(9, forWho);
                pss.setString(10, db);
                pss.setString(11, tblName);

                pss.addBatch();

                needInsertCount++;
            }
        }

        if (needInsertCount > 0) {
            pss.executeBatch();
        }

        con.commit();
        success = true;
    } catch (SQLException ex) {
        ex.printStackTrace();
        LOG.error("grant auth on db error, forWho=" + forWho + ", db=" + db + ", tbl=" + tbl + ", msg="
                + ex.getMessage());
        LOG.error(ex.getMessage());
    } finally {
        if (!success) {
            try {
                con.rollback();
            } catch (SQLException e) {
            }
        }
        closeStatement(ps);
        closeStatement(pss);
        closeConnection(con);
    }

    return success;
}