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);
}
}
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));
}
}
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);
}
}
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));
}
}
Aggregations