use of java.sql.SQLWarning in project thingsboard by thingsboard.
the class AbstractSqlTsDatabaseUpgradeService method executeQuery.
protected void executeQuery(Connection conn, String query) {
try {
Statement statement = conn.createStatement();
// NOSONAR, ignoring because method used to execute thingsboard database upgrade script
statement.execute(query);
SQLWarning warnings = statement.getWarnings();
if (warnings != null) {
log.info("{}", warnings.getMessage());
SQLWarning nextWarning = warnings.getNextWarning();
while (nextWarning != null) {
log.info("{}", nextWarning.getMessage());
nextWarning = nextWarning.getNextWarning();
}
}
Thread.sleep(2000);
log.info("Successfully executed query: {}", query);
} catch (InterruptedException | SQLException e) {
log.error("Failed to execute query: {} due to: {}", query, e.getMessage());
throw new RuntimeException("Failed to execute query:" + query + " due to: ", e);
}
}
use of java.sql.SQLWarning in project invesdwin-context-persistence by subes.
the class MySqlLoadDataInfile method internalPersist.
@SuppressWarnings("GuardedBy")
@Transactional
private int internalPersist(final StringBuilder workFileAndOutputStream) throws SQLException {
try (Connection conn = ds.getConnection()) {
try (com.mysql.cj.jdbc.JdbcStatement stmt = conn.createStatement().unwrap(com.mysql.cj.jdbc.JdbcStatement.class)) {
final boolean prevAutoCommit = conn.getAutoCommit();
if (disabledChecks) {
if (prevAutoCommit) {
stmt.execute("set autocommit=0");
}
stmt.execute("set unique_checks=0");
stmt.execute("set foreign_key_checks=0");
}
final String query = createQuery("memoryFile.txt");
// http://jeffrick.com/2010/03/23/bulk-insert-into-a-mysql-database/
stmt.setLocalInfileInputStream(IOUtils.toInputStream(workFileAndOutputStream.toString(), Charset.defaultCharset()));
final int countUpdated = stmt.executeUpdate(query);
final SQLWarning warnings = conn.getWarnings();
if (disabledChecks) {
stmt.execute("set foreign_key_checks=1");
stmt.execute("set unique_checks=1");
if (prevAutoCommit) {
stmt.execute("set autocommit=1");
}
}
if (warnings != null) {
throw warnings;
}
return countUpdated;
}
}
}
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 commentPrefix the prefix that identifies 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; never
* {@code null} or empty
* @param blockCommentEndDelimiter the <em>end</em> block comment delimiter; never
* {@code null} or empty
* @throws ScriptException if an error occurred while executing the SQL script
* @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 commentPrefix, String separator, String blockCommentStartDelimiter, String blockCommentEndDelimiter) throws ScriptException {
try {
if (logger.isInfoEnabled()) {
logger.info("Executing SQL script from " + resource);
}
long startTime = System.currentTimeMillis();
String script;
try {
script = readScript(resource, commentPrefix, separator);
} catch (IOException ex) {
throw new CannotReadScriptException(resource, ex);
}
if (separator == null) {
separator = DEFAULT_STATEMENT_SEPARATOR;
}
if (!EOF_STATEMENT_SEPARATOR.equals(separator) && !containsSqlScriptDelimiters(script, separator)) {
separator = FALLBACK_STATEMENT_SEPARATOR;
}
List<String> statements = new LinkedList<>();
splitSqlScript(resource, script, separator, commentPrefix, 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.debug("Could not close JDBC Statement", ex);
}
}
long elapsedTime = System.currentTimeMillis() - startTime;
if (logger.isInfoEnabled()) {
logger.info("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);
}
}
use of java.sql.SQLWarning in project spring-framework by spring-projects.
the class JdbcTemplate method handleWarnings.
/**
* Throw an SQLWarningException if we're not ignoring warnings,
* else log the warnings (at debug level).
* @param stmt the current JDBC statement
* @throws SQLWarningException if not ignoring warnings
* @see org.springframework.jdbc.SQLWarningException
*/
protected void handleWarnings(Statement stmt) throws SQLException {
if (isIgnoreWarnings()) {
if (logger.isDebugEnabled()) {
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();
}
}
} else {
handleWarnings(stmt.getWarnings());
}
}
use of java.sql.SQLWarning in project robovm by robovm.
the class OldConnectionTest method testGetWarnings.
// TODO GetWarnings is not supported: returns null
public void testGetWarnings() throws Exception {
Statement st = null;
int errorCode1 = -1;
int errorCode2 = -1;
try {
st = conn.createStatement();
st.execute("select animals from zoooo");
fail("SQLException was not thrown");
} catch (SQLException e) {
// expected
errorCode1 = e.getErrorCode();
}
SQLWarning wrs = conn.getWarnings();
assertNull(wrs);
// tests implementation: but errorcodes need to change too -> change impl.
/*
Statement st = null;
int errorCode1 = -1;
int errorCode2 = -1;
try {
st = conn.createStatement();
st.execute("select animals from zoooo");
fail("SQLException was not thrown");
} catch (SQLException e) {
// expected
errorCode1 = e.getErrorCode();
}
try {
SQLWarning wrs = conn.getWarnings();
assertNotNull(wrs);
assertEquals(errorCode1, wrs.getErrorCode());
assertNull(wrs.getNextWarning());
} catch (Exception e) {
fail("Unexpected Exception: " + e.getMessage());
}
try {
st.execute("select horse from zoooooo");
} catch (SQLException e) {
// expected
errorCode2 = e.getErrorCode();
}
try {
SQLWarning wrs = conn.getWarnings();
assertEquals(errorCode1, wrs.getErrorCode());
assertNotNull(wrs.getNextWarning());
assertEquals(errorCode2, wrs.getErrorCode());
} catch (Exception e) {
fail("Unexpected Exception: " + e.getMessage());
}
try {
st.close();
} catch (SQLException ee) {
}
*/
conn.close();
try {
conn.getWarnings();
fail("Exception expected");
} catch (SQLException e) {
//ok
}
}
Aggregations