Search in sources :

Example 6 with JdbcSQLException

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

the class TestMultiThread method testViews.

private void testViews() throws Exception {
    // currently the combination of LOCK_MODE=0 and MULTI_THREADED
    // is not supported
    deleteDb("lockMode");
    final String url = getURL("lockMode;MULTI_THREADED=1", true);
    // create some common tables and views
    ExecutorService executor = Executors.newFixedThreadPool(8);
    Connection conn = getConnection(url);
    try {
        Statement stat = conn.createStatement();
        stat.execute("CREATE TABLE INVOICE(INVOICE_ID INT PRIMARY KEY, AMOUNT DECIMAL)");
        stat.execute("CREATE VIEW INVOICE_VIEW as SELECT * FROM INVOICE");
        stat.execute("CREATE TABLE INVOICE_DETAIL(DETAIL_ID INT PRIMARY KEY, " + "INVOICE_ID INT, DESCRIPTION VARCHAR)");
        stat.execute("CREATE VIEW INVOICE_DETAIL_VIEW as SELECT * FROM INVOICE_DETAIL");
        stat.close();
        // create views that reference the common views in different threads
        ArrayList<Future<Void>> jobs = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            final int j = i;
            jobs.add(executor.submit(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    try (Connection conn2 = getConnection(url)) {
                        Statement stat2 = conn2.createStatement();
                        stat2.execute("CREATE VIEW INVOICE_VIEW" + j + " as SELECT * FROM INVOICE_VIEW");
                        // the following query intermittently results in a
                        // NullPointerException
                        stat2.execute("CREATE VIEW INVOICE_DETAIL_VIEW" + j + " as SELECT DTL.* FROM INVOICE_VIEW" + j + " INV JOIN INVOICE_DETAIL_VIEW DTL " + "ON INV.INVOICE_ID = DTL.INVOICE_ID" + " WHERE DESCRIPTION='TEST'");
                        ResultSet rs = stat2.executeQuery("SELECT * FROM INVOICE_VIEW" + j);
                        rs.next();
                        rs.close();
                        rs = stat2.executeQuery("SELECT * FROM INVOICE_DETAIL_VIEW" + j);
                        rs.next();
                        rs.close();
                        stat2.close();
                    }
                    return null;
                }
            }));
        }
        // check for exceptions
        for (Future<Void> job : jobs) {
            try {
                job.get();
            } catch (ExecutionException ex) {
                // trying to test
                if (!(ex.getCause() instanceof JdbcSQLException) || ((JdbcSQLException) ex.getCause()).getErrorCode() != ErrorCode.LOCK_TIMEOUT_1) {
                    throw ex;
                }
            }
        }
    } finally {
        IOUtils.closeSilently(conn);
        executor.shutdown();
        executor.awaitTermination(20, TimeUnit.SECONDS);
    }
    deleteDb("lockMode");
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) JdbcSQLException(org.h2.jdbc.JdbcSQLException) Callable(java.util.concurrent.Callable) ExecutorService(java.util.concurrent.ExecutorService) ResultSet(java.sql.ResultSet) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException)

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