Search in sources :

Example 1 with OperationGroup

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

the class SessionTest method testOperationGroup.

@Test
public void testOperationGroup() throws Exception {
    try (DataSource ds = getDataSource();
        Session se = ds.getSession()) {
        AtomicInteger opCounter = new AtomicInteger();
        se.rowCountOperation("INSERT INTO " + OP_GROUP_TABLE + " VALUES (0)").apply(rc -> opCounter.incrementAndGet()).submit();
        Submission<List<Boolean>> opgSubmission = null;
        try (OperationGroup<Boolean, List<Boolean>> opg = se.operationGroup()) {
            opgSubmission = opg.collect(Collectors.toList()).timeout(getTimeout()).submit();
            opg.rowOperation("SELECT c FROM " + OP_GROUP_TABLE).collect(Collectors.reducing(Boolean.TRUE, row -> opCounter.compareAndSet(1, 2), (b1, b2) -> b1 && b2)).submit();
            opg.localOperation().onExecution(() -> opCounter.compareAndSet(2, 3)).submit();
            opg.rowCountOperation("UPDATE " + OP_GROUP_TABLE + " SET c = 1 WHERE c = 0").apply(rc -> opCounter.compareAndSet(3, 4)).submit();
        }
        assertNotNull(opgSubmission);
        List<Boolean> opgResult = opgSubmission.getCompletionStage().toCompletableFuture().get();
        assertNotNull(opgResult);
        assertEquals(3, opgResult.size());
        for (Boolean memberResult : opgResult) assertTrue(memberResult);
        se.rollback().toCompletableFuture().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) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) List(java.util.List) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DataSource(jdk.incubator.sql2.DataSource) Session(jdk.incubator.sql2.Session) Test(org.junit.Test)

Example 2 with OperationGroup

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

the class SessionTest method testTransaction.

/**
 * Verifies the following behavior:
 * (1) Session.rowCountOperation returns a new
 * ParameterizedRowCountOperation.
 * [Spec: {@link Session#rowCountOperation(String)}]
 * <br>
 * (2) Session.transactionCompletion() returns a new TransactionCompletion
 * [Spec: {@link Session#transactionCompletion()}
 * <br>
 * (3) Session.commitMaybeRollback(TransactionCompletion) returns a
 * CompletionStage that is completed with the outcome of the transaction.
 * [Spec:
 * {@link Session#commitMaybeRollback(jdk.incubator.sql2.TransactionCompletion)}]
 * <br>
 * (4) Session.rowOperation(String) returns a new
 * ParameterizedRowCountOperation
 * [Spec: {@link Session#rowOperation(String)}]
 * <br>
 * (5) Session.commitMaybeRollback(TransactionCompletion) submits an
 * operation which commits or rolls back the current transaction.
 * [Spec:
 * {@link Session#commitMaybeRollback(jdk.incubator.sql2.TransactionCompletion)}]
 * <br>
 * (6) Session.endTransactionOperation(TransactionCompletion) returns a new
 * Operation that ends the database transaction. This Operation is a member of
 * the OperationGroup. The transaction is ended with a commit unless the
 * TransactionCompletion has been TransactionCompletion.setRollbackOnly in
 * which case the transaction is ended with a rollback.
 * [Spec: {@link Session#endTransactionOperation(TransactionCompletion)}]
 *
 * @throws Exception
 */
@Test
public void testTransaction() throws Exception {
    DataSource ds = DataSourceFactory.newFactory(getDataSourceFactoryName()).builder().url(getUrl()).username(getUser()).password(getPassword()).sessionProperty(AdbaSessionProperty.TRANSACTION_ISOLATION, TransactionIsolation.READ_COMMITTED).build();
    try (Session se = ds.getSession()) {
        // (1)
        long insertCount = se.<Long>rowCountOperation("INSERT INTO " + TRANSACTION_TABLE + " VALUES ('a')").apply((rowCount) -> rowCount.getCount()).timeout(getTimeout()).submit().getCompletionStage().toCompletableFuture().get();
        assertNotNull(insertCount);
        assertEquals(1L, insertCount);
        // (3, 4)
        TransactionOutcome commitOutcome = se.commitMaybeRollback(se.transactionCompletion()).toCompletableFuture().get(getTimeout().toMillis(), TimeUnit.MILLISECONDS);
        assertEquals(TransactionOutcome.COMMIT, commitOutcome);
        // Committed changes will be visible to other sessions
        try (Session se2 = ds.getSession()) {
            // (5, 6)
            List<String> selectedRows = se2.<List<String>>rowOperation("SELECT * FROM " + TRANSACTION_TABLE).collect(Collectors.mapping((row) -> row.at(1).get(String.class), Collectors.toList())).timeout(getTimeout()).submit().getCompletionStage().toCompletableFuture().get();
            assertNotNull(selectedRows);
            assertEquals(1, selectedRows.size());
            assertEquals("a", selectedRows.get(0));
            // (1)
            se2.rowCountOperation("INSERT INTO " + TRANSACTION_TABLE + " VALUES ('b')").timeout(getTimeout()).submit();
            // (4)
            List<String> selectedRows2 = se2.<List<String>>rowOperation("SELECT * FROM " + TRANSACTION_TABLE).collect(Collectors.mapping((row) -> row.at(1).get(String.class), Collectors.toList())).timeout(getTimeout()).submit().getCompletionStage().toCompletableFuture().get();
            assertNotNull(selectedRows2);
            assertEquals(2, selectedRows2.size());
            assertTrue(selectedRows2.contains("a"));
            assertTrue(selectedRows2.contains("b"));
            // (3, 7)
            TransactionCompletion rollbackInsert = se2.transactionCompletion();
            rollbackInsert.setRollbackOnly();
            TransactionOutcome rollbackOutcome = se2.endTransactionOperation(rollbackInsert).timeout(getTimeout()).submit().getCompletionStage().toCompletableFuture().get();
            assertEquals(TransactionOutcome.ROLLBACK, rollbackOutcome);
        }
        List<String> selectedRows = se.<List<String>>rowOperation("SELECT * FROM " + TRANSACTION_TABLE).collect(Collectors.mapping((row) -> row.at(1).get(String.class), Collectors.toList())).timeout(getTimeout()).submit().getCompletionStage().toCompletableFuture().get();
        assertNotNull(selectedRows);
        assertEquals(1, selectedRows.size());
        assertEquals("a", selectedRows.get(0));
    }
}
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) TransactionOutcome(jdk.incubator.sql2.TransactionOutcome) List(java.util.List) TransactionCompletion(jdk.incubator.sql2.TransactionCompletion) DataSource(jdk.incubator.sql2.DataSource) Session(jdk.incubator.sql2.Session) Test(org.junit.Test)

Example 3 with OperationGroup

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

the class SessionTest method testOperationGroup.

@Test
public void testOperationGroup() throws Exception {
    try (DataSource ds = getDataSource();
        Session se = ds.getSession()) {
        AtomicInteger opCounter = new AtomicInteger();
        se.rowCountOperation("INSERT INTO " + OP_GROUP_TABLE + " VALUES (0)").apply(rc -> opCounter.incrementAndGet()).submit();
        Submission<List<Boolean>> opgSubmission = null;
        try (OperationGroup<Boolean, List<Boolean>> opg = se.operationGroup()) {
            opgSubmission = opg.collect(Collectors.toList()).timeout(getTimeout()).submit();
            opg.rowOperation("SELECT c FROM " + OP_GROUP_TABLE).collect(Collectors.reducing(Boolean.TRUE, row -> opCounter.compareAndSet(1, 2), (b1, b2) -> b1 && b2)).submit();
            opg.localOperation().onExecution(() -> opCounter.compareAndSet(2, 3)).submit();
            opg.rowCountOperation("UPDATE " + OP_GROUP_TABLE + " SET c = 1 WHERE c = 0").apply(rc -> opCounter.compareAndSet(3, 4)).submit();
        }
        assertNotNull(opgSubmission);
        List<Boolean> opgResult = opgSubmission.getCompletionStage().toCompletableFuture().get();
        assertNotNull(opgResult);
        assertEquals(3, opgResult.size());
        for (Boolean memberResult : opgResult) assertTrue(memberResult);
        se.rollback().toCompletableFuture().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) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) List(java.util.List) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DataSource(jdk.incubator.sql2.DataSource) Session(jdk.incubator.sql2.Session) Test(org.junit.Test)

Example 4 with OperationGroup

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

the class SessionTest method testTransaction.

/**
 * Verifies the following behavior:
 * (1) Session.rowCountOperation returns a new
 * ParameterizedRowCountOperation.
 * [Spec: {@link Session#rowCountOperation(String)}]
 * <br>
 * (2) Session.transactionCompletion() returns a new TransactionCompletion
 * [Spec: {@link Session#transactionCompletion()}
 * <br>
 * (3) Session.commitMaybeRollback(TransactionCompletion) returns a
 * CompletionStage that is completed with the outcome of the transaction.
 * [Spec:
 * {@link Session#commitMaybeRollback(jdk.incubator.sql2.TransactionCompletion)}]
 * <br>
 * (4) Session.rowOperation(String) returns a new
 * ParameterizedRowCountOperation
 * [Spec: {@link Session#rowOperation(String)}]
 * <br>
 * (5) Session.commitMaybeRollback(TransactionCompletion) submits an
 * operation which commits or rolls back the current transaction.
 * [Spec:
 * {@link Session#commitMaybeRollback(jdk.incubator.sql2.TransactionCompletion)}]
 * <br>
 * (6) Session.endTransactionOperation(TransactionCompletion) returns a new
 * Operation that ends the database transaction. This Operation is a member of
 * the OperationGroup. The transaction is ended with a commit unless the
 * TransactionCompletion has been TransactionCompletion.setRollbackOnly in
 * which case the transaction is ended with a rollback.
 * [Spec: {@link Session#endTransactionOperation(TransactionCompletion)}]
 *
 * @throws Exception
 */
@Test
public void testTransaction() throws Exception {
    DataSource ds = DataSourceFactory.newFactory(getDataSourceFactoryName()).builder().url(getUrl()).username(getUser()).password(getPassword()).sessionProperty(AdbaSessionProperty.TRANSACTION_ISOLATION, TransactionIsolation.READ_COMMITTED).build();
    try (Session se = ds.getSession()) {
        // (1)
        long insertCount = se.<Long>rowCountOperation("INSERT INTO " + TRANSACTION_TABLE + " VALUES ('a')").apply((rowCount) -> rowCount.getCount()).timeout(getTimeout()).submit().getCompletionStage().toCompletableFuture().get();
        assertNotNull(insertCount);
        assertEquals(1L, insertCount);
        // (3, 4)
        TransactionOutcome commitOutcome = se.commitMaybeRollback(se.transactionCompletion()).toCompletableFuture().get(getTimeout().toMillis(), TimeUnit.MILLISECONDS);
        assertEquals(TransactionOutcome.COMMIT, commitOutcome);
        // Committed changes will be visible to other sessions
        try (Session se2 = ds.getSession()) {
            // (5, 6)
            List<String> selectedRows = se2.<List<String>>rowOperation("SELECT * FROM " + TRANSACTION_TABLE).collect(Collectors.mapping((row) -> row.at(1).get(String.class), Collectors.toList())).timeout(getTimeout()).submit().getCompletionStage().toCompletableFuture().get();
            assertNotNull(selectedRows);
            assertEquals(1, selectedRows.size());
            assertEquals("a", selectedRows.get(0));
            // (1)
            se2.rowCountOperation("INSERT INTO " + TRANSACTION_TABLE + " VALUES ('b')").timeout(getTimeout()).submit();
            // (4)
            List<String> selectedRows2 = se2.<List<String>>rowOperation("SELECT * FROM " + TRANSACTION_TABLE).collect(Collectors.mapping((row) -> row.at(1).get(String.class), Collectors.toList())).timeout(getTimeout()).submit().getCompletionStage().toCompletableFuture().get();
            assertNotNull(selectedRows2);
            assertEquals(2, selectedRows2.size());
            assertTrue(selectedRows2.contains("a"));
            assertTrue(selectedRows2.contains("b"));
            // (3, 7)
            TransactionCompletion rollbackInsert = se2.transactionCompletion();
            rollbackInsert.setRollbackOnly();
            TransactionOutcome rollbackOutcome = se2.endTransactionOperation(rollbackInsert).timeout(getTimeout()).submit().getCompletionStage().toCompletableFuture().get();
            assertEquals(TransactionOutcome.ROLLBACK, rollbackOutcome);
        }
        List<String> selectedRows = se.<List<String>>rowOperation("SELECT * FROM " + TRANSACTION_TABLE).collect(Collectors.mapping((row) -> row.at(1).get(String.class), Collectors.toList())).timeout(getTimeout()).submit().getCompletionStage().toCompletableFuture().get();
        assertNotNull(selectedRows);
        assertEquals(1, selectedRows.size());
        assertEquals("a", selectedRows.get(0));
    }
}
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) TransactionOutcome(jdk.incubator.sql2.TransactionOutcome) List(java.util.List) TransactionCompletion(jdk.incubator.sql2.TransactionCompletion) DataSource(jdk.incubator.sql2.DataSource) Session(jdk.incubator.sql2.Session) Test(org.junit.Test)

Aggregations

LifecycleEvent (com.oracle.adbaoverjdbc.test.SessionTest.TestLifecycleListener.LifecycleEvent)4 TestConfig (com.oracle.adbaoverjdbc.test.TestConfig)4 List (java.util.List)4 BlockingQueue (java.util.concurrent.BlockingQueue)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 ExecutionException (java.util.concurrent.ExecutionException)4 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)4 TimeUnit (java.util.concurrent.TimeUnit)4 TimeoutException (java.util.concurrent.TimeoutException)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Function (java.util.function.Function)4 Collectors (java.util.stream.Collectors)4 AdbaSessionProperty (jdk.incubator.sql2.AdbaSessionProperty)4 TransactionIsolation (jdk.incubator.sql2.AdbaSessionProperty.TransactionIsolation)4 DataSource (jdk.incubator.sql2.DataSource)4 DataSourceFactory (jdk.incubator.sql2.DataSourceFactory)4 Operation (jdk.incubator.sql2.Operation)4 OperationGroup (jdk.incubator.sql2.OperationGroup)4