Java Code Examples for java.sql.SQLWarning#getNextWarning()

The following examples show how to use java.sql.SQLWarning#getNextWarning() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DatasourceCrashCallable.java    From MyTown2 with The Unlicense 6 votes vote down vote up
@Override
public String call() throws Exception {
    MyTownDatasource datasource = MyTown.instance.datasource;
    if (datasource == null) {
        return "Datasource is not initialized yet";
    }
    String str = "";

    str += String.format("Class: %s\n", datasource.getClass().getName());
    str += String.format("Stats (Towns: %s, Residents: %s, Nations: %s, Blocks: %s, Ranks: %s, Plots: %s)\n", MyTownUniverse.instance.towns.size(), MyTownUniverse.instance.residents.size(), 0 /*MyTownUniverse.instance.getNationsMap().size()*/, MyTownUniverse.instance.blocks.size(), MyTownUniverse.instance.ranks.size(), MyTownUniverse.instance.plots.size());
    Connection conn = datasource.getBridge().getConnection();

    str += String.format("AutoCommit: %s%n", conn.getAutoCommit());
    str += String.format("----- SQL Warnings -----%n");
    str += String.format("%s8 | %s9 | %s%n", "SQLState", "ErrorCode", "Message");
    SQLWarning sqlWarning = conn.getWarnings();
    do {
        str += String.format("%s8 | %s9 | %s%n", sqlWarning.getSQLState(), sqlWarning.getErrorCode(), sqlWarning.getMessage());
    } while (sqlWarning.getNextWarning() != null);
    return str;
}
 
Example 2
Source File: JdbcTemplate.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * 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());
	}
}
 
Example 3
Source File: SQLWarningTests.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Validate that the ordering of the returned SQLWarning is correct using
 * traditional while loop
 */
@Test
public void test14() {
    SQLWarning ex = new SQLWarning("Warning 1", t1);
    SQLWarning ex1 = new SQLWarning("Warning 2");
    SQLWarning ex2 = new SQLWarning("Warning 3", t2);
    ex.setNextWarning(ex1);
    ex.setNextWarning(ex2);
    int num = 0;
    SQLWarning sqe = ex;
    while (sqe != null) {
        assertTrue(warnings[num++].equals(sqe.getMessage()));
        Throwable c = sqe.getCause();
        while (c != null) {
            assertTrue(msgs[num++].equals(c.getMessage()));
            c = c.getCause();
        }
        sqe = sqe.getNextWarning();
    }
}
 
Example 4
Source File: SQLWarningTests.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Validate that the ordering of the returned SQLWarning is correct using
 * traditional while loop
 */
@Test
public void test14() {
    SQLWarning ex = new SQLWarning("Warning 1", t1);
    SQLWarning ex1 = new SQLWarning("Warning 2");
    SQLWarning ex2 = new SQLWarning("Warning 3", t2);
    ex.setNextWarning(ex1);
    ex.setNextWarning(ex2);
    int num = 0;
    SQLWarning sqe = ex;
    while (sqe != null) {
        assertTrue(warnings[num++].equals(sqe.getMessage()));
        Throwable c = sqe.getCause();
        while (c != null) {
            assertTrue(msgs[num++].equals(c.getMessage()));
            c = c.getCause();
        }
        sqe = sqe.getNextWarning();
    }
}
 
Example 5
Source File: GFXDServiceImpl.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
private GFXDExceptionData gfxdWarning(SQLWarning warnings)
    throws SQLException {
  GFXDExceptionData warningData = new GFXDExceptionData(
      warnings.getMessage(), warnings.getSQLState(), warnings.getErrorCode());
  ArrayList<GFXDExceptionData> nextWarnings = null;
  SQLWarning next = warnings.getNextWarning();
  if (next != null) {
    nextWarnings = new ArrayList<GFXDExceptionData>();
    do {
      nextWarnings.add(new GFXDExceptionData(next.getMessage(), next
          .getSQLState(), next.getErrorCode()));
    } while ((next = next.getNextWarning()) != null);
  }
  //GFXDExceptionData sqlw = new GFXDExceptionData(warningData);
  //sqlw.setNextWarnings(nextWarnings);
  if (nextWarnings != null) {
    warningData.setReason(warningData.getReason() + nextWarnings.toString());
  }
  return warningData;
}
 
Example 6
Source File: JdbcTemplate.java    From effectivejava with Apache License 2.0 6 votes vote down vote up
/**
 * 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());
	}
}
 
Example 7
Source File: BaseJdbcObject.java    From cassandra-jdbc-driver with Apache License 2.0 6 votes vote down vote up
/**
 * Append a warning to the end of chained SQL warnings.
 *
 * @param warning warning to be appended to the warnings list
 */
public void appendWarning(SQLWarning warning) {
    if (warning != null) {
        if (_warning == null) {
            _warning = warning;
        } else {
            SQLWarning w = _warning;
            SQLWarning prevWarning = w;
            do {
                if (w == warning) {
                    break;
                } else {
                    prevWarning = w;
                }
            } while ((w = w.getNextWarning()) != null);

            if (prevWarning != w) {
                prevWarning.setNextWarning(warning);
            }
        }
    }
}
 
Example 8
Source File: SQLWarningTests.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Validate that the ordering of the returned SQLWarning is correct using
 * traditional while loop
 */
@Test
public void test14() {
    SQLWarning ex = new SQLWarning("Warning 1", t1);
    SQLWarning ex1 = new SQLWarning("Warning 2");
    SQLWarning ex2 = new SQLWarning("Warning 3", t2);
    ex.setNextWarning(ex1);
    ex.setNextWarning(ex2);
    int num = 0;
    SQLWarning sqe = ex;
    while (sqe != null) {
        assertTrue(warnings[num++].equals(sqe.getMessage()));
        Throwable c = sqe.getCause();
        while (c != null) {
            assertTrue(msgs[num++].equals(c.getMessage()));
            c = c.getCause();
        }
        sqe = sqe.getNextWarning();
    }
}
 
Example 9
Source File: JdbcTemplate.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * 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());
	}
}
 
Example 10
Source File: SQLWarningTests.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Validate that the ordering of the returned SQLWarning is correct using
 * traditional while loop
 */
@Test
public void test14() {
    SQLWarning ex = new SQLWarning("Warning 1", t1);
    SQLWarning ex1 = new SQLWarning("Warning 2");
    SQLWarning ex2 = new SQLWarning("Warning 3", t2);
    ex.setNextWarning(ex1);
    ex.setNextWarning(ex2);
    int num = 0;
    SQLWarning sqe = ex;
    while (sqe != null) {
        assertTrue(warnings[num++].equals(sqe.getMessage()));
        Throwable c = sqe.getCause();
        while (c != null) {
            assertTrue(msgs[num++].equals(c.getMessage()));
            c = c.getCause();
        }
        sqe = sqe.getNextWarning();
    }
}
 
Example 11
Source File: JDBCDisplayUtil.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
static public void ShowWarnings(PrintStream out, SQLWarning warning) {
	while (warning != null) {
		out.println("WARNING "+
			mapNull(warning.getSQLState(),"(no SQLState)")+": "+
			mapNull(warning.getMessage(),"(no message)"));
		warning = warning.getNextWarning();
	}
}
 
Example 12
Source File: BaseSQLQueryFactory.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
protected void logWarnings(PreparedStatement pstmt)
throws SQLException {
  SQLWarning warning = pstmt.getWarnings();
  while (warning != null) {
    String message = warning.getMessage();
    //String sqlState = warning.getSQLState();
    int errorCode = warning.getErrorCode();
    Log.getLogWriter().warning("While executing prepared statement: " + pstmt
                             + " got error code: " + errorCode
                             + " for: " + message);
    warning = warning.getNextWarning();
  }
}
 
Example 13
Source File: CastingTest.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * <p>
 * Check the results for the queries in testDataTruncation().
 * </p>
 *
 * <p>
 * The method expects a query that returns three rows with columns of a
 * character string or binary string data type, where some of the values
 * are cast to a narrower data type.
 * </p>
 *
 * <p>
 * Expect the following truncations to have taken place:
 * </p>
 *
 * <ol>
 * <li>Row 1, column 1: truncated from 3 to 2 bytes</li>
 * <li>Row 3, column 1: truncated from 3 to 2 bytes</li>
 * <li>Row 3, column 2: truncated from 4 to 2 bytes</li>
 * </ol>
 */
private void checkDataTruncationResult(Statement s, String sql)
        throws SQLException {
    ResultSet rs = s.executeQuery(sql);

    // First row should have one warning (column 1)
    assertTrue(rs.next());
    SQLWarning w = rs.getWarnings();
    assertDataTruncation(w, -1, true, false, 3, 2);
    w = w.getNextWarning();
    assertNull(w);
    rs.clearWarnings(); // workaround for DERBY-5765

    // Second row should have no warnings
    assertTrue(rs.next());
    assertNull(rs.getWarnings());

    // Third row should have two warnings (column 1 and 2)
    assertTrue(rs.next());
    w = rs.getWarnings();
    assertDataTruncation(w, -1, true, false, 3, 2);
    // Client driver doesn't support nested warnings
    if (usingEmbedded()) {
        w = w.getNextWarning();
        assertDataTruncation(w, -1, true, false, 4, 2);
    }
    w = w.getNextWarning();
    assertNull(w);
    rs.clearWarnings(); // workaround for DERBY-5765

    // No more rows
    assertFalse(rs.next());
    rs.close();

    // There should be no warnings on the statement or the connection
    assertNull(s.getWarnings());
    assertNull(getConnection().getWarnings());
}
 
Example 14
Source File: GfxdMessage.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/** log the warnings, if any, in execution of an SQL statement */
public static void logWarnings(Statement stmt, String sqlText, String prefix,
    LogWriter logger) throws SQLException {
  SQLWarning warning = stmt.getWarnings();
  if (warning != null) {
    if (logger.warningEnabled()) {
      logger.warning(prefix + sqlText + " "+ warning.getMessage(), null);
    }
    while ((warning = warning.getNextWarning()) != null) {
      if (logger.warningEnabled()) {
        logger.warning(prefix + sqlText + " " + warning.getMessage(), null);
      }
    }
  }
}
 
Example 15
Source File: PlatformImplBase.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Logs any warnings associated to the given connection. Note that the connection needs
 * to be open for this.
 * 
 * @param connection The open connection
 */
protected void logWarnings(Connection connection) throws SQLException
{
    SQLWarning warning = connection.getWarnings();

    while (warning != null)
    {
        getLog().warn(warning.getLocalizedMessage(), warning.getCause());
        warning = warning.getNextWarning();
    }
}
 
Example 16
Source File: Instrumenter.java    From evosql with Apache License 2.0 5 votes vote down vote up
public static void printWarnings() {
	if (st == null) return;
	
	SQLWarning w = null;
	try {
		w = st.getWarnings();
	} catch (SQLException e) {
		e.printStackTrace();
	}
	while (w != null) {
		System.out.println(w.getMessage());
		w = w.getNextWarning();
	}
}
 
Example 17
Source File: JDBCDisplayUtil.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
	@param out the place to write to
	@param warning the SQLWarning
*/
static public int /* GemStoneChange void */ ShowWarnings(PrintWriter out, SQLWarning warning) {
  int numWarnings = 0; // GemStoneAddition
	while (warning != null) {
	  numWarnings++; // GemStoneAddition
		String p1 = mapNull(warning.getSQLState(),LocalizedResource.getMessage("UT_NoSqlst_7"));
		String p2 = mapNull(warning.getMessage(),LocalizedResource.getMessage("UT_NoMessa_8"));
		out.println(LocalizedResource.getMessage("UT_Warni01", p1, p2));
		warning = warning.getNextWarning();
	}
	return numWarnings; // GemStoneAddition
}
 
Example 18
Source File: TestJdbcWarnings.java    From presto with Apache License 2.0 5 votes vote down vote up
private static void assertStartsWithExpectedWarnings(SQLWarning warning, SQLWarning expected)
{
    assertNotNull(expected);
    assertNotNull(warning);
    while (true) {
        assertWarningsEqual(warning, expected);
        warning = warning.getNextWarning();
        expected = expected.getNextWarning();
        if (expected == null) {
            return;
        }
        assertNotNull(warning);
    }
}
 
Example 19
Source File: HdfsAnalyzeTest.java    From pxf with Apache License 2.0 4 votes vote down vote up
/**
 * Negative test, ANALYZE should fail when the reject limit is breached.
 *
 * The first stage (tuple estimate) should pass successfully, and so the
 * pg_class values should be correct. The failure is supposed to happen in
 * the second stage (count of first fragment), when the new pxf table should
 * have a segment reject limit of 25 percent.
 *
 * @throws Exception if test failed to run
 */
@Test(groups = { "features" })
public void negativeAnalyzeFailOnRejectLimit() throws Exception {

    String dataPath = hdfs.getWorkingDirectory()
            + "/analyze_fail_reject_limit.txt";
    String[] fields = new String[] { "n1 int", "s1 text" };

    for (int i = 0; i < 10000; i++) {
        // inject errors
        String n1 = ((i % 3 == 0) ? "oops" : "") + i;
        dataTable.addRow(new String[] { n1, n1 });
    }

    hdfs.writeTableToFile(dataPath, dataTable, ",");

    exTable.setName("analyze_fail_reject_limit");
    exTable.setFields(fields);
    exTable.setPath(dataPath);

    exTable.setSegmentRejectLimit(5000);

    gpdb.createTableAndVerify(exTable);

    try {
        gpdb.analyze(exTable);
        Assert.fail("analyze should fail on segment reject limit 25 percent");
    } catch (SQLWarning e) {

        ExceptionUtils.validate(
                null,
                e,
                new Exception(
                        "skipping \"analyze_fail_reject_limit\" --- error returned: "
                                + "Segment reject limit reached. Aborting operation. Last error was: invalid input syntax for integer: \"oops300\", column n1"),
                true, true);
        e = e.getNextWarning();
        ExceptionUtils.validate(null, e,
                new Exception("ANALYZE completed. Success: 0, Failure: 1 ("
                        + exTable.getName() + ")"), false, true);
        e = e.getNextWarning();
        Assert.assertNull(e);
    }

    ReportUtils.report(null, getClass(),
            "Expect default pg_class entries because ANALYZE estimate failed");
    verifyPgClassValues(DEFAULT_RELPAGES, DEFAULT_RELTUPLES, 0);

    ReportUtils.report(null, getClass(),
            "Expect no pg_stats entries because ANALYZE failed");
    verifyPgStatsEntries(0);

    ReportUtils.startLevel(null, getClass(),
            "Verify query doesn't fail because reject limit on original table is ok");
    gpdb.queryResults(exTable, "SELECT COUNT(*) FROM " + exTable.getName());

    Table countTable = new Table("count", null);
    countTable.addRow(new String[] { "6666" });
    ComparisonUtils.compareTables(exTable, countTable, null);
    ReportUtils.stopLevel(null);
}
 
Example 20
Source File: MigrationDatabaseCreator.java    From product-es with Apache License 2.0 4 votes vote down vote up
/**
 * executes given sql
 *
 * @param sql
 * @throws Exception
 */
private void executeSQL(String sql) throws SQLException {

    // Check and ignore empty statements
    if ("".equals(sql.trim())) {
        return;
    }

    ResultSet resultSet = null;
    try {
        if (log.isDebugEnabled()) {
            log.debug("SQL : " + sql);
        }

        boolean ret;
        int updateCount = 0, updateCountTotal = 0;
        ret = statement.execute(sql);
        updateCount = statement.getUpdateCount();
        resultSet = statement.getResultSet();
        do {
            if (!ret && updateCount != -1) {
                updateCountTotal += updateCount;
            }
            ret = statement.getMoreResults();
            if (ret) {
                updateCount = statement.getUpdateCount();
                resultSet = statement.getResultSet();
            }
        } while (ret);

        if (log.isDebugEnabled()) {
            log.debug(sql + " : " + updateCountTotal + " rows affected");
        }
        SQLWarning warning = conn.getWarnings();
        while (warning != null) {
            if (log.isDebugEnabled()) {
                log.debug(warning + " sql warning");//TODO:Check for if
            }
            warning = warning.getNextWarning();
        }
        conn.clearWarnings();
    } finally {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                log.error("Error occurred while closing result set.", e);
            }
        }
    }
}