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));
}
}
}
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");
}
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());
}
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;
}
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);
}
Aggregations