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