use of java.sql.SQLWarning in project tomee by apache.
the class ImportSql method importSql.
private void importSql(final URL script, final Statement statement) {
final BufferedReader bufferedReader;
try {
bufferedReader = new BufferedReader(new InputStreamReader(new BufferedInputStream(script.openStream())));
} catch (final IOException e) {
LOGGER.error("can't open " + script.toExternalForm(), e);
return;
}
try {
for (String sql = bufferedReader.readLine(); sql != null; sql = bufferedReader.readLine()) {
String trimmedSql = sql.trim();
// empty or comment
if (trimmedSql.isEmpty() || trimmedSql.startsWith("--") || trimmedSql.startsWith("//") || trimmedSql.startsWith("/*")) {
continue;
}
if (trimmedSql.endsWith(";")) {
trimmedSql = trimmedSql.substring(0, trimmedSql.length() - 1);
}
try {
if (!trimmedSql.toLowerCase(Locale.ENGLISH).startsWith("select")) {
statement.executeUpdate(trimmedSql);
} else {
// why could it be the case?
statement.executeQuery(trimmedSql);
}
SQLWarning warnings = statement.getWarnings();
while (warnings != null) {
LOGGER.warning(warnings.getMessage());
warnings = warnings.getNextWarning();
}
} catch (final SQLException e) {
LOGGER.error("error importing script " + script.toExternalForm(), e);
}
}
} catch (final IOException e) {
LOGGER.error("can't import " + script.toExternalForm(), e);
}
}
use of java.sql.SQLWarning in project hive by apache.
the class Commands method showRemainingLogsIfAny.
private void showRemainingLogsIfAny(Statement statement) {
if (statement instanceof HiveStatement) {
HiveStatement hiveStatement = (HiveStatement) statement;
List<String> logs = null;
do {
try {
logs = hiveStatement.getQueryLog();
} catch (SQLException e) {
beeLine.error(new SQLWarning(e));
return;
}
for (String log : logs) {
if (!beeLine.isTestMode()) {
beeLine.info(log);
} else {
// In test mode print the logs to the output
beeLine.output(log);
}
}
} while (logs.size() > 0);
} else {
beeLine.debug("The statement instance is not HiveStatement type: " + statement.getClass());
}
}
use of java.sql.SQLWarning in project hive by apache.
the class TestJdbcDriver2 method testAutoCommit.
@Test
public void testAutoCommit() throws Exception {
con.clearWarnings();
con.setAutoCommit(true);
assertNull(con.getWarnings());
con.setAutoCommit(false);
SQLWarning warning = con.getWarnings();
assertNotNull(warning);
assertEquals("Hive does not support autoCommit=false", warning.getMessage());
assertNull(warning.getNextWarning());
con.clearWarnings();
}
use of java.sql.SQLWarning in project hive by apache.
the class HiveConnection method setAutoCommit.
/*
* (non-Javadoc)
*
* @see java.sql.Connection#setAutoCommit(boolean)
*/
@Override
public void setAutoCommit(boolean autoCommit) throws SQLException {
// Per JDBC spec, if the connection is closed a SQLException should be thrown.
if (isClosed) {
throw new SQLException("Connection is closed");
}
// if setAutoCommit is called and the auto-commit mode is not changed, the call is a no-op.
if (!autoCommit) {
LOG.warn("Request to set autoCommit to false; Hive does not support autoCommit=false.");
SQLWarning warning = new SQLWarning("Hive does not support autoCommit=false");
if (warningChain == null) {
warningChain = warning;
} else {
warningChain.setNextWarning(warning);
}
}
}
use of java.sql.SQLWarning in project spring-framework by spring-projects.
the class ScriptUtils method executeSqlScript.
/**
* Execute the given SQL script.
* <p>Statement separators and comments will be removed before executing
* individual statements within the supplied script.
* <p><strong>Warning</strong>: this method does <em>not</em> release the
* provided {@link Connection}.
* @param connection the JDBC connection to use to execute the script; already
* configured and ready to use
* @param resource the resource (potentially associated with a specific encoding)
* to load the SQL script from
* @param continueOnError whether or not to continue without throwing an exception
* in the event of an error
* @param ignoreFailedDrops whether or not to continue in the event of specifically
* an error on a {@code DROP} statement
* @param commentPrefixes the prefixes that identify single-line comments in the
* SQL script (typically "--")
* @param separator the script statement separator; defaults to
* {@value #DEFAULT_STATEMENT_SEPARATOR} if not specified and falls back to
* {@value #FALLBACK_STATEMENT_SEPARATOR} as a last resort; may be set to
* {@value #EOF_STATEMENT_SEPARATOR} to signal that the script contains a
* single statement without a separator
* @param blockCommentStartDelimiter the <em>start</em> block comment delimiter
* @param blockCommentEndDelimiter the <em>end</em> block comment delimiter
* @throws ScriptException if an error occurred while executing the SQL script
* @since 5.2
* @see #DEFAULT_STATEMENT_SEPARATOR
* @see #FALLBACK_STATEMENT_SEPARATOR
* @see #EOF_STATEMENT_SEPARATOR
* @see org.springframework.jdbc.datasource.DataSourceUtils#getConnection
* @see org.springframework.jdbc.datasource.DataSourceUtils#releaseConnection
*/
public static void executeSqlScript(Connection connection, EncodedResource resource, boolean continueOnError, boolean ignoreFailedDrops, String[] commentPrefixes, @Nullable String separator, String blockCommentStartDelimiter, String blockCommentEndDelimiter) throws ScriptException {
try {
if (logger.isDebugEnabled()) {
logger.debug("Executing SQL script from " + resource);
}
long startTime = System.currentTimeMillis();
String script;
try {
script = readScript(resource, separator, commentPrefixes, blockCommentEndDelimiter);
} catch (IOException ex) {
throw new CannotReadScriptException(resource, ex);
}
if (separator == null) {
separator = DEFAULT_STATEMENT_SEPARATOR;
}
if (!EOF_STATEMENT_SEPARATOR.equals(separator) && !containsStatementSeparator(resource, script, separator, commentPrefixes, blockCommentStartDelimiter, blockCommentEndDelimiter)) {
separator = FALLBACK_STATEMENT_SEPARATOR;
}
List<String> statements = new ArrayList<>();
splitSqlScript(resource, script, separator, commentPrefixes, blockCommentStartDelimiter, blockCommentEndDelimiter, statements);
int stmtNumber = 0;
Statement stmt = connection.createStatement();
try {
for (String statement : statements) {
stmtNumber++;
try {
stmt.execute(statement);
int rowsAffected = stmt.getUpdateCount();
if (logger.isDebugEnabled()) {
logger.debug(rowsAffected + " returned as update count for SQL: " + statement);
SQLWarning warningToLog = stmt.getWarnings();
while (warningToLog != null) {
logger.debug("SQLWarning ignored: SQL state '" + warningToLog.getSQLState() + "', error code '" + warningToLog.getErrorCode() + "', message [" + warningToLog.getMessage() + "]");
warningToLog = warningToLog.getNextWarning();
}
}
} catch (SQLException ex) {
boolean dropStatement = StringUtils.startsWithIgnoreCase(statement.trim(), "drop");
if (continueOnError || (dropStatement && ignoreFailedDrops)) {
if (logger.isDebugEnabled()) {
logger.debug(ScriptStatementFailedException.buildErrorMessage(statement, stmtNumber, resource), ex);
}
} else {
throw new ScriptStatementFailedException(statement, stmtNumber, resource, ex);
}
}
}
} finally {
try {
stmt.close();
} catch (Throwable ex) {
logger.trace("Could not close JDBC Statement", ex);
}
}
long elapsedTime = System.currentTimeMillis() - startTime;
if (logger.isDebugEnabled()) {
logger.debug("Executed SQL script from " + resource + " in " + elapsedTime + " ms.");
}
} catch (Exception ex) {
if (ex instanceof ScriptException) {
throw (ScriptException) ex;
}
throw new UncategorizedScriptException("Failed to execute database script from resource [" + resource + "]", ex);
}
}
Aggregations