Search in sources :

Example 1 with AgroalConnectionProvider

use of org.hibernate.agroal.internal.AgroalConnectionProvider in project hibernate-orm by hibernate.

the class AgroalConnectionProviderTest method testAgroalConnectionProvider.

@Test
public void testAgroalConnectionProvider() throws Exception {
    JdbcServices jdbcServices = serviceRegistry().getService(JdbcServices.class);
    ConnectionProviderJdbcConnectionAccess connectionAccess = assertTyping(ConnectionProviderJdbcConnectionAccess.class, jdbcServices.getBootstrapJdbcConnectionAccess());
    assertTyping(AgroalConnectionProvider.class, connectionAccess.getConnectionProvider());
    AgroalConnectionProvider agroalConnectionProvider = (AgroalConnectionProvider) connectionAccess.getConnectionProvider();
    // For simplicity's sake, using the following in hibernate.properties:
    // hibernate.agroal.maxSize 2
    // hibernate.agroal.minSize 2
    List<Connection> conns = new ArrayList<>();
    for (int i = 0; i < 2; i++) {
        Connection conn = agroalConnectionProvider.getConnection();
        assertNotNull(conn);
        assertFalse(conn.isClosed());
        conns.add(conn);
    }
    try {
        agroalConnectionProvider.getConnection();
        fail("SQLException expected -- no more connections should have been available in the pool.");
    } catch (SQLException e) {
        // expected
        assertTrue(e.getMessage().contains("timeout"));
    }
    for (Connection conn : conns) {
        agroalConnectionProvider.closeConnection(conn);
        assertTrue(conn.isClosed());
    }
    releaseSessionFactory();
    try {
        agroalConnectionProvider.getConnection();
        fail("Exception expected -- the pool should have been shutdown.");
    } catch (Exception e) {
        // expected
        assertTrue(e.getMessage().contains("closed"));
    }
}
Also used : AgroalConnectionProvider(org.hibernate.agroal.internal.AgroalConnectionProvider) 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) Test(org.junit.Test)

Aggregations

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