Search in sources :

Example 1 with HikariCPConnectionProvider

use of org.hibernate.hikaricp.internal.HikariCPConnectionProvider in project hibernate-orm by hibernate.

the class HikariCPConnectionProviderTest method testHikariCPConnectionProvider.

@Test
public void testHikariCPConnectionProvider() throws Exception {
    JdbcServices jdbcServices = serviceRegistry().getService(JdbcServices.class);
    ConnectionProviderJdbcConnectionAccess connectionAccess = assertTyping(ConnectionProviderJdbcConnectionAccess.class, jdbcServices.getBootstrapJdbcConnectionAccess());
    assertTyping(HikariCPConnectionProvider.class, connectionAccess.getConnectionProvider());
    HikariCPConnectionProvider hikariCP = (HikariCPConnectionProvider) connectionAccess.getConnectionProvider();
    // For simplicity's sake, using the following in hibernate.properties:
    // hibernate.hikari.minimumPoolSize 2
    // hibernate.hikari.maximumPoolSize 2
    final List<Connection> conns = new ArrayList<Connection>();
    for (int i = 0; i < 2; i++) {
        Connection conn = hikariCP.getConnection();
        assertNotNull(conn);
        assertFalse(conn.isClosed());
        conns.add(conn);
    }
    try {
        hikariCP.getConnection();
        fail("SQLException expected -- no more connections should have been available in the pool.");
    } catch (SQLException e) {
        // expected
        assertTrue(e.getMessage().contains("Connection is not available, request timed out after"));
    }
    for (Connection conn : conns) {
        hikariCP.closeConnection(conn);
        assertTrue(conn.isClosed());
    }
    releaseSessionFactory();
    try {
        hikariCP.getConnection();
        fail("Exception expected -- the pool should have been shutdown.");
    } catch (Exception e) {
        // expected
        assertTrue(e.getMessage().contains("has been closed"));
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) SQLException(java.sql.SQLException) ConnectionProviderJdbcConnectionAccess(org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.ConnectionProviderJdbcConnectionAccess) HikariCPConnectionProvider(org.hibernate.hikaricp.internal.HikariCPConnectionProvider) Test(org.junit.Test)

Aggregations

Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 ConnectionProviderJdbcConnectionAccess (org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.ConnectionProviderJdbcConnectionAccess)1 JdbcServices (org.hibernate.engine.jdbc.spi.JdbcServices)1 HikariCPConnectionProvider (org.hibernate.hikaricp.internal.HikariCPConnectionProvider)1 Test (org.junit.Test)1