Search in sources :

Example 1 with JdbcSQLNonTransientException

use of org.h2.jdbc.JdbcSQLNonTransientException in project openmrs-core by openmrs.

the class H2DatabaseIT method dropAllDatabaseObjects.

protected void dropAllDatabaseObjects() throws SQLException {
    Connection connection = getConnection();
    Statement statement = null;
    try {
        statement = connection.createStatement();
        String query = "DROP ALL OBJECTS";
        statement.execute(query);
    } catch (JdbcSQLNonTransientException e) {
        log.error("connection is already closed, most likely a test method already dropped all database objects");
    } finally {
        connection.close();
    }
}
Also used : JdbcSQLNonTransientException(org.h2.jdbc.JdbcSQLNonTransientException) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(liquibase.database.jvm.JdbcConnection)

Example 2 with JdbcSQLNonTransientException

use of org.h2.jdbc.JdbcSQLNonTransientException in project ma-core-public by infiniteautomation.

the class H2DatabaseTest method test2ExploitLobStorageMap.

// This test may break database
// Needs to be executed as last test for the suite
// https://github.com/h2database/h2database/issues/1808
@Test(timeout = 10 * 1000)
public void test2ExploitLobStorageMap() throws SQLException {
    Exception expected = null;
    try {
        Connection conn1 = Common.getBean(DatabaseProxy.class).getDataSource().getConnection();
        String createTable = "CREATE TABLE t1 (id int AUTO_INCREMENT, ver bigint, data text, PRIMARY KEY (id))";
        conn1.prepareStatement(createTable).executeUpdate();
        String insert = "INSERT INTO t1 (id, ver, data) values (1, 0, ?)";
        PreparedStatement insertStmt = conn1.prepareStatement(insert);
        String largeData = org.h2.util.StringUtils.pad("", 512, "x", false);
        insertStmt.setString(1, largeData);
        insertStmt.executeUpdate();
        new Thread(() -> {
            try {
                Connection conn2 = Common.getBean(DatabaseProxy.class).getDataSource().getConnection();
                String update = "UPDATE t1 SET ver = ver + 1 WHERE id = 1";
                while (!Thread.currentThread().isInterrupted()) {
                    conn2.prepareStatement(update).executeUpdate();
                }
            } catch (JdbcSQLSyntaxErrorException ex) {
                assertEquals("42S02", ex.getSQLState());
                assertEquals("UPDATE t1 SET ver = ver + 1 WHERE id = 1", ex.getSQL());
                assertEquals("Table \"T1\" not found", ex.getOriginalMessage());
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
        }).start();
        while (true) {
            conn1.prepareStatement("SELECT * FROM t1").executeQuery();
        }
    } catch (JdbcSQLNonTransientException ex) {
        expected = ex;
        assertEquals("HY000", ex.getSQLState());
        assertEquals("SELECT * FROM t1", ex.getSQL());
        assertEquals("General error: \"java.lang.NullPointerException\"", ex.getOriginalMessage());
    }
    assertNotNull(expected);
}
Also used : JdbcSQLNonTransientException(org.h2.jdbc.JdbcSQLNonTransientException) SQLException(java.sql.SQLException) JdbcSQLSyntaxErrorException(org.h2.jdbc.JdbcSQLSyntaxErrorException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) JdbcSQLNonTransientException(org.h2.jdbc.JdbcSQLNonTransientException) JdbcSQLSyntaxErrorException(org.h2.jdbc.JdbcSQLSyntaxErrorException) SQLException(java.sql.SQLException) Test(org.junit.Test)

Aggregations

Connection (java.sql.Connection)2 JdbcSQLNonTransientException (org.h2.jdbc.JdbcSQLNonTransientException)2 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 JdbcConnection (liquibase.database.jvm.JdbcConnection)1 JdbcSQLSyntaxErrorException (org.h2.jdbc.JdbcSQLSyntaxErrorException)1 Test (org.junit.Test)1