Search in sources :

Example 1 with SqlException

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

the class MultiOperationTest method multiRowCountOperation.

/**
 * Do something that approximates real work. Do a transaction. Uses
 * TransactionCompletion, CompletionStage args, and catch Operation.
 */
@Test
public void multiRowCountOperation() throws Exception {
    fail("TODO: Fix this test");
    DataSourceFactory factory = DataSourceFactory.newFactory(FACTORY_NAME);
    try (DataSource ds = factory.builder().url(URL).username(USER).password(PASSWORD).build();
        Session session = ds.getSession(t -> fail("ERROR: " + t.getMessage()))) {
        assertNotNull(session);
        TransactionCompletion trans = session.transactionCompletion();
        MultiOperation multiOp = session.multiOperation("update forum_user set city_id = ?" + " where id = ?").set("1", 40, AdbaType.INTEGER).set("2", 7782, AdbaType.INTEGER).onError(t -> {
            System.out.println(t.toString());
        });
        RowCountOperation<Long> cntOp = multiOp.<Long>rowCountOperation();
        multiOp.submit();
        cntOp.apply(c -> {
            long count = c.getCount();
            if (count != 1L)
                throw new SqlException("updated wrong number of rows", null, null, -1, null, -1);
            return count;
        }).onError(t -> t.printStackTrace()).submit().getCompletionStage().thenAccept(c -> {
            assertTrue((long) c > 0);
        });
        session.catchErrors();
        session.rollback().toCompletableFuture().get(TestConfig.getTimeout().toMillis(), TimeUnit.MILLISECONDS);
    }
}
Also used : DataSourceFactory(jdk.incubator.sql2.DataSourceFactory) BeforeClass(org.junit.BeforeClass) RowOperation(jdk.incubator.sql2.RowOperation) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) SqlException(jdk.incubator.sql2.SqlException) OperationGroup(jdk.incubator.sql2.OperationGroup) Flow(java.util.concurrent.Flow) BiConsumer(java.util.function.BiConsumer) Collector(java.util.stream.Collector) Session(jdk.incubator.sql2.Session) AfterClass(org.junit.AfterClass) Properties(java.util.Properties) JDBC_CONNECTION_PROPERTIES(com.oracle.adbaoverjdbc.JdbcConnectionProperties.JDBC_CONNECTION_PROPERTIES) AdbaType(jdk.incubator.sql2.AdbaType) Test(org.junit.Test) RowPublisherOperation(jdk.incubator.sql2.RowPublisherOperation) TransactionCompletion(jdk.incubator.sql2.TransactionCompletion) Date(java.sql.Date) TimeUnit(java.util.concurrent.TimeUnit) DataSource(jdk.incubator.sql2.DataSource) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) Result(jdk.incubator.sql2.Result) RowCountOperation(jdk.incubator.sql2.RowCountOperation) ForkJoinPool(java.util.concurrent.ForkJoinPool) MultiOperation(jdk.incubator.sql2.MultiOperation) Assert(org.junit.Assert) Submission(jdk.incubator.sql2.Submission) DataSourceFactory(jdk.incubator.sql2.DataSourceFactory) MultiOperation(jdk.incubator.sql2.MultiOperation) SqlException(jdk.incubator.sql2.SqlException) TransactionCompletion(jdk.incubator.sql2.TransactionCompletion) DataSource(jdk.incubator.sql2.DataSource) Session(jdk.incubator.sql2.Session) Test(org.junit.Test)

Example 2 with SqlException

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

the class RowBaseOperation method initRowOperationResultSet.

protected void initRowOperationResultSet(PreparedStatement jdbcStatement, ResultSet resultSet) {
    try {
        this.jdbcStatement = jdbcStatement;
        initFetchSize();
        this.resultSet = resultSet;
        resultSetMetaData = this.resultSet.getMetaData();
        rowsRemain = true;
        rowCount = 0;
    } catch (SQLException ex) {
        throw new SqlException(ex.getMessage(), ex, ex.getSQLState(), ex.getErrorCode(), sqlString, -1);
    }
}
Also used : SQLException(java.sql.SQLException) SqlException(jdk.incubator.sql2.SqlException)

Example 3 with SqlException

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

the class RowBaseOperation method executeJdbcQuery.

protected void executeJdbcQuery() {
    checkCanceled();
    try {
        jdbcStatement = session.prepareStatement(sqlString);
        initFetchSize();
        setParameters.forEach((String k, ParameterValue v) -> {
            v.set(jdbcStatement, k);
        });
        group.logger.log(Level.FINE, () -> "executeQuery(\"" + sqlString + "\")");
        resultSet = jdbcStatement.executeQuery();
        resultSetMetaData = resultSet.getMetaData();
        rowsRemain = true;
        rowCount = 0;
    } catch (SQLException ex) {
        throw new SqlException(ex.getMessage(), ex, ex.getSQLState(), ex.getErrorCode(), sqlString, -1);
    }
}
Also used : SQLException(java.sql.SQLException) SqlException(jdk.incubator.sql2.SqlException)

Example 4 with SqlException

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

the class Session method jdbcConnect.

// JDBC operations. These are all blocking
private Void jdbcConnect(com.oracle.adbaoverjdbc.Operation<Void> op) {
    try {
        Properties info = (Properties) properties.get(JdbcConnectionProperties.JDBC_CONNECTION_PROPERTIES);
        info = (Properties) (info == null ? JdbcConnectionProperties.JDBC_CONNECTION_PROPERTIES.defaultValue() : info.clone());
        Properties sensitiveInfo = (Properties) properties.get(JdbcConnectionProperties.SENSITIVE_JDBC_CONNECTION_PROPERTIES);
        if (sensitiveInfo != null)
            info.putAll(sensitiveInfo);
        String user = (String) properties.get(AdbaSessionProperty.USER);
        if (user != null)
            info.setProperty("user", user);
        String password = (String) properties.get(AdbaSessionProperty.PASSWORD);
        if (password != null)
            info.setProperty("password", password);
        String url = (String) properties.get(AdbaSessionProperty.URL);
        Properties p = info;
        group.logger.log(Level.FINE, () -> "DriverManager.getSession(\"" + url + "\", " + p + ")");
        jdbcConnection = DriverManager.getConnection(url, info);
        jdbcConnection.setAutoCommit(false);
        if (sessionLifecycle == Lifecycle.ABORTING)
            closeImmediate();
        else
            setLifecycle(Session.Lifecycle.ATTACHED);
        return null;
    } catch (SQLException ex) {
        setLifecycle(Session.Lifecycle.CLOSED);
        throw new SqlException(ex.getMessage(), ex, ex.getSQLState(), ex.getErrorCode(), null, -1);
    }
}
Also used : SQLException(java.sql.SQLException) SqlException(jdk.incubator.sql2.SqlException) Properties(java.util.Properties)

Example 5 with SqlException

use of jdk.incubator.sql2.SqlException 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)

Aggregations

SqlException (jdk.incubator.sql2.SqlException)34 SQLException (java.sql.SQLException)30 AdbaType (jdk.incubator.sql2.AdbaType)6 ResultSet (java.sql.ResultSet)4 List (java.util.List)4 Properties (java.util.Properties)4 CompletionStage (java.util.concurrent.CompletionStage)4 ForkJoinPool (java.util.concurrent.ForkJoinPool)4 TimeUnit (java.util.concurrent.TimeUnit)4 Collector (java.util.stream.Collector)4 DataSource (jdk.incubator.sql2.DataSource)4 DataSourceFactory (jdk.incubator.sql2.DataSourceFactory)4 RowCountOperation (jdk.incubator.sql2.RowCountOperation)4 RowOperation (jdk.incubator.sql2.RowOperation)4 RowPublisherOperation (jdk.incubator.sql2.RowPublisherOperation)4 Session (jdk.incubator.sql2.Session)4 TransactionCompletion (jdk.incubator.sql2.TransactionCompletion)4 JDBC_CONNECTION_PROPERTIES (com.oracle.adbaoverjdbc.JdbcConnectionProperties.JDBC_CONNECTION_PROPERTIES)2 Date (java.sql.Date)2 ArrayList (java.util.ArrayList)2