Example usage for java.sql Connection setClientInfo

List of usage examples for java.sql Connection setClientInfo

Introduction

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

Prototype

void setClientInfo(String name, String value) throws SQLClientInfoException;

Source Link

Document

Sets the value of the client info property specified by name to the value specified by value.

Usage

From source file:org.apache.hadoop.hive.ql.dataToDB.BaseDBExternalDataLoad.java

private ResultSetMetaData getMetaData() throws SemanticException {
    Connection conn = null;
    try {/*from w ww .jav  a  2  s  . c  o  m*/
        String sql = "select * from " + config.getDbTable();
        Class.forName(config.getDriver());
        conn = DriverManager.getConnection(config.getUrl(), config.getUser(), config.getPwd());
        try {
            String processName = java.lang.management.ManagementFactory.getRuntimeMXBean().getName();
            String processID = processName.substring(0, processName.indexOf('@'));
            String appinfo = "getMetaData_" + processID + "_" + SessionState.get().getSessionName();
            conn.setClientInfo("ApplicationName", appinfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        conn.setAutoCommit(false);
        Statement s = conn.createStatement();
        s.setFetchSize(1);
        ResultSet set = s.executeQuery(sql);
        conn.commit();
        ResultSetMetaData meta = set.getMetaData();
        return meta;

    } catch (Exception e) {
        throw new SemanticException(e.getMessage());
    }
}

From source file:org.apache.hadoop.hive.ql.dataToDB.StoreAsPgdata.java

public Connection sendCreatTableToPg(Table tbl) throws SemanticException {
    Connection conn = null;
    checkProperty(tbl);/*from  w ww  .j a va 2 s  .  c o m*/

    ExtractConfig config = new ExtractConfig();
    setConfig(tbl, config);
    String sql = config.getSql();
    log.info("url: " + config.getUrl());
    log.info("user: " + config.getUser());
    log.info("pwd: " + config.getPwd());
    log.info("sql: " + sql);
    try {
        if (sql == null || sql.equals("")) {
            throw new Exception(Constants.TBALE_SQL + " and " + Constants.TABLE_NAME + " both empty!");
        }

        conn = DriverManager.getConnection(config.getUrl(), config.getUser(), config.getPwd());
        try {
            String processName = java.lang.management.ManagementFactory.getRuntimeMXBean().getName();
            String processID = processName.substring(0, processName.indexOf('@'));
            String appinfo = "sendCreatTableToPg_" + processID + "_" + SessionState.get().getSessionName();
            conn.setClientInfo("ApplicationName", appinfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        conn.setAutoCommit(false);
        Statement s = conn.createStatement();

        s.execute(sql);
    } catch (Exception e) {
        try {
            if (conn != null) {
                conn.rollback();
            }
            throw new SemanticException(e.getMessage());
        } catch (Exception exc) {
            throw new SemanticException(exc.getMessage());
        }
    }
    return conn;
}

From source file:org.apache.hadoop.hive.ql.dataToDB.StoreAsPgdata.java

public Connection sendCreatTableToPg(Table tbl, String sql) throws SemanticException {
    Connection conn = null;
    checkProperty(tbl);//from   ww w .ja v a 2  s  .co m

    ExtractConfig config = new ExtractConfig();
    setConfig(tbl, config);
    log.info("url: " + config.getUrl());
    log.info("user: " + config.getUser());
    log.info("pwd: " + config.getPwd());
    log.info("sql: " + sql);
    try {
        if (sql == null || sql.equals("")) {
            throw new Exception(Constants.TBALE_SQL + " and " + Constants.TABLE_NAME + " both empty!");
        }

        conn = DriverManager.getConnection(config.getUrl(), config.getUser(), config.getPwd());
        try {
            String processName = java.lang.management.ManagementFactory.getRuntimeMXBean().getName();
            String processID = processName.substring(0, processName.indexOf('@'));
            String appinfo = "sendCreatTableToPg_" + processID + "_" + SessionState.get().getSessionName();
            conn.setClientInfo("ApplicationName", appinfo);
        } catch (Exception e) {
            e.printStackTrace();
        }

        conn.setAutoCommit(false);
        stat = conn.createStatement();

        stat.execute(sql);
    } catch (Exception e) {
        try {
            if (conn != null) {
                conn.rollback();
            }
            throw new SemanticException(e.getMessage());
        } catch (Exception exc) {
            throw new SemanticException(exc.getMessage());
        }
    }
    return conn;
}

From source file:org.apache.hadoop.hive.ql.dataToDB.StoreAsPgdata.java

public Connection sendDropTableToPg(Table tbl) throws SemanticException {
    Connection conn = null;
    checkProperty(tbl);//from w  ww.  j a  va2s  . co  m
    try {
        ExtractConfig config = new ExtractConfig();
        setConfig(tbl, config);
        String table = tbl.getName();
        String sql = "drop table " + table;

        log.info("get connection");
        log.info("url: " + config.getUrl());
        log.info("user: " + config.getUser());
        log.info("pwd: " + config.getPwd());

        conn = DriverManager.getConnection(config.getUrl(), config.getUser(), config.getPwd());
        try {
            String processName = java.lang.management.ManagementFactory.getRuntimeMXBean().getName();
            String processID = processName.substring(0, processName.indexOf('@'));
            String appinfo = "sendDropTableToPg_" + processID + "_" + SessionState.get().getSessionName();
            conn.setClientInfo("ApplicationName", appinfo);
        } catch (Exception e) {
            e.printStackTrace();
        }

        conn.setAutoCommit(false);
        log.info("setAutoCommit");

        Statement s = conn.createStatement();
        log.info("createStatement");

        boolean ret;
        ret = s.execute(sql);

        log.info("executeQuery");

    } catch (Exception e) {
        String errStr = e.getMessage().toString();
        if (errStr.contains("not exist")) {
            log.info("the tdw external table is drop in pg");
            return conn;
        } else {
            try {
                if (conn != null) {
                    conn.rollback();
                }
                throw new SemanticException(e.getMessage());
            } catch (Exception exc) {
                throw new SemanticException(exc.getMessage());
            }
        }
    }

    return conn;
}

From source file:org.apache.hadoop.hive.ql.dataToDB.StoreAsPgdata.java

public int sendTruncateTableToPg(Table tbl) throws SemanticException {
    Connection conn = null;
    checkProperty(tbl);//from   w  w w.ja  v a2s . c  o m
    try {
        ExtractConfig config = new ExtractConfig();
        setConfig(tbl, config);
        String table = tbl.getName();
        String sql = "truncate table " + table;
        log.info("sql: " + sql);

        conn = DriverManager.getConnection(config.getUrl(), config.getUser(), config.getPwd());
        try {
            String processName = java.lang.management.ManagementFactory.getRuntimeMXBean().getName();
            String processID = processName.substring(0, processName.indexOf('@'));
            String appinfo = "sendTruncateTableToPg_" + processID + "_" + SessionState.get().getSessionName();
            conn.setClientInfo("ApplicationName", appinfo);
        } catch (Exception e) {
            e.printStackTrace();
        }

        conn.setAutoCommit(false);
        log.info("setAutoCommit");

        Statement s = conn.createStatement();
        log.info("createStatement");

        s.execute(sql);
        conn.commit();
        if (conn != null) {
            conn.close();
        }
    } catch (Exception e) {
        try {
            if (conn != null) {
                conn.rollback();
            }
            throw new SemanticException(e.getMessage());
        } catch (Exception exc) {
            throw new SemanticException(exc.getMessage());
        }
    }

    return 0;
}

From source file:org.apache.hadoop.hive.ql.dataToDB.StoreAsPgdata.java

public int sendUpdateTableToPg(Table tbl, String sql) throws SemanticException {
    Connection conn = null;
    checkProperty(tbl);/*from  ww  w. j a  v a2 s  .  co m*/
    try {
        ExtractConfig config = new ExtractConfig();
        setConfig(tbl, config);
        log.info("update sql: " + sql);

        conn = DriverManager.getConnection(config.getUrl(), config.getUser(), config.getPwd());
        try {
            String processName = java.lang.management.ManagementFactory.getRuntimeMXBean().getName();
            String processID = processName.substring(0, processName.indexOf('@'));
            String appinfo = "sendUpdateTableToPg_" + processID + "_" + SessionState.get().getSessionName();
            conn.setClientInfo("ApplicationName", appinfo);
        } catch (Exception e) {
            e.printStackTrace();
        }

        conn.setAutoCommit(false);
        log.info("setAutoCommit");

        Statement s = conn.createStatement();
        log.info("createStatement");

        s.execute(sql);
        conn.commit();
        if (conn != null) {
            conn.close();
        }
    } catch (Exception e) {
        try {
            if (conn != null) {
                conn.rollback();
            }
            throw new SemanticException(e.getMessage());
        } catch (Exception exc) {
            throw new SemanticException(exc.getMessage());
        }
    }

    return 0;
}

From source file:org.apache.hadoop.hive.ql.dataToDB.StoreAsPgdata.java

public int sendDeleteTableToPg(Table tbl, String sql) throws SemanticException {
    Connection conn = null;
    checkProperty(tbl);//from   ww  w  .j ava  2 s  . c  om
    try {
        ExtractConfig config = new ExtractConfig();
        setConfig(tbl, config);
        log.info("delete sql: " + sql);

        conn = DriverManager.getConnection(config.getUrl(), config.getUser(), config.getPwd());
        try {
            String processName = java.lang.management.ManagementFactory.getRuntimeMXBean().getName();
            String processID = processName.substring(0, processName.indexOf('@'));
            String appinfo = "sendDeleteTableToPg_" + processID + "_" + SessionState.get().getSessionName();
            conn.setClientInfo("ApplicationName", appinfo);
        } catch (Exception e) {
            e.printStackTrace();
        }

        conn.setAutoCommit(false);
        log.info("setAutoCommit");

        Statement s = conn.createStatement();
        log.info("createStatement");

        s.execute(sql);
        conn.commit();
        if (conn != null) {
            conn.close();
        }
    } catch (Exception e) {
        try {
            if (conn != null) {
                conn.rollback();
            }
            throw new SemanticException(e.getMessage());
        } catch (Exception exc) {
            throw new SemanticException(exc.getMessage());
        }
    }

    return 0;
}

From source file:org.apache.hadoop.hive.ql.dataToDB.StoreAsPgdata.java

public int sendDelteTableToPg(Table tbl) throws SemanticException {
    Connection conn = null;
    checkProperty(tbl);//  w  ww .  j av  a 2  s.c  o m
    try {
        ExtractConfig config = new ExtractConfig();
        setConfig(tbl, config);
        String table = tbl.getName();
        String sql = "delete table " + table;

        conn = DriverManager.getConnection(config.getUrl(), config.getUser(), config.getPwd());
        try {
            String processName = java.lang.management.ManagementFactory.getRuntimeMXBean().getName();
            String processID = processName.substring(0, processName.indexOf('@'));
            String appinfo = "sendDelteTableToPg_" + processID + "_" + SessionState.get().getSessionName();
            conn.setClientInfo("ApplicationName", appinfo);
        } catch (Exception e) {
            e.printStackTrace();
        }

        conn.setAutoCommit(false);
        log.info("setAutoCommit");

        Statement s = conn.createStatement();
        log.info("createStatement");

        s.execute(sql);
        conn.commit();
        if (conn != null) {
            conn.close();
        }
    } catch (Exception e) {
        try {
            if (conn != null) {
                conn.rollback();
            }
            throw new SemanticException(e.getMessage());
        } catch (Exception exc) {
            throw new SemanticException(exc.getMessage());
        }
    }

    return 0;
}

From source file:org.apache.hadoop.hive.ql.metadata.BIStore.java

public Connection openConnect() {
    try {/*  ww  w.ja v  a2s  .c o m*/
        Class.forName("org.postgresql.Driver");
    } catch (ClassNotFoundException e) {
        LOG.error(" get org.postgresql.Driver failed ");
        e.printStackTrace();
    }
    Connection conn = null;
    try {
        LOG.debug(" Connecting: " + url);
        DriverManager.setLoginTimeout(waittime);
        conn = DriverManager.getConnection(url, this.prop);
        try {
            String processName = java.lang.management.ManagementFactory.getRuntimeMXBean().getName();
            String processID = processName.substring(0, processName.indexOf('@'));
            String appinfo = "openConnect_" + processID + "_" + SessionState.get().getSessionName();
            conn.setClientInfo("ApplicationName", appinfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        LOG.debug(" get Connection ok: " + url);
    } catch (SQLException e) {
        LOG.error(" get Connection failed: " + url);
        e.printStackTrace();
    }
    return conn;
}

From source file:org.apache.hadoop.hive.ql.metadata.BIStore.java

public Connection openConnect(int timeout) {
    try {/*from  w w  w .j  a va 2  s .com*/
        Class.forName("org.postgresql.Driver");
    } catch (ClassNotFoundException e) {
        LOG.error(" get org.postgresql.Driver failed ");
        e.printStackTrace();
    }
    Connection conn = null;
    try {
        LOG.info(" Connecting: " + url);
        DriverManager.setLoginTimeout(timeout);
        this.prop.setProperty("loginTimeout", String.valueOf(timeout));
        conn = DriverManager.getConnection(url, this.prop);
        try {
            String processName = java.lang.management.ManagementFactory.getRuntimeMXBean().getName();
            String processID = processName.substring(0, processName.indexOf('@'));
            String appinfo = "openConnect_" + processID + "_" + SessionState.get().getSessionName();
            conn.setClientInfo("ApplicationName", appinfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        LOG.info(" get Connection ok: " + url);
    } catch (SQLException e) {
        LOG.error(" get Connection failed: " + url);
        e.printStackTrace();
    }
    return conn;
}