Search in sources :

Example 51 with SQLTimeoutException

use of java.sql.SQLTimeoutException in project ranger by apache.

the class PrestoClient method getSchemas.

private List<String> getSchemas(String needle, List<String> catalogs, List<String> schemas) throws HadoopException {
    List<String> ret = new ArrayList<>();
    if (con != null) {
        Statement stat = null;
        ResultSet rs = null;
        String sql = null;
        try {
            if (catalogs != null && !catalogs.isEmpty()) {
                for (String catalog : catalogs) {
                    sql = "SHOW SCHEMAS FROM \"" + StringEscapeUtils.escapeSql(catalog) + "\"";
                    try {
                        if (needle != null && !needle.isEmpty() && !needle.equals("*")) {
                            sql += " LIKE '" + StringEscapeUtils.escapeSql(needle) + "%'";
                        }
                        stat = con.createStatement();
                        rs = stat.executeQuery(sql);
                        while (rs.next()) {
                            String schema = rs.getString(1);
                            if (schemas != null && schemas.contains(schema)) {
                                continue;
                            }
                            ret.add(schema);
                        }
                    } finally {
                        close(rs);
                        close(stat);
                        rs = null;
                        stat = null;
                    }
                }
            }
        } catch (SQLTimeoutException sqlt) {
            String msgDesc = "Time Out, Unable to execute SQL [" + sql + "].";
            HadoopException hdpException = new HadoopException(msgDesc, sqlt);
            hdpException.generateResponseDataMap(false, getMessage(sqlt), msgDesc + ERR_MSG, null, null);
            if (LOG.isDebugEnabled()) {
                LOG.debug("<== PrestoClient.getSchemas() Error : ", sqlt);
            }
            throw hdpException;
        } catch (SQLException sqle) {
            String msgDesc = "Unable to execute SQL [" + sql + "].";
            HadoopException hdpException = new HadoopException(msgDesc, sqle);
            hdpException.generateResponseDataMap(false, getMessage(sqle), msgDesc + ERR_MSG, null, null);
            if (LOG.isDebugEnabled()) {
                LOG.debug("<== PrestoClient.getSchemas() Error : ", sqle);
            }
            throw hdpException;
        }
    }
    return ret;
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) SQLTimeoutException(java.sql.SQLTimeoutException) HadoopException(org.apache.ranger.plugin.client.HadoopException)

Example 52 with SQLTimeoutException

use of java.sql.SQLTimeoutException in project ranger by apache.

the class PrestoClient method getCatalogs.

private List<String> getCatalogs(String needle, List<String> catalogs) throws HadoopException {
    List<String> ret = new ArrayList<>();
    if (con != null) {
        Statement stat = null;
        ResultSet rs = null;
        String sql = "SHOW CATALOGS";
        try {
            if (needle != null && !needle.isEmpty() && !needle.equals("*")) {
                // Cannot use a prepared statement for this as presto does not support that
                sql += " LIKE '" + StringEscapeUtils.escapeSql(needle) + "%'";
            }
            stat = con.createStatement();
            rs = stat.executeQuery(sql);
            while (rs.next()) {
                String catalogName = rs.getString(1);
                if (catalogs != null && catalogs.contains(catalogName)) {
                    continue;
                }
                ret.add(catalogName);
            }
        } catch (SQLTimeoutException sqlt) {
            String msgDesc = "Time Out, Unable to execute SQL [" + sql + "].";
            HadoopException hdpException = new HadoopException(msgDesc, sqlt);
            hdpException.generateResponseDataMap(false, getMessage(sqlt), msgDesc + ERR_MSG, null, null);
        } catch (SQLException se) {
            String msg = "Unable to execute SQL [" + sql + "]. ";
            HadoopException he = new HadoopException(msg, se);
            he.generateResponseDataMap(false, getMessage(se), msg + ERR_MSG, null, null);
            throw he;
        } finally {
            close(rs);
            close(stat);
        }
    }
    return ret;
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) SQLTimeoutException(java.sql.SQLTimeoutException) HadoopException(org.apache.ranger.plugin.client.HadoopException)

Example 53 with SQLTimeoutException

use of java.sql.SQLTimeoutException in project ranger by apache.

the class PrestoClient method getTables.

private List<String> getTables(String needle, List<String> catalogs, List<String> schemas, List<String> tables) throws HadoopException {
    List<String> ret = new ArrayList<>();
    if (con != null) {
        Statement stat = null;
        ResultSet rs = null;
        String sql = null;
        if (catalogs != null && !catalogs.isEmpty() && schemas != null && !schemas.isEmpty()) {
            try {
                for (String catalog : catalogs) {
                    for (String schema : schemas) {
                        sql = "SHOW tables FROM \"" + StringEscapeUtils.escapeSql(catalog) + "\".\"" + StringEscapeUtils.escapeSql(schema) + "\"";
                        try {
                            if (needle != null && !needle.isEmpty() && !needle.equals("*")) {
                                sql += " LIKE '" + StringEscapeUtils.escapeSql(needle) + "%'";
                            }
                            stat = con.createStatement();
                            rs = stat.executeQuery(sql);
                            while (rs.next()) {
                                String table = rs.getString(1);
                                if (tables != null && tables.contains(table)) {
                                    continue;
                                }
                                ret.add(table);
                            }
                        } finally {
                            close(rs);
                            close(stat);
                            rs = null;
                            stat = null;
                        }
                    }
                }
            } catch (SQLTimeoutException sqlt) {
                String msgDesc = "Time Out, Unable to execute SQL [" + sql + "].";
                HadoopException hdpException = new HadoopException(msgDesc, sqlt);
                hdpException.generateResponseDataMap(false, getMessage(sqlt), msgDesc + ERR_MSG, null, null);
                if (LOG.isDebugEnabled()) {
                    LOG.debug("<== PrestoClient.getTables() Error : ", sqlt);
                }
                throw hdpException;
            } catch (SQLException sqle) {
                String msgDesc = "Unable to execute SQL [" + sql + "].";
                HadoopException hdpException = new HadoopException(msgDesc, sqle);
                hdpException.generateResponseDataMap(false, getMessage(sqle), msgDesc + ERR_MSG, null, null);
                if (LOG.isDebugEnabled()) {
                    LOG.debug("<== PrestoClient.getTables() Error : ", sqle);
                }
                throw hdpException;
            }
        }
    }
    return ret;
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) SQLTimeoutException(java.sql.SQLTimeoutException) HadoopException(org.apache.ranger.plugin.client.HadoopException)

Example 54 with SQLTimeoutException

use of java.sql.SQLTimeoutException in project athenz by yahoo.

the class JDBCCertRecordStoreConnectionTest method testSqlError.

@Test
public void testSqlError() throws SQLException {
    JDBCCertRecordStoreConnection jdbcConn = new JDBCCertRecordStoreConnection(mockConn);
    SQLException ex = new SQLException("sql-reason", "08S01", 9999);
    ResourceException rEx = (ResourceException) jdbcConn.sqlError(ex, "sqlError");
    assertEquals(ResourceException.INTERNAL_SERVER_ERROR, rEx.getCode());
    ex = new SQLException("sql-reason", "40001", 9999);
    rEx = (ResourceException) jdbcConn.sqlError(ex, "sqlError");
    assertEquals(ResourceException.INTERNAL_SERVER_ERROR, rEx.getCode());
    SQLTimeoutException tex = new SQLTimeoutException();
    rEx = (ResourceException) jdbcConn.sqlError(tex, "sqlError");
    assertEquals(ResourceException.SERVICE_UNAVAILABLE, rEx.getCode());
    jdbcConn.close();
}
Also used : SQLException(java.sql.SQLException) SQLTimeoutException(java.sql.SQLTimeoutException) ResourceException(com.yahoo.athenz.zts.ResourceException) Test(org.testng.annotations.Test)

Example 55 with SQLTimeoutException

use of java.sql.SQLTimeoutException in project drill by apache.

the class DrillJdbc41Factory method newServerPreparedStatement.

private DrillJdbc41PreparedStatement newServerPreparedStatement(DrillConnectionImpl connection, StatementHandle h, Meta.Signature signature, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
    String sql = signature.sql;
    try {
        DrillRpcFuture<CreatePreparedStatementResp> respFuture = connection.getClient().createPreparedStatement(signature.sql);
        CreatePreparedStatementResp resp;
        try {
            resp = respFuture.get();
        } catch (InterruptedException e) {
            // Preserve evidence that the interruption occurred so that code higher up
            // on the call stack can learn of the interruption and respond to it if it
            // wants to.
            Thread.currentThread().interrupt();
            throw new SQLException("Interrupted", e);
        }
        final RequestStatus status = resp.getStatus();
        if (status != RequestStatus.OK) {
            final String errMsgFromServer = resp.getError() != null ? resp.getError().getMessage() : "";
            if (status == RequestStatus.TIMEOUT) {
                logger.error("Request timed out to create prepare statement: {}", errMsgFromServer);
                throw new SQLTimeoutException("Failed to create prepared statement: " + errMsgFromServer);
            }
            if (status == RequestStatus.FAILED) {
                logger.error("Failed to create prepared statement: {}", errMsgFromServer);
                throw new SQLException("Failed to create prepared statement: " + errMsgFromServer);
            }
            logger.error("Failed to create prepared statement. Unknown status: {}, Error: {}", status, errMsgFromServer);
            throw new SQLException(String.format("Failed to create prepared statement. Unknown status: %s, Error: %s", status, errMsgFromServer));
        }
        return new DrillJdbc41PreparedStatement(connection, h, signature, resp.getPreparedStatement(), resultSetType, resultSetConcurrency, resultSetHoldability);
    } catch (SQLException e) {
        throw e;
    } catch (Exception e) {
        throw Helper.INSTANCE.createException("Error while preparing statement [" + sql + "]", e);
    }
}
Also used : SQLException(java.sql.SQLException) SQLTimeoutException(java.sql.SQLTimeoutException) CreatePreparedStatementResp(org.apache.drill.exec.proto.UserProtos.CreatePreparedStatementResp) SQLTimeoutException(java.sql.SQLTimeoutException) SQLException(java.sql.SQLException) RequestStatus(org.apache.drill.exec.proto.UserProtos.RequestStatus)

Aggregations

SQLTimeoutException (java.sql.SQLTimeoutException)56 SQLException (java.sql.SQLException)39 ResultSet (java.sql.ResultSet)20 Statement (java.sql.Statement)17 Connection (java.sql.Connection)15 Test (org.testng.annotations.Test)15 Test (org.junit.Test)14 BaseTest (util.BaseTest)14 PreparedStatement (java.sql.PreparedStatement)12 JdbcTest (org.apache.drill.categories.JdbcTest)8 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)7 ArrayList (java.util.ArrayList)7 HadoopException (org.apache.ranger.plugin.client.HadoopException)6 ScreenCreator (org.apache.drill.exec.physical.impl.ScreenCreator)4 MessageEvent (org.cerberus.engine.entity.MessageEvent)4 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)3 TimeoutException (java.util.concurrent.TimeoutException)3 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 Ignore (org.junit.Ignore)3 IOException (java.io.IOException)2