Search in sources :

Example 1 with Lifecycle

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

the class SessionTest method testAbort.

/**
 * Verifies the following behavior:
 * <br>
 * (1) If lifecycle is Session.Lifecycle.NEW, Session.Lifecycle.OPEN,
 * Session.Lifecycle.INACTIVE or Session.Lifecycle.CLOSING ->
 * Session.Lifecycle.ABORTING.[Spec: {@link Session#abort()}]
 * <br>
 * (2) If lifecycle is Session.Lifecycle.ABORTING or Session.Lifecycle.
 * CLOSED this is a no-op. [Spec: {@link Session#abort()}]
 *
 * @throws java.lang.Exception
 */
@Test
public void testAbort() throws Exception {
    DataSource ds = DataSourceFactory.newFactory(getDataSourceFactoryName()).builder().url(getUrl()).username(getUser()).password(getPassword()).sessionProperty(AdbaSessionProperty.TRANSACTION_ISOLATION, TransactionIsolation.SERIALIZABLE).build();
    Session se1 = ds.builder().build();
    TestLifecycleListener se1LifecycleListener = new TestLifecycleListener();
    se1.registerLifecycleListener(se1LifecycleListener);
    Submission<?> se1Submission = se1.submit();
    se1.attachOperation().submit();
    // Create a record which two sessions will try to update.
    se1.rowCountOperation("INSERT INTO " + ABORT_TABLE_1 + " VALUES ('a')").submit();
    se1.commitMaybeRollback(se1.transactionCompletion());
    // Session 1 acquires a lock on the record.
    se1.rowCountOperation("UPDATE " + ABORT_TABLE_1 + " SET c='b' WHERE c='a'").submit();
    se1.<List<String>>rowOperation("SELECT * FROM " + ABORT_TABLE_1).collect(Collectors.mapping(row -> row.at(1).get(String.class), Collectors.toList())).timeout(getTimeout()).submit().getCompletionStage().toCompletableFuture().get();
    // Session 2 gets blocked acquiring a lock on the same record.
    Session se2 = ds.builder().build();
    Submission<?> se2Submission = se2.submit();
    se2.attachOperation().submit();
    Submission<Long> blockedSubmission = se2.<Long>rowCountOperation("UPDATE " + ABORT_TABLE_1 + " SET c='z' WHERE c='a'").apply(count -> count.getCount()).timeout(getTimeout()).submit();
    // Session 1 is aborted so that session 2 can proceed.
    se1.abort();
    // (2)
    se1.abort();
    // (1)
    LifecycleEvent[] expectedEvents = { new LifecycleEvent(se1, Lifecycle.NEW, Lifecycle.ATTACHED), new LifecycleEvent(se1, Lifecycle.ATTACHED, Lifecycle.ABORTING), new LifecycleEvent(se1, Lifecycle.ABORTING, Lifecycle.CLOSED) };
    for (LifecycleEvent expected : expectedEvents) {
        LifecycleEvent actual = se1LifecycleListener.record.poll(getTimeout().toMillis(), TimeUnit.MILLISECONDS);
        if (actual == null)
            fail("Timeout waiting for lifecycle event: " + expected);
        assertEquals(expected, actual);
    }
    long unblockedResult = blockedSubmission.getCompletionStage().toCompletableFuture().get();
    assertEquals(1L, unblockedResult);
    se2.rollback();
    se2.close();
    se1Submission.getCompletionStage().toCompletableFuture().handle((r, e) -> {
        assertNotNull(e);
        return null;
    }).get(getTimeout().toMillis(), TimeUnit.MILLISECONDS);
    se2Submission.getCompletionStage().toCompletableFuture().handle((r, e) -> {
        assertNull(e);
        return null;
    }).get(getTimeout().toMillis(), TimeUnit.MILLISECONDS);
}
Also used : DataSourceFactory(jdk.incubator.sql2.DataSourceFactory) BeforeClass(org.junit.BeforeClass) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) TestConfig(com.oracle.adbaoverjdbc.test.TestConfig) OperationGroup(jdk.incubator.sql2.OperationGroup) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AdbaSessionProperty(jdk.incubator.sql2.AdbaSessionProperty) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) TransactionIsolation(jdk.incubator.sql2.AdbaSessionProperty.TransactionIsolation) Assert.fail(org.junit.Assert.fail) Session(jdk.incubator.sql2.Session) AfterClass(org.junit.AfterClass) Validation(jdk.incubator.sql2.Session.Validation) Assert.assertNotNull(org.junit.Assert.assertNotNull) Lifecycle(jdk.incubator.sql2.Session.Lifecycle) Assert.assertTrue(org.junit.Assert.assertTrue) BlockingQueue(java.util.concurrent.BlockingQueue) Test(org.junit.Test) LifecycleEvent(com.oracle.adbaoverjdbc.test.SessionTest.TestLifecycleListener.LifecycleEvent) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) TransactionCompletion(jdk.incubator.sql2.TransactionCompletion) TransactionOutcome(jdk.incubator.sql2.TransactionOutcome) Operation(jdk.incubator.sql2.Operation) SessionLifecycleListener(jdk.incubator.sql2.Session.SessionLifecycleListener) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) DataSource(jdk.incubator.sql2.DataSource) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Builder(jdk.incubator.sql2.Session.Builder) Assert.assertFalse(org.junit.Assert.assertFalse) Submission(jdk.incubator.sql2.Submission) Assert.assertEquals(org.junit.Assert.assertEquals) LifecycleEvent(com.oracle.adbaoverjdbc.test.SessionTest.TestLifecycleListener.LifecycleEvent) List(java.util.List) DataSource(jdk.incubator.sql2.DataSource) Session(jdk.incubator.sql2.Session) Test(org.junit.Test)

Example 2 with Lifecycle

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

the class SessionTest method testSessionBuilder.

/**
 * Verifies the following behavior:
 * <br>
 * (1) Session.Builder.property(SessionProperty, Object) will specify a
 * property and its value for the built Session.
 * [Spec: {@link Session.Builder#property(SessionProperty, Object)}]
 * Session.getProperties() will return the set of properties configured on
 * this Session excepting any sensitive properties.
 * [Spec: {@link Session#getProperties()}]
 * <br>
 * (2) A Session is initially in the Session.Lifecycle.NEW lifecycle state.
 * [Spec: {@link Session.Builder}]
 * <br>
 * (3) If an attach operation completes successfully and the lifecycle is
 * Session.Lifecycle.NEW -> Session.Lifecycle.OPEN.
 * [Spec: {@link Session#attachOperation()}]
 * <br>
 * (4) Session.getSessionLifecycle() returns the current lifecycle of this
 * Session. [Spec: {@link Session#getSessionLifecycle()}]
 * <br>
 * (5) Session.registerLifcycleListener(SessionLifecycleListener) registers
 * a listener that will be called whenever there is a change in the
 * lifecycle of this Session. If the listener is already registered this
 * is a no-op. [Spec:
 * {@link Session#registerLifecycleListener(SessionLifecycleListener)}]
 * (6) Session.closeOperation() returns an operation which, upon execution,
 * transitions the session's lifecyle from OPEN to CLOSING. When the
 * operation completes, and no other operations are enqueued, the lifecycle
 * transistions from CLOSING to CLOSED. If the operation is submitted when
 * the lifecycle is closed, the execution of the operation is a no-op.
 * [Spec: {@link Session#closeOperation()}]
 */
@Test
public void testSessionBuilder() throws Exception {
    TestLifecycleListener testListener = new TestLifecycleListener();
    final String url = getUrl();
    final String user = getUser();
    final String password = getPassword();
    Session se = DataSourceFactory.newFactory(getDataSourceFactoryName()).builder().build().builder().property(AdbaSessionProperty.URL, url).property(AdbaSessionProperty.USER, user).property(AdbaSessionProperty.PASSWORD, password).build();
    // (1)
    assertEquals(url, se.getProperties().get(AdbaSessionProperty.URL));
    assertEquals(user, se.getProperties().get(AdbaSessionProperty.USER));
    assertNull(se.getProperties().get(AdbaSessionProperty.PASSWORD));
    // (2, 4)
    assertEquals(Lifecycle.NEW, se.getSessionLifecycle());
    // (5)
    // The duplicate call verifies the no-op behavior.
    se.registerLifecycleListener(testListener);
    se.registerLifecycleListener(testListener);
    // (3, 4)
    se.submit();
    se.attachOperation().timeout(getTimeout()).submit().getCompletionStage().toCompletableFuture().get();
    assertEquals(Lifecycle.ATTACHED, se.getSessionLifecycle());
    // (4, 6)
    se.closeOperation().timeout(getTimeout()).submit().getCompletionStage().toCompletableFuture().get();
    assertEquals(Lifecycle.CLOSED, se.getSessionLifecycle());
    // (6)
    // The duplicate close operation verifies the no-op behavior.
    se.closeOperation().timeout(getTimeout()).submit().getCompletionStage().toCompletableFuture().get();
    // (2, 3, 5, 6)
    LifecycleEvent[] expected = { new LifecycleEvent(se, Lifecycle.NEW, Lifecycle.ATTACHED), new LifecycleEvent(se, Lifecycle.ATTACHED, Lifecycle.CLOSING), new LifecycleEvent(se, Lifecycle.CLOSING, Lifecycle.CLOSED) };
    LifecycleEvent[] actual = testListener.record.toArray(new LifecycleEvent[0]);
    assertArrayEquals(expected, actual);
}
Also used : LifecycleEvent(com.oracle.adbaoverjdbc.test.SessionTest.TestLifecycleListener.LifecycleEvent) Session(jdk.incubator.sql2.Session) Test(org.junit.Test)

Example 3 with Lifecycle

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

the class SessionTest method testTryWithResources.

/**
 * Verify that declaring a Session in try-with-resources closes the session
 * when the try block exits.
 * @throws Exception
 */
@Test
public void testTryWithResources() throws Exception {
    Builder sessionBuilder = DataSourceFactory.newFactory(getDataSourceFactoryName()).builder().url(getUrl()).username(getUser()).password(getPassword()).build().builder();
    TestLifecycleListener listener = new TestLifecycleListener();
    LifecycleEvent[] expectedEvents;
    try (Session se = sessionBuilder.build()) {
        se.registerLifecycleListener(listener);
        se.submit();
        se.attachOperation().timeout(getTimeout()).submit();
        expectedEvents = new LifecycleEvent[] { new LifecycleEvent(se, Lifecycle.NEW, Lifecycle.ATTACHED), new LifecycleEvent(se, Lifecycle.ATTACHED, Lifecycle.CLOSING), new LifecycleEvent(se, Lifecycle.CLOSING, Lifecycle.CLOSED) };
    }
    for (LifecycleEvent expected : expectedEvents) {
        LifecycleEvent actual = listener.record.poll(getTimeout().toMillis(), TimeUnit.MILLISECONDS);
        if (actual == null)
            fail("Timeout waiting for lifecycle event: " + expected);
        assertEquals(expected, actual);
    }
}
Also used : Builder(jdk.incubator.sql2.Session.Builder) LifecycleEvent(com.oracle.adbaoverjdbc.test.SessionTest.TestLifecycleListener.LifecycleEvent) Session(jdk.incubator.sql2.Session) Test(org.junit.Test)

Example 4 with Lifecycle

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

the class SessionTest method testSessionBuilder.

/**
 * Verifies the following behavior:
 * <br>
 * (1) Session.Builder.property(SessionProperty, Object) will specify a
 * property and its value for the built Session.
 * [Spec: {@link Session.Builder#property(SessionProperty, Object)}]
 * Session.getProperties() will return the set of properties configured on
 * this Session excepting any sensitive properties.
 * [Spec: {@link Session#getProperties()}]
 * <br>
 * (2) A Session is initially in the Session.Lifecycle.NEW lifecycle state.
 * [Spec: {@link Session.Builder}]
 * <br>
 * (3) If an attach operation completes successfully and the lifecycle is
 * Session.Lifecycle.NEW -> Session.Lifecycle.OPEN.
 * [Spec: {@link Session#attachOperation()}]
 * <br>
 * (4) Session.getSessionLifecycle() returns the current lifecycle of this
 * Session. [Spec: {@link Session#getSessionLifecycle()}]
 * <br>
 * (5) Session.registerLifcycleListener(SessionLifecycleListener) registers
 * a listener that will be called whenever there is a change in the
 * lifecycle of this Session. If the listener is already registered this
 * is a no-op. [Spec:
 * {@link Session#registerLifecycleListener(SessionLifecycleListener)}]
 * (6) Session.closeOperation() returns an operation which, upon execution,
 * transitions the session's lifecyle from OPEN to CLOSING. When the
 * operation completes, and no other operations are enqueued, the lifecycle
 * transistions from CLOSING to CLOSED. If the operation is submitted when
 * the lifecycle is closed, the execution of the operation is a no-op.
 * [Spec: {@link Session#closeOperation()}]
 */
@Test
public void testSessionBuilder() throws Exception {
    TestLifecycleListener testListener = new TestLifecycleListener();
    final String url = getUrl();
    final String user = getUser();
    final String password = getPassword();
    Session se = DataSourceFactory.newFactory(getDataSourceFactoryName()).builder().build().builder().property(AdbaSessionProperty.URL, url).property(AdbaSessionProperty.USER, user).property(AdbaSessionProperty.PASSWORD, password).build();
    // (1)
    assertEquals(url, se.getProperties().get(AdbaSessionProperty.URL));
    assertEquals(user, se.getProperties().get(AdbaSessionProperty.USER));
    assertNull(se.getProperties().get(AdbaSessionProperty.PASSWORD));
    // (2, 4)
    assertEquals(Lifecycle.NEW, se.getSessionLifecycle());
    // (5)
    // The duplicate call verifies the no-op behavior.
    se.registerLifecycleListener(testListener);
    se.registerLifecycleListener(testListener);
    // (3, 4)
    se.submit();
    se.attachOperation().timeout(getTimeout()).submit().getCompletionStage().toCompletableFuture().get();
    assertEquals(Lifecycle.ATTACHED, se.getSessionLifecycle());
    // (4, 6)
    se.closeOperation().timeout(getTimeout()).submit().getCompletionStage().toCompletableFuture().get();
    assertEquals(Lifecycle.CLOSED, se.getSessionLifecycle());
    // (6)
    // The duplicate close operation verifies the no-op behavior.
    se.closeOperation().timeout(getTimeout()).submit().getCompletionStage().toCompletableFuture().get();
    // (2, 3, 5, 6)
    LifecycleEvent[] expected = { new LifecycleEvent(se, Lifecycle.NEW, Lifecycle.ATTACHED), new LifecycleEvent(se, Lifecycle.ATTACHED, Lifecycle.CLOSING), new LifecycleEvent(se, Lifecycle.CLOSING, Lifecycle.CLOSED) };
    LifecycleEvent[] actual = testListener.record.toArray(new LifecycleEvent[0]);
    assertArrayEquals(expected, actual);
}
Also used : LifecycleEvent(com.oracle.adbaoverjdbc.test.SessionTest.TestLifecycleListener.LifecycleEvent) Session(jdk.incubator.sql2.Session) Test(org.junit.Test)

Example 5 with Lifecycle

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

the class SessionTest method testAbort.

/**
 * Verifies the following behavior:
 * <br>
 * (1) If lifecycle is Session.Lifecycle.NEW, Session.Lifecycle.OPEN,
 * Session.Lifecycle.INACTIVE or Session.Lifecycle.CLOSING ->
 * Session.Lifecycle.ABORTING.[Spec: {@link Session#abort()}]
 * <br>
 * (2) If lifecycle is Session.Lifecycle.ABORTING or Session.Lifecycle.
 * CLOSED this is a no-op. [Spec: {@link Session#abort()}]
 *
 * @throws java.lang.Exception
 */
@Test
public void testAbort() throws Exception {
    DataSource ds = DataSourceFactory.newFactory(getDataSourceFactoryName()).builder().url(getUrl()).username(getUser()).password(getPassword()).sessionProperty(AdbaSessionProperty.TRANSACTION_ISOLATION, TransactionIsolation.SERIALIZABLE).build();
    Session se1 = ds.builder().build();
    TestLifecycleListener se1LifecycleListener = new TestLifecycleListener();
    se1.registerLifecycleListener(se1LifecycleListener);
    Submission<?> se1Submission = se1.submit();
    se1.attachOperation().submit();
    // Create a record which two sessions will try to update.
    se1.rowCountOperation("INSERT INTO " + ABORT_TABLE_1 + " VALUES ('a')").submit();
    se1.commitMaybeRollback(se1.transactionCompletion());
    // Session 1 acquires a lock on the record.
    se1.rowCountOperation("UPDATE " + ABORT_TABLE_1 + " SET c='b' WHERE c='a'").submit();
    se1.<List<String>>rowOperation("SELECT * FROM " + ABORT_TABLE_1).collect(Collectors.mapping(row -> row.at(1).get(String.class), Collectors.toList())).timeout(getTimeout()).submit().getCompletionStage().toCompletableFuture().get();
    // Session 2 gets blocked acquiring a lock on the same record.
    Session se2 = ds.builder().build();
    Submission<?> se2Submission = se2.submit();
    se2.attachOperation().submit();
    Submission<Long> blockedSubmission = se2.<Long>rowCountOperation("UPDATE " + ABORT_TABLE_1 + " SET c='z' WHERE c='a'").apply(count -> count.getCount()).timeout(getTimeout()).submit();
    // Session 1 is aborted so that session 2 can proceed.
    se1.abort();
    // (2)
    se1.abort();
    // (1)
    LifecycleEvent[] expectedEvents = { new LifecycleEvent(se1, Lifecycle.NEW, Lifecycle.ATTACHED), new LifecycleEvent(se1, Lifecycle.ATTACHED, Lifecycle.ABORTING), new LifecycleEvent(se1, Lifecycle.ABORTING, Lifecycle.CLOSED) };
    for (LifecycleEvent expected : expectedEvents) {
        LifecycleEvent actual = se1LifecycleListener.record.poll(getTimeout().toMillis(), TimeUnit.MILLISECONDS);
        if (actual == null)
            fail("Timeout waiting for lifecycle event: " + expected);
        assertEquals(expected, actual);
    }
    long unblockedResult = blockedSubmission.getCompletionStage().toCompletableFuture().get();
    assertEquals(1L, unblockedResult);
    se2.rollback();
    se2.close();
    se1Submission.getCompletionStage().toCompletableFuture().handle((r, e) -> {
        assertNotNull(e);
        return null;
    }).get(getTimeout().toMillis(), TimeUnit.MILLISECONDS);
    se2Submission.getCompletionStage().toCompletableFuture().handle((r, e) -> {
        assertNull(e);
        return null;
    }).get(getTimeout().toMillis(), TimeUnit.MILLISECONDS);
}
Also used : DataSourceFactory(jdk.incubator.sql2.DataSourceFactory) BeforeClass(org.junit.BeforeClass) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) TestConfig(com.oracle.adbaoverjdbc.test.TestConfig) OperationGroup(jdk.incubator.sql2.OperationGroup) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AdbaSessionProperty(jdk.incubator.sql2.AdbaSessionProperty) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) TransactionIsolation(jdk.incubator.sql2.AdbaSessionProperty.TransactionIsolation) Assert.fail(org.junit.Assert.fail) Session(jdk.incubator.sql2.Session) AfterClass(org.junit.AfterClass) Validation(jdk.incubator.sql2.Session.Validation) Assert.assertNotNull(org.junit.Assert.assertNotNull) Lifecycle(jdk.incubator.sql2.Session.Lifecycle) Assert.assertTrue(org.junit.Assert.assertTrue) BlockingQueue(java.util.concurrent.BlockingQueue) Test(org.junit.Test) LifecycleEvent(com.oracle.adbaoverjdbc.test.SessionTest.TestLifecycleListener.LifecycleEvent) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) TransactionCompletion(jdk.incubator.sql2.TransactionCompletion) TransactionOutcome(jdk.incubator.sql2.TransactionOutcome) Operation(jdk.incubator.sql2.Operation) SessionLifecycleListener(jdk.incubator.sql2.Session.SessionLifecycleListener) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) DataSource(jdk.incubator.sql2.DataSource) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Builder(jdk.incubator.sql2.Session.Builder) Assert.assertFalse(org.junit.Assert.assertFalse) Submission(jdk.incubator.sql2.Submission) Assert.assertEquals(org.junit.Assert.assertEquals) LifecycleEvent(com.oracle.adbaoverjdbc.test.SessionTest.TestLifecycleListener.LifecycleEvent) List(java.util.List) DataSource(jdk.incubator.sql2.DataSource) Session(jdk.incubator.sql2.Session) Test(org.junit.Test)

Aggregations

LifecycleEvent (com.oracle.adbaoverjdbc.test.SessionTest.TestLifecycleListener.LifecycleEvent)6 Session (jdk.incubator.sql2.Session)6 Test (org.junit.Test)6 Builder (jdk.incubator.sql2.Session.Builder)4 TestConfig (com.oracle.adbaoverjdbc.test.TestConfig)2 List (java.util.List)2 BlockingQueue (java.util.concurrent.BlockingQueue)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExecutionException (java.util.concurrent.ExecutionException)2 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)2 TimeUnit (java.util.concurrent.TimeUnit)2 TimeoutException (java.util.concurrent.TimeoutException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Function (java.util.function.Function)2 Collectors (java.util.stream.Collectors)2 AdbaSessionProperty (jdk.incubator.sql2.AdbaSessionProperty)2 TransactionIsolation (jdk.incubator.sql2.AdbaSessionProperty.TransactionIsolation)2 DataSource (jdk.incubator.sql2.DataSource)2