Search in sources :

Example 1 with ConnectionHandle

use of org.apache.calcite.avatica.Meta.ConnectionHandle in project calcite-avatica by apache.

the class JdbcMetaTest method testRacingConnectionOpens.

@Test
public void testRacingConnectionOpens() throws Exception {
    final Map<String, String> properties = Collections.emptyMap();
    final Connection conn1 = Mockito.mock(Connection.class);
    final Connection conn2 = Mockito.mock(Connection.class);
    final ConnectionHandle ch1 = new ConnectionHandle("id1");
    // Override JdbcMeta to shim in a fake Connection object. Irrelevant for the test
    JdbcMeta meta = new JdbcMeta("jdbc:url") {

        @Override
        protected Connection createConnection(String url, Properties info) {
            // Hack to mimic the race condition of a connection being cached by another thread.
            getConnectionCache().put(ch1.id, conn1);
            // Return our "newly created" connectino
            return conn2;
        }
    };
    try {
        meta.openConnection(ch1, properties);
        fail("Should see an exception when the cache already contained our connection after" + " creating it");
    } catch (RuntimeException e) {
    // pass
    }
    // Our opened connection should get closed when this race condition happens
    Mockito.verify(conn2).close();
}
Also used : ConnectionHandle(org.apache.calcite.avatica.Meta.ConnectionHandle) Connection(java.sql.Connection) Properties(java.util.Properties) Test(org.junit.Test)

Example 2 with ConnectionHandle

use of org.apache.calcite.avatica.Meta.ConnectionHandle in project calcite-avatica by apache.

the class JdbcMetaTest method testPrepareSetsMaxRows.

@Test
public void testPrepareSetsMaxRows() throws Exception {
    final String id = UUID.randomUUID().toString();
    final String sql = "SELECT * FROM FOO";
    final int maxRows = 500;
    final ConnectionHandle ch = new ConnectionHandle(id);
    final AtomicInteger statementIdGenerator = new AtomicInteger(0);
    JdbcMeta meta = Mockito.mock(JdbcMeta.class);
    Connection connection = Mockito.mock(Connection.class);
    PreparedStatement statement = Mockito.mock(PreparedStatement.class);
    ResultSetMetaData resultSetMetaData = Mockito.mock(ResultSetMetaData.class);
    ParameterMetaData parameterMetaData = Mockito.mock(ParameterMetaData.class);
    @SuppressWarnings("unchecked") Cache<Integer, StatementInfo> statementCache = (Cache<Integer, StatementInfo>) Mockito.mock(Cache.class);
    Mockito.when(meta.getStatementIdGenerator()).thenReturn(statementIdGenerator);
    Mockito.when(meta.getStatementCache()).thenReturn(statementCache);
    Mockito.when(meta.getConnection(id)).thenReturn(connection);
    Mockito.when(connection.prepareStatement(sql)).thenReturn(statement);
    Mockito.when(statement.isWrapperFor(AvaticaPreparedStatement.class)).thenReturn(false);
    Mockito.when(statement.getMetaData()).thenReturn(resultSetMetaData);
    Mockito.when(statement.getParameterMetaData()).thenReturn(parameterMetaData);
    // Call the real methods
    Mockito.doCallRealMethod().when(meta).setMaxRows(statement, maxRows);
    Mockito.doCallRealMethod().when(meta).prepare(ch, sql, maxRows);
    meta.prepare(ch, sql, maxRows);
    Mockito.verify(statement).setMaxRows(maxRows);
}
Also used : ConnectionHandle(org.apache.calcite.avatica.Meta.ConnectionHandle) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) AvaticaPreparedStatement(org.apache.calcite.avatica.AvaticaPreparedStatement) ResultSetMetaData(java.sql.ResultSetMetaData) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ParameterMetaData(java.sql.ParameterMetaData) Cache(com.google.common.cache.Cache) Test(org.junit.Test)

Example 3 with ConnectionHandle

use of org.apache.calcite.avatica.Meta.ConnectionHandle in project calcite-avatica by apache.

the class JdbcMetaTest method testConcurrentConnectionOpening.

@Test
public void testConcurrentConnectionOpening() throws Exception {
    final Map<String, String> properties = Collections.emptyMap();
    final Connection conn = Mockito.mock(Connection.class);
    // Override JdbcMeta to shim in a fake Connection object. Irrelevant for the test
    JdbcMeta meta = new JdbcMeta("jdbc:url") {

        @Override
        protected Connection createConnection(String url, Properties info) {
            return conn;
        }
    };
    ConnectionHandle ch1 = new ConnectionHandle("id1");
    meta.openConnection(ch1, properties);
    assertEquals(conn, meta.getConnectionCache().getIfPresent(ch1.id));
    try {
        meta.openConnection(ch1, properties);
        fail("Should not be allowed to open two connections with the same ID");
    } catch (RuntimeException e) {
    // pass
    }
    // Cached object should not change
    assertEquals(conn, meta.getConnectionCache().getIfPresent(ch1.id));
}
Also used : ConnectionHandle(org.apache.calcite.avatica.Meta.ConnectionHandle) Connection(java.sql.Connection) Properties(java.util.Properties) Test(org.junit.Test)

Aggregations

Connection (java.sql.Connection)3 ConnectionHandle (org.apache.calcite.avatica.Meta.ConnectionHandle)3 Test (org.junit.Test)3 Properties (java.util.Properties)2 Cache (com.google.common.cache.Cache)1 ParameterMetaData (java.sql.ParameterMetaData)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AvaticaPreparedStatement (org.apache.calcite.avatica.AvaticaPreparedStatement)1