Search in sources :

Example 11 with DataSourceFactory

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);
    }
}
Also used : ParameterizedRowCountOperation(jdk.incubator.sql2.ParameterizedRowCountOperation) AfterClass(org.junit.AfterClass) DataSourceFactory(jdk.incubator.sql2.DataSourceFactory) BeforeClass(org.junit.BeforeClass) Test(org.junit.Test) TimeUnit(java.util.concurrent.TimeUnit) DataSource(jdk.incubator.sql2.DataSource) TestConfig(com.oracle.adbaoverjdbc.test.TestConfig) Result(jdk.incubator.sql2.Result) ForkJoinPool(java.util.concurrent.ForkJoinPool) Collector(java.util.stream.Collector) Assert(org.junit.Assert) Session(jdk.incubator.sql2.Session) DataSourceFactory(jdk.incubator.sql2.DataSourceFactory) DataSource(jdk.incubator.sql2.DataSource) Session(jdk.incubator.sql2.Session) Test(org.junit.Test)

Example 12 with DataSourceFactory

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);
    }
}
Also used : DataSourceFactory(jdk.incubator.sql2.DataSourceFactory) ArrayList(java.util.ArrayList) DataSource(jdk.incubator.sql2.DataSource) Session(jdk.incubator.sql2.Session) Test(org.junit.Test)

Example 13 with DataSourceFactory

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);
}
Also used : DataSourceFactory(jdk.incubator.sql2.DataSourceFactory) Test(org.junit.Test)

Example 14 with DataSourceFactory

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);
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) SqlException(jdk.incubator.sql2.SqlException) DataSource(jdk.incubator.sql2.DataSource) CompletionStage(java.util.concurrent.CompletionStage) DataSourceFactory(jdk.incubator.sql2.DataSourceFactory) ForkJoinPool(java.util.concurrent.ForkJoinPool) AdbaType(jdk.incubator.sql2.AdbaType) Collector(java.util.stream.Collector) Session(jdk.incubator.sql2.Session) TransactionCompletion(jdk.incubator.sql2.TransactionCompletion) DataSourceFactory(jdk.incubator.sql2.DataSourceFactory) SqlException(jdk.incubator.sql2.SqlException) TransactionCompletion(jdk.incubator.sql2.TransactionCompletion) DataSource(jdk.incubator.sql2.DataSource) Session(jdk.incubator.sql2.Session)

Example 15 with DataSourceFactory

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);
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) DataSource(jdk.incubator.sql2.DataSource) DataSourceFactory(jdk.incubator.sql2.DataSourceFactory) ForkJoinPool(java.util.concurrent.ForkJoinPool) Test(org.junit.Test) Collector(java.util.stream.Collector) Session(jdk.incubator.sql2.Session) DataSourceFactory(jdk.incubator.sql2.DataSourceFactory) DataSource(jdk.incubator.sql2.DataSource) Session(jdk.incubator.sql2.Session)

Aggregations

DataSourceFactory (jdk.incubator.sql2.DataSourceFactory)28 DataSource (jdk.incubator.sql2.DataSource)24 Session (jdk.incubator.sql2.Session)22 Test (org.junit.Test)22 ForkJoinPool (java.util.concurrent.ForkJoinPool)14 TimeUnit (java.util.concurrent.TimeUnit)14 Collector (java.util.stream.Collector)14 ArrayList (java.util.ArrayList)12 SqlException (jdk.incubator.sql2.SqlException)12 List (java.util.List)10 CompletableFuture (java.util.concurrent.CompletableFuture)10 CompletionStage (java.util.concurrent.CompletionStage)10 Flow (java.util.concurrent.Flow)10 AdbaType (jdk.incubator.sql2.AdbaType)10 MultiOperation (jdk.incubator.sql2.MultiOperation)10 Result (jdk.incubator.sql2.Result)10 RowPublisherOperation (jdk.incubator.sql2.RowPublisherOperation)10 TransactionCompletion (jdk.incubator.sql2.TransactionCompletion)10 AfterClass (org.junit.AfterClass)10 Assert (org.junit.Assert)10