Search in sources :

Example 1 with TransactionOutcome

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

the class Session method jdbcEndTransaction.

TransactionOutcome jdbcEndTransaction(SimpleOperation<TransactionOutcome> op, TransactionCompletion trans) {
    try {
        if (trans.endWithCommit(this)) {
            // DEBUG
            group.logger.log(Level.FINE, () -> "commit");
            jdbcConnection.commit();
            return TransactionOutcome.COMMIT;
        } else {
            // DEBUG
            group.logger.log(Level.FINE, () -> "rollback");
            jdbcConnection.rollback();
            return TransactionOutcome.ROLLBACK;
        }
    } 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 2 with TransactionOutcome

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

the class Session method jdbcEndTransaction.

TransactionOutcome jdbcEndTransaction(SimpleOperation<TransactionOutcome> op, TransactionCompletion trans) {
    try {
        if (trans.endWithCommit(this)) {
            // DEBUG
            group.logger.log(Level.FINE, () -> "commit");
            jdbcConnection.commit();
            return TransactionOutcome.COMMIT;
        } else {
            // DEBUG
            group.logger.log(Level.FINE, () -> "rollback");
            jdbcConnection.rollback();
            return TransactionOutcome.ROLLBACK;
        }
    } 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 TransactionOutcome

use of jdk.incubator.sql2.TransactionOutcome 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 4 with TransactionOutcome

use of jdk.incubator.sql2.TransactionOutcome 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)2 TestConfig (com.oracle.adbaoverjdbc.test.TestConfig)2 SQLException (java.sql.SQLException)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 DataSourceFactory (jdk.incubator.sql2.DataSourceFactory)2 Operation (jdk.incubator.sql2.Operation)2