use of org.hibernate.service.ServiceRegistry in project dropwizard by dropwizard.
the class SessionFactoryFactory method buildSessionFactory.
private SessionFactory buildSessionFactory(HibernateBundle<?> bundle, PooledDataSourceFactory dbConfig, ConnectionProvider connectionProvider, Map<String, String> properties, List<Class<?>> entities) {
final Configuration configuration = new Configuration();
configuration.setProperty(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS, "managed");
configuration.setProperty(AvailableSettings.USE_SQL_COMMENTS, Boolean.toString(dbConfig.isAutoCommentsEnabled()));
configuration.setProperty(AvailableSettings.USE_GET_GENERATED_KEYS, "true");
configuration.setProperty(AvailableSettings.GENERATE_STATISTICS, "true");
configuration.setProperty(AvailableSettings.USE_REFLECTION_OPTIMIZER, "true");
configuration.setProperty(AvailableSettings.ORDER_UPDATES, "true");
configuration.setProperty(AvailableSettings.ORDER_INSERTS, "true");
configuration.setProperty(AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS, "true");
configuration.setProperty("jadira.usertype.autoRegisterUserTypes", "true");
for (Map.Entry<String, String> property : properties.entrySet()) {
configuration.setProperty(property.getKey(), property.getValue());
}
addAnnotatedClasses(configuration, entities);
bundle.configure(configuration);
final ServiceRegistry registry = new StandardServiceRegistryBuilder().addService(ConnectionProvider.class, connectionProvider).applySettings(configuration.getProperties()).build();
configure(configuration, registry);
return configuration.buildSessionFactory(registry);
}
use of org.hibernate.service.ServiceRegistry 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.service.ServiceRegistry in project hibernate-orm by hibernate.
the class ClassLoaderServiceImplTest method testStoppableClassLoaderService.
/**
* HHH-8363 discovered multiple leaks within CLS. Most notably, it wasn't getting GC'd due to holding
* references to ServiceLoaders. Ensure that the addition of Stoppable functionality cleans up properly.
*
* TODO: Is there a way to test that the ServiceLoader was actually reset?
*/
@Test
@TestForIssue(jiraKey = "HHH-8363")
public void testStoppableClassLoaderService() {
final BootstrapServiceRegistryBuilder bootstrapBuilder = new BootstrapServiceRegistryBuilder();
bootstrapBuilder.applyClassLoader(new TestClassLoader());
final ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder(bootstrapBuilder.build()).build();
final ClassLoaderService classLoaderService = serviceRegistry.getService(ClassLoaderService.class);
TestIntegrator testIntegrator1 = findTestIntegrator(classLoaderService);
assertNotNull(testIntegrator1);
TestIntegrator testIntegrator2 = findTestIntegrator(classLoaderService);
assertNotNull(testIntegrator2);
assertSame(testIntegrator1, testIntegrator2);
StandardServiceRegistryBuilder.destroy(serviceRegistry);
try {
findTestIntegrator(classLoaderService);
Assert.fail("Should have thrown an HibernateException -- the ClassLoaderService instance was closed.");
} catch (HibernateException e) {
String message = e.getMessage();
Assert.assertEquals("HHH000469: The ClassLoaderService can not be reused. This instance was stopped already.", message);
}
}
use of org.hibernate.service.ServiceRegistry in project hibernate-orm by hibernate.
the class AuditedDynamicComponentTest method testAuditedDynamicComponentFailure.
//@Test
public void testAuditedDynamicComponentFailure() throws URISyntaxException {
final Configuration config = new Configuration();
final URL hbm = Thread.currentThread().getContextClassLoader().getResource("mappings/dynamicComponents/mapAudited.hbm.xml");
config.addFile(new File(hbm.toURI()));
final String auditStrategy = getAuditStrategy();
if (!StringTools.isEmpty(auditStrategy)) {
config.setProperty(EnversSettings.AUDIT_STRATEGY, auditStrategy);
}
final ServiceRegistry serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry(config.getProperties());
try {
config.buildSessionFactory(serviceRegistry);
Assert.fail("MappingException expected");
} catch (MappingException e) {
Assert.assertEquals("Audited dynamic-component properties are not supported. Consider applying @NotAudited annotation to " + AuditedDynamicComponentEntity.class.getName() + "#customFields.", e.getMessage());
} finally {
ServiceRegistryBuilder.destroy(serviceRegistry);
}
}
use of org.hibernate.service.ServiceRegistry in project hibernate-orm by hibernate.
the class SchemaCreateHelper method toWriter.
public static void toWriter(Metadata metadata, Writer writer) {
final ServiceRegistry serviceRegistry = ((MetadataImplementor) metadata).getMetadataBuildingOptions().getServiceRegistry();
final Map settings = serviceRegistry.getService(ConfigurationService.class).getSettings();
settings.put(AvailableSettings.HBM2DDL_SCRIPTS_ACTION, Action.CREATE);
settings.put(AvailableSettings.HBM2DDL_SCRIPTS_CREATE_TARGET, writer);
SchemaManagementToolCoordinator.process(metadata, serviceRegistry, settings, DelayedDropRegistryNotAvailableImpl.INSTANCE);
}
Aggregations