Search in sources :

Example 1 with ScalarCallback

use of org.sqlite.ScalarCallback in project sqlite-jna by gwenn.

the class SqliteStatementTest method testQueryTimeout.

@Ignore
@Test
public void testQueryTimeout() throws Exception {
    try (Statement stmt = conn.createStatement()) {
        try {
            stmt.setQueryTimeout(-1);
            fail("negative timeout value allowed?");
        } catch (SQLException e) {
        }
        ((Conn) conn).getConn().createScalarFunction("delay", 0, FunctionFlags.SQLITE_UTF8, new ScalarCallback() {

            @Override
            public void func(SQLite3Context pCtx, SQLite3Values args) {
                try {
                    Thread.currentThread().join(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                pCtx.setResultInt(0);
            }
        });
        stmt.setQueryTimeout(1);
        assertEquals(1, stmt.getQueryTimeout());
        long startTime = System.currentTimeMillis();
        try (ResultSet rs = stmt.executeQuery("SELECT *, delay() from test_table")) {
            rs.next();
            fail("Expected a timeout exception");
        } catch (SQLTimeoutException e) {
            long endTime = System.currentTimeMillis();
            if (endTime - startTime < 1000) {
                fail("Timeout expired early -- " + (endTime - startTime));
            }
        }
        try {
            stmt.execute("INSERT INTO test_table VALUES (2, delay())");
        } catch (SQLiteException e) {
            long endTime = System.currentTimeMillis();
            if (endTime - startTime < 1000) {
                fail("Timeout expired early -- " + (endTime - startTime));
            }
        }
    }
}
Also used : SQLite3Context(org.sqlite.SQLite.SQLite3Context) ScalarCallback(org.sqlite.ScalarCallback) SQLException(java.sql.SQLException) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) SQLTimeoutException(java.sql.SQLTimeoutException) SQLiteException(org.sqlite.SQLiteException) SQLite3Values(org.sqlite.SQLite.SQLite3Values) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 SQLTimeoutException (java.sql.SQLTimeoutException)1 Statement (java.sql.Statement)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1 SQLite3Context (org.sqlite.SQLite.SQLite3Context)1 SQLite3Values (org.sqlite.SQLite.SQLite3Values)1 SQLiteException (org.sqlite.SQLiteException)1 ScalarCallback (org.sqlite.ScalarCallback)1