Search in sources :

Example 1 with JdbcSQLException

use of org.h2.jdbc.JdbcSQLException in project h2database by h2database.

the class SessionRemote method done.

/**
 * Called to flush the output after data has been sent to the server and
 * just before receiving data. This method also reads the status code from
 * the server and throws any exception the server sent.
 *
 * @param transfer the transfer object
 * @throws DbException if the server sent an exception
 * @throws IOException if there is a communication problem between client
 *             and server
 */
public void done(Transfer transfer) throws IOException {
    transfer.flush();
    int status = transfer.readInt();
    if (status == STATUS_ERROR) {
        String sqlstate = transfer.readString();
        String message = transfer.readString();
        String sql = transfer.readString();
        int errorCode = transfer.readInt();
        String stackTrace = transfer.readString();
        JdbcSQLException s = new JdbcSQLException(message, sql, sqlstate, errorCode, null, stackTrace);
        if (errorCode == ErrorCode.CONNECTION_BROKEN_1) {
            // allow re-connect
            throw new IOException(s.toString(), s);
        }
        throw DbException.convert(s);
    } else if (status == STATUS_CLOSED) {
        transferList = null;
    } else if (status == STATUS_OK_STATE_CHANGED) {
        sessionStateChanged = true;
    } else if (status == STATUS_OK) {
    // ok
    } else {
        throw DbException.get(ErrorCode.CONNECTION_BROKEN_1, "unexpected status " + status);
    }
}
Also used : JdbcSQLException(org.h2.jdbc.JdbcSQLException) IOException(java.io.IOException)

Example 2 with JdbcSQLException

use of org.h2.jdbc.JdbcSQLException in project h2database by h2database.

the class TraceSystem method writeFile.

private synchronized void writeFile(String s, Throwable t) {
    try {
        if (checkSize++ >= CHECK_SIZE_EACH_WRITES) {
            checkSize = 0;
            closeWriter();
            if (maxFileSize > 0 && FileUtils.size(fileName) > maxFileSize) {
                String old = fileName + ".old";
                FileUtils.delete(old);
                FileUtils.move(fileName, old);
            }
        }
        if (!openWriter()) {
            return;
        }
        printWriter.println(s);
        if (t != null) {
            if (levelFile == ERROR && t instanceof JdbcSQLException) {
                JdbcSQLException se = (JdbcSQLException) t;
                int code = se.getErrorCode();
                if (ErrorCode.isCommon(code)) {
                    printWriter.println(t.toString());
                } else {
                    t.printStackTrace(printWriter);
                }
            } else {
                t.printStackTrace(printWriter);
            }
        }
        printWriter.flush();
        if (closed) {
            closeWriter();
        }
    } catch (Exception e) {
        logWritingError(e);
    }
}
Also used : JdbcSQLException(org.h2.jdbc.JdbcSQLException) JdbcSQLException(org.h2.jdbc.JdbcSQLException) IOException(java.io.IOException)

Example 3 with JdbcSQLException

use of org.h2.jdbc.JdbcSQLException in project h2database by h2database.

the class TestGeneralCommonTableQueries method testNumberedParameterizedQuery.

private void testNumberedParameterizedQuery() throws Exception {
    deleteDb("commonTableExpressionQueries");
    Connection conn = getConnection("commonTableExpressionQueries");
    PreparedStatement prep;
    ResultSet rs;
    conn.setAutoCommit(false);
    prep = conn.prepareStatement("WITH t1 AS (" + "     SELECT R.X, 'T1' FROM SYSTEM_RANGE(?1,?2) R" + ")," + "t2 AS (" + "     SELECT R.X, 'T2' FROM SYSTEM_RANGE(?3,?4) R" + ") " + "SELECT * FROM t1 UNION ALL SELECT * FROM t2 UNION ALL SELECT X, 'Q' FROM SYSTEM_RANGE(?5,?6)");
    prep.setInt(1, 1);
    prep.setInt(2, 2);
    prep.setInt(3, 3);
    prep.setInt(4, 4);
    prep.setInt(5, 5);
    prep.setInt(6, 6);
    rs = prep.executeQuery();
    for (int n : new int[] { 1, 2, 3, 4, 5, 6 }) {
        assertTrue(rs.next());
        assertEquals(n, rs.getInt(1));
    }
    assertEquals("X", rs.getMetaData().getColumnLabel(1));
    assertEquals("'T1'", rs.getMetaData().getColumnLabel(2));
    assertFalse(rs.next());
    try {
        prep = conn.prepareStatement("SELECT * FROM t1 UNION ALL SELECT * FROM t2 " + "UNION ALL SELECT X, 'Q' FROM SYSTEM_RANGE(5,6)");
        rs = prep.executeQuery();
        fail("Temp view T1 was accessible after previous WITH statement finished " + "- but should not have been.");
    } catch (JdbcSQLException e) {
        // ensure the T1 table has been removed even without auto commit
        assertContains(e.getMessage(), "Table \"T1\" not found;");
    }
    conn.close();
    deleteDb("commonTableExpressionQueries");
}
Also used : Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) JdbcSQLException(org.h2.jdbc.JdbcSQLException)

Example 4 with JdbcSQLException

use of org.h2.jdbc.JdbcSQLException in project h2database by h2database.

the class TcpServerThread method sendError.

private void sendError(Throwable t) {
    try {
        SQLException e = DbException.convert(t).getSQLException();
        StringWriter writer = new StringWriter();
        e.printStackTrace(new PrintWriter(writer));
        String trace = writer.toString();
        String message;
        String sql;
        if (e instanceof JdbcSQLException) {
            JdbcSQLException j = (JdbcSQLException) e;
            message = j.getOriginalMessage();
            sql = j.getSQL();
        } else {
            message = e.getMessage();
            sql = null;
        }
        transfer.writeInt(SessionRemote.STATUS_ERROR).writeString(e.getSQLState()).writeString(message).writeString(sql).writeInt(e.getErrorCode()).writeString(trace).flush();
    } catch (Exception e2) {
        if (!transfer.isClosed()) {
            server.traceError(e2);
        }
        // if writing the error does not work, close the connection
        stop = true;
    }
}
Also used : StringWriter(java.io.StringWriter) SQLException(java.sql.SQLException) JdbcSQLException(org.h2.jdbc.JdbcSQLException) JdbcSQLException(org.h2.jdbc.JdbcSQLException) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException) IOException(java.io.IOException) JdbcSQLException(org.h2.jdbc.JdbcSQLException) PrintWriter(java.io.PrintWriter)

Example 5 with JdbcSQLException

use of org.h2.jdbc.JdbcSQLException in project h2database by h2database.

the class DbException method addSQL.

/**
 * Set the SQL statement of the given exception.
 * This method may create a new object.
 *
 * @param sql the SQL statement
 * @return the exception
 */
public DbException addSQL(String sql) {
    SQLException e = getSQLException();
    if (e instanceof JdbcSQLException) {
        JdbcSQLException j = (JdbcSQLException) e;
        if (j.getSQL() == null) {
            j.setSQL(sql);
        }
        return this;
    }
    e = new JdbcSQLException(e.getMessage(), sql, e.getSQLState(), e.getErrorCode(), e, null);
    return new DbException(e);
}
Also used : SQLException(java.sql.SQLException) JdbcSQLException(org.h2.jdbc.JdbcSQLException) JdbcSQLException(org.h2.jdbc.JdbcSQLException)

Aggregations

JdbcSQLException (org.h2.jdbc.JdbcSQLException)6 IOException (java.io.IOException)3 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1 Callable (java.util.concurrent.Callable)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 DbException (org.h2.message.DbException)1