use of jdk.incubator.sql2.DataSourceFactory in project oracle-db-examples by oracle-samples.
the class RowCountOperationTestWithDMLReturning method transaction.
/**
* Do something that approximates real work. Do a transaction. Uses
* TransactionCompletion, CompletionStage args, and catch Operation.
*/
@Test
public void transaction() throws Exception {
DataSourceFactory factory = DataSourceFactory.newFactory(FACTORY_NAME);
try (DataSource ds = factory.builder().url(URL).username(USER).password(PASSWORD).build();
Session session = ds.getSession(t -> System.out.println("ERROR: " + t.toString()))) {
ParameterizedRowCountOperation<Long> countOpReturning = session.<Long>rowCountOperation("insert into " + TEST_TABLE + "(C11) values (1)");
countOpReturning.returning(new String[] { "C12" }).collect(Collector.of(() -> null, (a, r) -> assertEquals(1, r.at(1).get(Integer.class).intValue()), (l, r) -> null)).onError(t -> fail(t.getMessage())).submit();
ParameterizedRowCountOperation<Long> countOpApplying = session.<Long>rowCountOperation("insert into " + TEST_TABLE + "(C11) values (1)");
countOpApplying.apply(Result.RowCount::getCount).onError(t -> fail(t.getMessage())).submit().getCompletionStage().thenAccept(c -> assertEquals(1L, c.longValue()));
session.catchErrors();
session.rollback().toCompletableFuture().get(TestConfig.getTimeout().toMillis(), TimeUnit.MILLISECONDS);
}
}
use of jdk.incubator.sql2.DataSourceFactory in project oracle-db-examples by oracle-samples.
the class ArrayRowCountOperationTest method transaction.
/**
* Do something that approximates real work. Do a transaction. Uses
* TransactionCompletion, CompletionStage args, and catch Operation.
*/
@Test
public void transaction() throws Exception {
DataSourceFactory factory = DataSourceFactory.newFactory(FACTORY_NAME);
try (DataSource ds = factory.builder().url(URL).username(USER).password(PASSWORD).build();
Session session = ds.getSession(t -> System.out.println("ERROR: " + t.toString()))) {
List<Integer> userList = new ArrayList<Integer>();
userList.add(100);
userList.add(101);
userList.add(102);
List<Integer> cityList = new ArrayList<Integer>();
cityList.add(30);
cityList.add(40);
cityList.add(10);
// public <A, S extends T> ArrayRowCountOperation<T> collect(Collector<? super Result.RowCount, A, S> c);
session.<Long>arrayRowCountOperation("insert into forum_user(id, city_id) values (?, ?)").set("1", userList).set("2", cityList).collect(Collector.of(() -> null, (a, r) -> assertEquals(1L, r.getCount()), (l, r) -> null)).onError(t -> fail(t.getMessage())).submit().getCompletionStage();
session.catchErrors();
session.rollback().toCompletableFuture().get(TestConfig.getTimeout().toMillis(), TimeUnit.MILLISECONDS);
}
}
use of jdk.incubator.sql2.DataSourceFactory in project oracle-db-examples by oracle-samples.
the class DataSourceFactoryTest method testNewFactoryNegative.
/**
* Assert DataSourceFactory.newFactory(String) returns null if the input
* is not the name of a factory class.
*/
@Test
public void testNewFactoryNegative() {
DataSourceFactory factory = DataSourceFactory.newFactory("NOT A FACTORY NAME");
assertNull(factory);
}
use of jdk.incubator.sql2.DataSourceFactory in project oracle-db-examples by oracle-samples.
the class ReadMe method readme.
public void readme(String url, String user, String password) {
// get the AoJ DataSourceFactory
DataSourceFactory factory = DataSourceFactory.newFactory("com.oracle.adbaoverjdbc.DataSourceFactory");
// get a DataSource and a Session
try (DataSource ds = factory.builder().url(url).username(user).password(password).build();
Session conn = ds.getSession(t -> System.out.println("ERROR: " + t.getMessage()))) {
// get a TransactionCompletion
TransactionCompletion trans = conn.transactionCompletion();
// select the EMPNO of CLARK
CompletionStage<Integer> idF = conn.<Integer>rowOperation("select empno, ename from emp where ename = ? for update").set("1", "CLARK", AdbaType.VARCHAR).collect(Collector.of(() -> new int[1], (a, r) -> {
a[0] = r.at("empno").get(Integer.class);
}, (l, r) -> null, a -> a[0])).submit().getCompletionStage();
// update CLARK to work in department 50
conn.<Long>rowCountOperation("update emp set deptno = ? where empno = ?").set("1", 50, AdbaType.INTEGER).set("2", idF, AdbaType.INTEGER).apply(c -> {
if (c.getCount() != 1L) {
trans.setRollbackOnly();
throw new SqlException("updated wrong number of rows", null, null, -1, null, -1);
}
return c.getCount();
}).onError(t -> t.printStackTrace()).submit();
// resume normal execution if there were any errors
conn.catchErrors();
// commit (or rollback) the transaction
conn.commitMaybeRollback(trans);
}
// wait for the async tasks to complete before exiting
ForkJoinPool.commonPool().awaitQuiescence(1, TimeUnit.MINUTES);
}
use of jdk.incubator.sql2.DataSourceFactory in project oracle-db-examples by oracle-samples.
the class HelloWorld method main.
public static void main(String[] args) {
DataSourceFactory factory = DataSourceFactory.newFactory(FACTORY_NAME);
if (factory == null) {
System.err.println("Error: Could not get a DataSourceFactory!");
} else {
System.out.println("DataSourceFactory: " + factory);
try (DataSource ds = factory.builder().url(URL).username(USER).password(PASSWORD).build();
Session session = ds.getSession()) {
System.out.println("Connected! DataSource: " + ds + " Session: " + session);
TestFixtures.createDummyTable(session);
session.rowOperation("SELECT 1 FROM dummy").collect(Collector.of(() -> null, (a, r) -> {
System.out.println(r.at(1).get(String.class));
}, (l, r) -> null, a -> {
System.out.println("end");
return null;
})).onError(ex -> {
ex.printStackTrace();
}).submit();
TestFixtures.dropDummyTable(session);
}
}
ForkJoinPool.commonPool().awaitQuiescence(1, TimeUnit.MINUTES);
}
Aggregations