Search in sources :

Example 1 with Validation

use of jdk.incubator.sql2.Session.Validation in project oracle-db-examples by oracle.

the class DataSourceTest method testGetSessionError.

@Test
public void testGetSessionError() throws Exception {
    // Use an invalid URL to get an error message
    String url = "jdbc:oracle:not_thin:localhost:5521";
    String user = getUser();
    String password = getPassword();
    try (DataSource ds = dsFactory.builder().url(url).username(user).password(password).build()) {
        AtomicReference<Throwable> errConsumer = new AtomicReference<>();
        ExecutionException validEx = null;
        try (Session session = ds.getSession(errConsumer::set)) {
            session.validationOperation(Validation.COMPLETE).timeout(getTimeout()).submit().getCompletionStage().toCompletableFuture().get();
        } catch (ExecutionException exeEx) {
            // Expecting exceptional completion of the validation operation
            validEx = exeEx;
        }
        // Verify that the errConsumer was invoked, and that the validation
        // operation completed with the same error.
        Throwable attachEx = errConsumer.get();
        assertTrue(attachEx instanceof CompletionException);
        assertNotNull(validEx);
        // TODO: If attach operation fails, should the validation operation
        // complete with SqlSkipped?
        // assertTrue(validEx.getCause() instanceof SqlSkippedException);
        // assertEquals(attachEx.getCause(), validEx.getCause().getCause());
        assertEquals(attachEx.getCause(), validEx.getCause());
    }
}
Also used : CompletionException(java.util.concurrent.CompletionException) AtomicReference(java.util.concurrent.atomic.AtomicReference) ExecutionException(java.util.concurrent.ExecutionException) DataSource(jdk.incubator.sql2.DataSource) Session(jdk.incubator.sql2.Session) Test(org.junit.Test)

Example 2 with Validation

use of jdk.incubator.sql2.Session.Validation in project oracle-db-examples by oracle-samples.

the class Session method jdbcValidate.

private Void jdbcValidate(com.oracle.adbaoverjdbc.Operation<Void> op, Validation depth) {
    try {
        switch(depth) {
            case COMPLETE:
            case SERVER:
                int timeoutSeconds = (int) (op.getTimeoutMillis() / 1000L);
                // DEBUG
                group.logger.log(Level.FINE, () -> "Session.isValid(" + timeoutSeconds + ")");
                if (!jdbcConnection.isValid(timeoutSeconds)) {
                    throw new SqlException("validation failure", null, null, -1, null, -1);
                }
                break;
            case NETWORK:
            case SOCKET:
            case LOCAL:
            case NONE:
                // DEBUG
                group.logger.log(Level.FINE, () -> "Session.isClosed");
                if (jdbcConnection.isClosed()) {
                    throw new SqlException("validation failure", null, null, -1, null, -1);
                }
        }
        return null;
    } catch (SQLException ex) {
        throw new SqlException(ex.getMessage(), ex, ex.getSQLState(), ex.getErrorCode(), null, -1);
    }
}
Also used : SQLException(java.sql.SQLException) SqlException(jdk.incubator.sql2.SqlException)

Example 3 with Validation

use of jdk.incubator.sql2.Session.Validation in project oracle-db-examples by oracle-samples.

the class DataSourceTest method testGetSessionError.

@Test
public void testGetSessionError() throws Exception {
    // Use an invalid URL to get an error message
    String url = "jdbc:oracle:not_thin:localhost:5521";
    String user = getUser();
    String password = getPassword();
    try (DataSource ds = dsFactory.builder().url(url).username(user).password(password).build()) {
        AtomicReference<Throwable> errConsumer = new AtomicReference<>();
        ExecutionException validEx = null;
        try (Session session = ds.getSession(errConsumer::set)) {
            session.validationOperation(Validation.COMPLETE).timeout(getTimeout()).submit().getCompletionStage().toCompletableFuture().get();
        } catch (ExecutionException exeEx) {
            // Expecting exceptional completion of the validation operation
            validEx = exeEx;
        }
        // Verify that the errConsumer was invoked, and that the validation
        // operation completed with the same error.
        Throwable attachEx = errConsumer.get();
        assertTrue(attachEx instanceof CompletionException);
        assertNotNull(validEx);
        // TODO: If attach operation fails, should the validation operation
        // complete with SqlSkipped?
        // assertTrue(validEx.getCause() instanceof SqlSkippedException);
        // assertEquals(attachEx.getCause(), validEx.getCause().getCause());
        assertEquals(attachEx.getCause(), validEx.getCause());
    }
}
Also used : CompletionException(java.util.concurrent.CompletionException) AtomicReference(java.util.concurrent.atomic.AtomicReference) ExecutionException(java.util.concurrent.ExecutionException) DataSource(jdk.incubator.sql2.DataSource) Session(jdk.incubator.sql2.Session) Test(org.junit.Test)

Example 4 with Validation

use of jdk.incubator.sql2.Session.Validation in project oracle-db-examples by oracle.

the class Session method jdbcValidate.

private Void jdbcValidate(com.oracle.adbaoverjdbc.Operation<Void> op, Validation depth) {
    try {
        switch(depth) {
            case COMPLETE:
            case SERVER:
                int timeoutSeconds = (int) (op.getTimeoutMillis() / 1000L);
                // DEBUG
                group.logger.log(Level.FINE, () -> "Session.isValid(" + timeoutSeconds + ")");
                if (!jdbcConnection.isValid(timeoutSeconds)) {
                    throw new SqlException("validation failure", null, null, -1, null, -1);
                }
                break;
            case NETWORK:
            case SOCKET:
            case LOCAL:
            case NONE:
                // DEBUG
                group.logger.log(Level.FINE, () -> "Session.isClosed");
                if (jdbcConnection.isClosed()) {
                    throw new SqlException("validation failure", null, null, -1, null, -1);
                }
        }
        return null;
    } catch (SQLException ex) {
        throw new SqlException(ex.getMessage(), ex, ex.getSQLState(), ex.getErrorCode(), null, -1);
    }
}
Also used : SQLException(java.sql.SQLException) SqlException(jdk.incubator.sql2.SqlException)

Aggregations

SQLException (java.sql.SQLException)2 CompletionException (java.util.concurrent.CompletionException)2 ExecutionException (java.util.concurrent.ExecutionException)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 DataSource (jdk.incubator.sql2.DataSource)2 Session (jdk.incubator.sql2.Session)2 SqlException (jdk.incubator.sql2.SqlException)2 Test (org.junit.Test)2