Search in sources :

Example 6 with BatchReadOnlyTransaction

use of com.google.cloud.spanner.BatchReadOnlyTransaction in project spanner-jdbc by olavloite.

the class CloudSpannerPartitionResultSetTest method createSubject.

private CloudSpannerPartitionResultSet createSubject() {
    Partition partition = mock(Partition.class);
    BatchReadOnlyTransaction transaction = mock(BatchReadOnlyTransaction.class);
    ResultSet rs = CloudSpannerResultSetTest.getMockResultSet();
    when(transaction.execute(partition)).thenReturn(rs);
    return new CloudSpannerPartitionResultSet(mock(CloudSpannerStatement.class), transaction, partition, "SELECT * FROM FOO");
}
Also used : Partition(com.google.cloud.spanner.Partition) CloudSpannerStatement(nl.topicus.jdbc.statement.CloudSpannerStatement) BatchReadOnlyTransaction(com.google.cloud.spanner.BatchReadOnlyTransaction) ResultSet(com.google.cloud.spanner.ResultSet)

Example 7 with BatchReadOnlyTransaction

use of com.google.cloud.spanner.BatchReadOnlyTransaction in project beam by apache.

the class CreateTransactionFn method processElement.

@ProcessElement
public void processElement(ProcessContext c) throws Exception {
    BatchReadOnlyTransaction tx = spannerAccessor.getBatchClient().batchReadOnlyTransaction(timestampBound);
    c.output(Transaction.create(tx.getBatchTransactionId()));
}
Also used : BatchReadOnlyTransaction(com.google.cloud.spanner.BatchReadOnlyTransaction)

Example 8 with BatchReadOnlyTransaction

use of com.google.cloud.spanner.BatchReadOnlyTransaction in project spanner-jdbc by olavloite.

the class BatchReadOnlyTest method testExecuteBatchReadOnly.

@Test
public void testExecuteBatchReadOnly() throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
    for (int testRun = 0; testRun < 2; testRun++) {
        final int numberOfPartitions = 6;
        BatchClient batchClient = mock(BatchClient.class);
        BatchReadOnlyTransaction tx = mock(BatchReadOnlyTransaction.class);
        List<Partition> partitions = new ArrayList<>(numberOfPartitions);
        for (int i = 0; i < numberOfPartitions; i++) partitions.add(mock(Partition.class));
        when(tx.partitionQuery(any(), any())).then(new Returns(partitions));
        when(batchClient.batchReadOnlyTransaction(TimestampBound.strong())).then(new Returns(tx));
        Field field = CloudSpannerTransaction.class.getDeclaredField("batchClient");
        field.setAccessible(true);
        field.set(connection.getTransaction(), batchClient);
        connection.setBatchReadOnly(true);
        Statement statement;
        if (testRun % 2 == 0) {
            statement = connection.createStatement();
            assertTrue(statement.execute(SELECT_ALL_FROM_FOO));
        } else {
            PreparedStatement ps = connection.prepareStatement(SELECT_ALL_FROM_FOO);
            assertTrue(ps.execute());
            statement = ps;
        }
        List<ResultSet> resultSets = new ArrayList<>();
        do {
            resultSets.add(statement.getResultSet());
        } while (statement.getMoreResults());
        assertEquals(numberOfPartitions, resultSets.size());
    }
}
Also used : Partition(com.google.cloud.spanner.Partition) Returns(org.mockito.internal.stubbing.answers.Returns) Field(java.lang.reflect.Field) BatchClient(com.google.cloud.spanner.BatchClient) BatchReadOnlyTransaction(com.google.cloud.spanner.BatchReadOnlyTransaction) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test) UnitTest(nl.topicus.jdbc.test.category.UnitTest)

Aggregations

BatchReadOnlyTransaction (com.google.cloud.spanner.BatchReadOnlyTransaction)8 Partition (com.google.cloud.spanner.Partition)7 ResultSet (com.google.cloud.spanner.ResultSet)5 BatchClient (com.google.cloud.spanner.BatchClient)2 Test (org.junit.Test)2 Timestamp (com.google.cloud.Timestamp)1 BatchTransactionId (com.google.cloud.spanner.BatchTransactionId)1 FakeBatchTransactionId (com.google.cloud.spanner.FakeBatchTransactionId)1 ReadQueryUpdateTransactionOption (com.google.cloud.spanner.Options.ReadQueryUpdateTransactionOption)1 PartitionOptions (com.google.cloud.spanner.PartitionOptions)1 Spanner (com.google.cloud.spanner.Spanner)1 SpannerOptions (com.google.cloud.spanner.SpannerOptions)1 Struct (com.google.cloud.spanner.Struct)1 TimestampBound (com.google.cloud.spanner.TimestampBound)1 Field (java.lang.reflect.Field)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1 ExecutorService (java.util.concurrent.ExecutorService)1