Search in sources :

Example 1 with StatementHolder

use of org.vibur.dbcp.stcache.StatementHolder in project vibur-dbcp by vibur.

the class ViburDBCPDataSourceTest method testTwoPreparedSelectStatementsWithStatementsCache.

@Test
public void testTwoPreparedSelectStatementsWithStatementsCache() throws SQLException {
    ViburDBCPDataSource ds = createDataSourceWithStatementsCache();
    ConcurrentMap<StatementMethod, StatementHolder> mockedStatementCache = mockStatementCache(ds);
    try (Connection connection = ds.getConnection()) {
        executeAndVerifyPreparedSelectStatement(connection);
        executeAndVerifyPreparedSelectStatementByLastName(connection);
        InOrder inOrder = inOrder(mockedStatementCache);
        inOrder.verify(mockedStatementCache).get(key1.capture());
        inOrder.verify(mockedStatementCache).putIfAbsent(same(key1.getValue()), val1.capture());
        inOrder.verify(mockedStatementCache).get(key2.capture());
        inOrder.verify(mockedStatementCache).putIfAbsent(same(key2.getValue()), val2.capture());
        // key1 will be evicted from the StatementCache because its capacity is set to 1.
        assertEquals(1, mockedStatementCache.size());
        assertTrue(mockedStatementCache.containsKey(key2.getValue()));
        assertNotEquals(key1.getValue(), key2.getValue());
        assertEquals(EVICTED, val1.getValue().state().get());
        assertEquals(AVAILABLE, val2.getValue().state().get());
    }
}
Also used : InOrder(org.mockito.InOrder) StatementMethod(org.vibur.dbcp.stcache.StatementMethod) TakenConnection(org.vibur.dbcp.pool.TakenConnection) StatementHolder(org.vibur.dbcp.stcache.StatementHolder) Test(org.junit.Test)

Example 2 with StatementHolder

use of org.vibur.dbcp.stcache.StatementHolder in project vibur-dbcp by vibur.

the class ViburDBCPDataSourceTest method testPreparedSelectStatementWithStatementsCache.

@Test
public void testPreparedSelectStatementWithStatementsCache() throws SQLException {
    ViburDBCPDataSource ds = createDataSourceWithStatementsCache();
    ConcurrentMap<StatementMethod, StatementHolder> mockedStatementCache = mockStatementCache(ds);
    try (Connection connection = ds.getConnection()) {
        executeAndVerifyPreparedSelectStatement(connection);
        executeAndVerifyPreparedSelectStatement(connection);
        InOrder inOrder = inOrder(mockedStatementCache);
        inOrder.verify(mockedStatementCache).get(key1.capture());
        inOrder.verify(mockedStatementCache).putIfAbsent(same(key1.getValue()), val1.capture());
        inOrder.verify(mockedStatementCache).get(key2.capture());
        assertEquals(1, mockedStatementCache.size());
        assertTrue(mockedStatementCache.containsKey(key1.getValue()));
        assertEquals(key1.getValue(), key2.getValue());
        assertEquals(AVAILABLE, val1.getValue().state().get());
    }
}
Also used : InOrder(org.mockito.InOrder) StatementMethod(org.vibur.dbcp.stcache.StatementMethod) TakenConnection(org.vibur.dbcp.pool.TakenConnection) StatementHolder(org.vibur.dbcp.stcache.StatementHolder) Test(org.junit.Test)

Example 3 with StatementHolder

use of org.vibur.dbcp.stcache.StatementHolder in project hibernate-orm by hibernate.

the class ViburDBCPConnectionProviderTest method testSelectStatementWithStatementsCache.

@Test
public void testSelectStatementWithStatementsCache() {
    setUpPoolAndDatabase(1, 10);
    ConnectionProvider cp = sessionFactory().getServiceRegistry().getService(ConnectionProvider.class);
    ViburDBCPDataSource ds = ((ViburDBCPConnectionProvider) cp).getDataSource();
    ConcurrentMap<StatementMethod, StatementHolder> mockedStatementCache = mockStatementCache(ds);
    doInHibernate(this::sessionFactory, ViburDBCPConnectionProviderTest::executeAndVerifySelect);
    // We set above the poolMaxSize = 1, that's why the second session will get and use the same underlying connection.
    doInHibernate(this::sessionFactory, ViburDBCPConnectionProviderTest::executeAndVerifySelect);
    InOrder inOrder = inOrder(mockedStatementCache);
    inOrder.verify(mockedStatementCache).get(key1.capture());
    inOrder.verify(mockedStatementCache).putIfAbsent(same(key1.getValue()), val1.capture());
    inOrder.verify(mockedStatementCache).get(key2.capture());
    assertEquals(1, mockedStatementCache.size());
    assertTrue(mockedStatementCache.containsKey(key1.getValue()));
    assertEquals(key1.getValue(), key2.getValue());
    assertEquals(AVAILABLE, val1.getValue().state().get());
}
Also used : InOrder(org.mockito.InOrder) StatementMethod(org.vibur.dbcp.stcache.StatementMethod) ViburDBCPDataSource(org.vibur.dbcp.ViburDBCPDataSource) StatementHolder(org.vibur.dbcp.stcache.StatementHolder) ViburDBCPConnectionProvider(org.hibernate.vibur.internal.ViburDBCPConnectionProvider) ViburDBCPConnectionProvider(org.hibernate.vibur.internal.ViburDBCPConnectionProvider) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) Test(org.junit.Test)

Example 4 with StatementHolder

use of org.vibur.dbcp.stcache.StatementHolder in project vibur-dbcp by vibur.

the class ViburDBCPDataSourceTest method testSelectStatementWithStatementsCache.

@Test
public void testSelectStatementWithStatementsCache() throws SQLException {
    ViburDBCPDataSource ds = createDataSourceWithStatementsCache();
    ConcurrentMap<StatementMethod, StatementHolder> mockedStatementCache = mockStatementCache(ds);
    try (Connection connection = ds.getConnection()) {
        executeAndVerifySelectStatement(connection);
        executeAndVerifySelectStatement(connection);
        verifyZeroInteractions(mockedStatementCache);
    }
}
Also used : StatementMethod(org.vibur.dbcp.stcache.StatementMethod) TakenConnection(org.vibur.dbcp.pool.TakenConnection) StatementHolder(org.vibur.dbcp.stcache.StatementHolder) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)4 StatementHolder (org.vibur.dbcp.stcache.StatementHolder)4 StatementMethod (org.vibur.dbcp.stcache.StatementMethod)4 InOrder (org.mockito.InOrder)3 TakenConnection (org.vibur.dbcp.pool.TakenConnection)3 ConnectionProvider (org.hibernate.engine.jdbc.connections.spi.ConnectionProvider)1 ViburDBCPConnectionProvider (org.hibernate.vibur.internal.ViburDBCPConnectionProvider)1 ViburDBCPDataSource (org.vibur.dbcp.ViburDBCPDataSource)1