use of org.hibernate.engine.jdbc.spi.JdbcServices in project hibernate-orm by hibernate.
the class ServiceBootstrappingTest method testBuildWithLogging.
@Test
public void testBuildWithLogging() {
Properties props = ConnectionProviderBuilder.getConnectionProviderProperties();
props.put(Environment.SHOW_SQL, "true");
StandardServiceRegistryImpl serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().applySettings(props).build();
try {
JdbcServices jdbcServices = serviceRegistry.getService(JdbcServices.class);
assertTrue(jdbcServices.getDialect() instanceof H2Dialect);
final ConnectionProviderJdbcConnectionAccess connectionAccess = assertTyping(ConnectionProviderJdbcConnectionAccess.class, jdbcServices.getBootstrapJdbcConnectionAccess());
assertTrue(connectionAccess.getConnectionProvider().isUnwrappableAs(DriverManagerConnectionProviderImpl.class));
assertTrue(jdbcServices.getSqlStatementLogger().isLogToStdout());
} finally {
serviceRegistry.destroy();
}
}
use of org.hibernate.engine.jdbc.spi.JdbcServices in project hibernate-orm by hibernate.
the class ServiceBootstrappingTest method testBasicBuild.
@Test
public void testBasicBuild() {
// this test requires that SHOW_SQL property isn't passed from the outside (eg. via Gradle)
final String showSqlPropertyFromOutside = System.getProperty(Environment.SHOW_SQL);
Assume.assumeFalse("true".equals(showSqlPropertyFromOutside));
final StandardServiceRegistryImpl serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().applySettings(ConnectionProviderBuilder.getConnectionProviderProperties()).build();
try {
final JdbcServices jdbcServices = serviceRegistry.getService(JdbcServices.class);
assertTrue(jdbcServices.getDialect() instanceof H2Dialect);
final ConnectionProviderJdbcConnectionAccess connectionAccess = assertTyping(ConnectionProviderJdbcConnectionAccess.class, jdbcServices.getBootstrapJdbcConnectionAccess());
assertTrue(connectionAccess.getConnectionProvider().isUnwrappableAs(DriverManagerConnectionProviderImpl.class));
assertFalse(jdbcServices.getSqlStatementLogger().isLogToStdout());
} finally {
serviceRegistry.destroy();
}
}
use of org.hibernate.engine.jdbc.spi.JdbcServices in project hibernate-orm by hibernate.
the class AbstractRegionAccessStrategyTest method mockedSession.
protected SharedSessionContractImplementor mockedSession() {
SessionMock session = mock(SessionMock.class);
when(session.isClosed()).thenReturn(false);
when(session.getTimestamp()).thenReturn(TIME_SERVICE.wallClockTime());
if (jtaPlatform == BatchModeJtaPlatform.class) {
BatchModeTransactionCoordinator txCoord = new BatchModeTransactionCoordinator();
when(session.getTransactionCoordinator()).thenReturn(txCoord);
when(session.beginTransaction()).then(invocation -> {
Transaction tx = txCoord.newTransaction();
tx.begin();
return tx;
});
} else if (jtaPlatform == null) {
Connection connection = mock(Connection.class);
JdbcConnectionAccess jdbcConnectionAccess = mock(JdbcConnectionAccess.class);
try {
when(jdbcConnectionAccess.obtainConnection()).thenReturn(connection);
} catch (SQLException e) {
// never thrown from mock
}
JdbcSessionOwner jdbcSessionOwner = mock(JdbcSessionOwner.class);
when(jdbcSessionOwner.getJdbcConnectionAccess()).thenReturn(jdbcConnectionAccess);
SqlExceptionHelper sqlExceptionHelper = mock(SqlExceptionHelper.class);
JdbcServices jdbcServices = mock(JdbcServices.class);
when(jdbcServices.getSqlExceptionHelper()).thenReturn(sqlExceptionHelper);
ServiceRegistry serviceRegistry = mock(ServiceRegistry.class);
when(serviceRegistry.getService(JdbcServices.class)).thenReturn(jdbcServices);
JdbcSessionContext jdbcSessionContext = mock(JdbcSessionContext.class);
when(jdbcSessionContext.getServiceRegistry()).thenReturn(serviceRegistry);
when(jdbcSessionOwner.getJdbcSessionContext()).thenReturn(jdbcSessionContext);
NonJtaTransactionCoordinator txOwner = mock(NonJtaTransactionCoordinator.class);
when(txOwner.getResourceLocalTransaction()).thenReturn(new JdbcResourceTransactionMock());
when(txOwner.getJdbcSessionOwner()).thenReturn(jdbcSessionOwner);
when(txOwner.isActive()).thenReturn(true);
TransactionCoordinator txCoord = JdbcResourceLocalTransactionCoordinatorBuilderImpl.INSTANCE.buildTransactionCoordinator(txOwner, null);
when(session.getTransactionCoordinator()).thenReturn(txCoord);
when(session.beginTransaction()).then(invocation -> {
Transaction tx = new TransactionImpl(txCoord, session.getExceptionConverter());
tx.begin();
return tx;
});
} else {
throw new IllegalStateException("Unknown JtaPlatform: " + jtaPlatform);
}
return session;
}
use of org.hibernate.engine.jdbc.spi.JdbcServices 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"));
}
}
use of org.hibernate.engine.jdbc.spi.JdbcServices in project hibernate-orm by hibernate.
the class JdbcCoordinatorTest method testConnectionClose.
@Test
public void testConnectionClose() throws NoSuchFieldException, IllegalAccessException, SQLException {
Connection connection = Mockito.mock(Connection.class);
JdbcSessionOwner sessionOwner = Mockito.mock(JdbcSessionOwner.class);
JdbcConnectionAccess jdbcConnectionAccess = Mockito.mock(JdbcConnectionAccess.class);
when(jdbcConnectionAccess.obtainConnection()).thenReturn(connection);
when(jdbcConnectionAccess.supportsAggressiveRelease()).thenReturn(false);
JdbcSessionContext sessionContext = Mockito.mock(JdbcSessionContext.class);
when(sessionOwner.getJdbcSessionContext()).thenReturn(sessionContext);
when(sessionOwner.getJdbcConnectionAccess()).thenReturn(jdbcConnectionAccess);
ServiceRegistry serviceRegistry = Mockito.mock(ServiceRegistry.class);
when(sessionContext.getServiceRegistry()).thenReturn(serviceRegistry);
when(sessionContext.getPhysicalConnectionHandlingMode()).thenReturn(PhysicalConnectionHandlingMode.IMMEDIATE_ACQUISITION_AND_HOLD);
JdbcObserver jdbcObserver = Mockito.mock(JdbcObserver.class);
when(sessionContext.getObserver()).thenReturn(jdbcObserver);
JdbcServices jdbcServices = Mockito.mock(JdbcServices.class);
when(serviceRegistry.getService(eq(JdbcServices.class))).thenReturn(jdbcServices);
SqlExceptionHelper sqlExceptionHelper = Mockito.mock(SqlExceptionHelper.class);
when(jdbcServices.getSqlExceptionHelper()).thenReturn(sqlExceptionHelper);
JdbcCoordinatorImpl jdbcCoordinator = new JdbcCoordinatorImpl(null, sessionOwner);
Batch currentBatch = Mockito.mock(Batch.class);
Field currentBatchField = JdbcCoordinatorImpl.class.getDeclaredField("currentBatch");
currentBatchField.setAccessible(true);
currentBatchField.set(jdbcCoordinator, currentBatch);
doThrow(IllegalStateException.class).when(currentBatch).release();
try {
jdbcCoordinator.close();
fail("Should throw IllegalStateException");
} catch (Exception expected) {
assertEquals(IllegalStateException.class, expected.getClass());
}
verify(jdbcConnectionAccess, times(1)).releaseConnection(same(connection));
}
Aggregations