Search in sources :

Example 16 with CloudSpannerConnection

use of nl.topicus.jdbc.CloudSpannerConnection in project spanner-jdbc by olavloite.

the class ExtendedModeIT method test3_ExtendedModeDeleteWithId.

@Test
public void test3_ExtendedModeDeleteWithId() throws SQLException {
    ((CloudSpannerConnection) getConnection()).setAllowExtendedMode(true);
    try (ResultSet rs = getConnection().createStatement().executeQuery("select count(*) from test where id=30 and name like '3%'")) {
        while (rs.next()) {
            assertEquals(1L, rs.getLong(1));
        }
    }
    PreparedStatement ps = getConnection().prepareStatement("delete from test where id=? and name like ?");
    ps.setLong(1, 30L);
    ps.setString(2, "3%");
    ps.executeUpdate();
    getConnection().commit();
    try (ResultSet rs = getConnection().createStatement().executeQuery("select count(*) from test where id=30 and name like '3%'")) {
        while (rs.next()) {
            assertEquals(0L, rs.getLong(1));
        }
    }
}
Also used : ResultSet(java.sql.ResultSet) CloudSpannerConnection(nl.topicus.jdbc.CloudSpannerConnection) PreparedStatement(java.sql.PreparedStatement) IntegrationTest(nl.topicus.jdbc.test.category.IntegrationTest) Test(org.junit.Test)

Example 17 with CloudSpannerConnection

use of nl.topicus.jdbc.CloudSpannerConnection in project spanner-jdbc by olavloite.

the class ConnectionPoolingTester method testPooling.

public void testPooling(CloudSpannerConnection original) throws SQLException, PropertyVetoException, InterruptedException {
    log.info("Starting connection pooling tests");
    ComboPooledDataSource cpds = new ComboPooledDataSource();
    cpds.setDriverClass("nl.topicus.jdbc.CloudSpannerDriver");
    cpds.setJdbcUrl(original.getUrl());
    cpds.setProperties(original.getSuppliedProperties());
    cpds.setInitialPoolSize(5);
    cpds.setMinPoolSize(5);
    cpds.setAcquireIncrement(5);
    cpds.setMaxPoolSize(20);
    cpds.setCheckoutTimeout(1000);
    log.info("Connection pool created and configured. Acquiring connection from pool");
    Assert.assertEquals(0, cpds.getNumBusyConnectionsDefaultUser());
    Connection connection = cpds.getConnection();
    Assert.assertNotNull(connection);
    Assert.assertEquals(1, cpds.getNumBusyConnectionsDefaultUser());
    connection.close();
    while (cpds.getNumBusyConnections() == 1) {
        TimeUnit.MILLISECONDS.sleep(100L);
    }
    Assert.assertEquals(0, cpds.getNumBusyConnectionsDefaultUser());
    log.info("About to acquire 10 connections");
    Connection[] connections = new Connection[10];
    for (int i = 0; i < connections.length; i++) {
        connections[i] = cpds.getConnection();
        Assert.assertEquals(i + 1, cpds.getNumBusyConnectionsDefaultUser());
    }
    log.info("10 connections acquired, closing connections...");
    for (int i = 0; i < connections.length; i++) {
        connections[i].close();
    }
    log.info("10 connections closed");
    log.info("Acquiring 20 connections");
    // Check that we can get 20 connections
    connections = new Connection[20];
    for (int i = 0; i < connections.length; i++) {
        connections[i] = cpds.getConnection();
        connections[i].prepareStatement("SELECT 1").executeQuery();
    }
    log.info("20 connections acquired, trying to get one more");
    // Verify that we can't get a connection now
    connection = null;
    try {
        connection = cpds.getConnection();
    } catch (SQLException e) {
        // timeout exception
        log.info("Exception when trying to get one more connection (this is expected)");
    }
    log.info("Closing 20 connections");
    Assert.assertNull(connection);
    for (int i = 0; i < connections.length; i++) {
        connections[i].close();
    }
    log.info("Closing connection pool");
    cpds.close();
    log.info("Finished tests");
}
Also used : ComboPooledDataSource(com.mchange.v2.c3p0.ComboPooledDataSource) SQLException(java.sql.SQLException) CloudSpannerConnection(nl.topicus.jdbc.CloudSpannerConnection) Connection(java.sql.Connection)

Example 18 with CloudSpannerConnection

use of nl.topicus.jdbc.CloudSpannerConnection in project spanner-jdbc by olavloite.

the class TransactionTester method runRepeatableReadTest.

private void runRepeatableReadTest() throws SQLException {
    // Do a read on the original connection. This starts a transaction.
    List<Object[]> originalRows = getResultList("SELECT * FROM TEST");
    // Open a new connection and do an insert. This also starts a different
    // transaction.
    CloudSpannerConnection csConnection = (CloudSpannerConnection) connection;
    Connection otherConnection = DriverManager.getConnection(csConnection.getUrl(), csConnection.getSuppliedProperties());
    otherConnection.setAutoCommit(false);
    insertRowInTest(100001l);
    // Commit the changes
    otherConnection.commit();
    otherConnection.close();
    // Do a read on the original connection. This read should be equal to
    // the previous read.
    List<Object[]> rows = getResultList("SELECT * FROM TEST");
    Assert.assertEquals(originalRows.size(), rows.size());
    for (int index = 0; index < rows.size(); index++) Assert.assertArrayEquals(originalRows.get(index), rows.get(index));
    // Do a commit and then do the same query. Now the new row should be
    // found.
    connection.commit();
    List<Object[]> newRows = getResultList("SELECT * FROM TEST");
    Assert.assertEquals(originalRows.size() + 1, newRows.size());
}
Also used : CloudSpannerConnection(nl.topicus.jdbc.CloudSpannerConnection) Connection(java.sql.Connection) CloudSpannerConnection(nl.topicus.jdbc.CloudSpannerConnection) Savepoint(java.sql.Savepoint)

Example 19 with CloudSpannerConnection

use of nl.topicus.jdbc.CloudSpannerConnection in project spanner-jdbc by olavloite.

the class CloudSpannerTestObjects method createConnection.

public static CloudSpannerConnection createConnection() throws SQLException {
    CloudSpannerConnection connection = Mockito.mock(CloudSpannerConnection.class);
    Mockito.doCallRealMethod().when(connection).setAutoCommit(Mockito.anyBoolean());
    Mockito.when(connection.getAutoCommit()).thenCallRealMethod();
    connection.setAutoCommit(false);
    Mockito.doCallRealMethod().when(connection).setBatchReadOnly(Mockito.anyBoolean());
    Mockito.when(connection.isBatchReadOnly()).thenCallRealMethod();
    Mockito.doCallRealMethod().when(connection).setReadOnly(Mockito.anyBoolean());
    Mockito.when(connection.isReadOnly()).thenCallRealMethod();
    Mockito.when(connection.isAllowExtendedMode()).thenAnswer(new Returns(true));
    Mockito.when(connection.createArrayOf(Mockito.anyString(), Mockito.any())).thenCallRealMethod();
    CloudSpannerDatabaseMetaData metadata = createMetaData();
    Mockito.when(connection.getMetaData()).thenReturn(metadata);
    CloudSpannerTransaction transaction = Mockito.mock(CloudSpannerTransaction.class);
    Mockito.when(transaction.executeQuery(Mockito.any())).thenReturn(Mockito.mock(com.google.cloud.spanner.ResultSet.class));
    Mockito.when(transaction.partitionQuery(Mockito.any(), Mockito.any())).thenReturn(Arrays.asList(mock(Partition.class), mock(Partition.class), mock(Partition.class)));
    Mockito.when(connection.getTransaction()).thenReturn(transaction);
    TableKeyMetaData tableFoo = Mockito.mock(TableKeyMetaData.class);
    Mockito.when(tableFoo.getKeyColumns()).thenAnswer(new Returns(Arrays.asList("ID")));
    Mockito.when(connection.getTable(Mockito.matches(Pattern.compile("FOO", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE)))).thenAnswer(new Returns(tableFoo));
    TableKeyMetaData tableBar = Mockito.mock(TableKeyMetaData.class);
    Mockito.when(tableBar.getKeyColumns()).thenAnswer(new Returns(Arrays.asList("ID1", "ID2")));
    Mockito.when(connection.getTable(Mockito.matches(Pattern.compile("BAR", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE)))).thenAnswer(new Returns(tableBar));
    Mockito.when(connection.getLogger()).thenAnswer(new Returns(new Logger()));
    mockXAMethods(connection);
    return connection;
}
Also used : TableKeyMetaData(nl.topicus.jdbc.MetaDataStore.TableKeyMetaData) Returns(org.mockito.internal.stubbing.answers.Returns) CloudSpannerDatabaseMetaData(nl.topicus.jdbc.CloudSpannerDatabaseMetaData) CloudSpannerResultSet(nl.topicus.jdbc.resultset.CloudSpannerResultSet) ResultSet(java.sql.ResultSet) CloudSpannerConnection(nl.topicus.jdbc.CloudSpannerConnection) CloudSpannerTransaction(nl.topicus.jdbc.transaction.CloudSpannerTransaction) Logger(nl.topicus.jdbc.Logger)

Example 20 with CloudSpannerConnection

use of nl.topicus.jdbc.CloudSpannerConnection in project spanner-jdbc by olavloite.

the class CloudSpannerXADataSourceTest method testGetConnectionWithUserNameAndPassword.

@Test
public void testGetConnectionWithUserNameAndPassword() throws SQLException {
    CloudSpannerXADataSource subject = new CloudSpannerXADataSource();
    CloudSpannerDataSourceTest.setCommonDataSourceTestProperties(subject);
    subject.setCreateXATable(false);
    CloudSpannerXAConnection connection = subject.getXAConnection("test", "test");
    assertNotNull(connection);
    Connection con = connection.getConnection();
    Assert.assertTrue(con.isWrapperFor(CloudSpannerConnection.class));
    CloudSpannerConnection cloudSpannerConnection = con.unwrap(CloudSpannerConnection.class);
    CloudSpannerDataSourceTest.testCommonDataSourceTestProperties(cloudSpannerConnection);
}
Also used : CloudSpannerXADataSource(nl.topicus.jdbc.CloudSpannerXADataSource) CloudSpannerConnection(nl.topicus.jdbc.CloudSpannerConnection) Connection(java.sql.Connection) CloudSpannerConnection(nl.topicus.jdbc.CloudSpannerConnection) UnitTest(nl.topicus.jdbc.test.category.UnitTest) Test(org.junit.Test) CloudSpannerDataSourceTest(nl.topicus.jdbc.CloudSpannerDataSourceTest)

Aggregations

CloudSpannerConnection (nl.topicus.jdbc.CloudSpannerConnection)37 Test (org.junit.Test)22 UnitTest (nl.topicus.jdbc.test.category.UnitTest)17 ResultSet (java.sql.ResultSet)11 Connection (java.sql.Connection)5 PreparedStatement (java.sql.PreparedStatement)5 IntegrationTest (nl.topicus.jdbc.test.category.IntegrationTest)5 SQLException (java.sql.SQLException)4 Returns (org.mockito.internal.stubbing.answers.Returns)4 TableKeyMetaData (nl.topicus.jdbc.MetaDataStore.TableKeyMetaData)3 CloudSpannerResultSet (nl.topicus.jdbc.resultset.CloudSpannerResultSet)3 Select (net.sf.jsqlparser.statement.select.Select)2 CloudSpannerDataSourceTest (nl.topicus.jdbc.CloudSpannerDataSourceTest)2 CloudSpannerDatabaseMetaData (nl.topicus.jdbc.CloudSpannerDatabaseMetaData)2 CloudSpannerXADataSource (nl.topicus.jdbc.CloudSpannerXADataSource)2 CloudSpannerSQLException (nl.topicus.jdbc.exception.CloudSpannerSQLException)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Code (com.google.rpc.Code)1 ComboPooledDataSource (com.mchange.v2.c3p0.ComboPooledDataSource)1 PropertyVetoException (java.beans.PropertyVetoException)1