Search in sources :

Example 66 with StandardServiceRegistry

use of org.hibernate.boot.registry.StandardServiceRegistry in project hibernate-orm by hibernate.

the class SingleRegisteredProviderTest method testCachingImplicitlyEnabledNoRegistered.

@Test
public void testCachingImplicitlyEnabledNoRegistered() {
    final BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder().build();
    final Collection<Class<? extends RegionFactory>> implementors = bsr.getService(StrategySelector.class).getRegisteredStrategyImplementors(RegionFactory.class);
    assertThat(implementors.size(), equalTo(1));
    bsr.getService(StrategySelector.class).unRegisterStrategyImplementor(RegionFactory.class, implementors.iterator().next());
    final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder(bsr).applySetting(AvailableSettings.USE_SECOND_LEVEL_CACHE, "").build();
    assertThat(ssr.getService(RegionFactory.class), instanceOf(NoCachingRegionFactory.class));
}
Also used : BootstrapServiceRegistryBuilder(org.hibernate.boot.registry.BootstrapServiceRegistryBuilder) RegionFactory(org.hibernate.cache.spi.RegionFactory) NoCachingRegionFactory(org.hibernate.cache.internal.NoCachingRegionFactory) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) NoCachingRegionFactory(org.hibernate.cache.internal.NoCachingRegionFactory) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) StrategySelector(org.hibernate.boot.registry.selector.spi.StrategySelector) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 67 with StandardServiceRegistry

use of org.hibernate.boot.registry.StandardServiceRegistry in project hibernate-orm by hibernate.

the class SingleRegisteredProviderTest method testCachingImplicitlyEnabledRegistered.

@Test
public void testCachingImplicitlyEnabledRegistered() {
    final BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder().build();
    final Collection<Class<? extends RegionFactory>> implementors = bsr.getService(StrategySelector.class).getRegisteredStrategyImplementors(RegionFactory.class);
    assertThat(implementors.size(), equalTo(1));
    final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder(bsr).applySetting(AvailableSettings.USE_SECOND_LEVEL_CACHE, "").build();
    assertThat(ssr.getService(RegionFactory.class), instanceOf(NoCachingRegionFactory.class));
}
Also used : BootstrapServiceRegistryBuilder(org.hibernate.boot.registry.BootstrapServiceRegistryBuilder) RegionFactory(org.hibernate.cache.spi.RegionFactory) NoCachingRegionFactory(org.hibernate.cache.internal.NoCachingRegionFactory) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) NoCachingRegionFactory(org.hibernate.cache.internal.NoCachingRegionFactory) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) StrategySelector(org.hibernate.boot.registry.selector.spi.StrategySelector) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 68 with StandardServiceRegistry

use of org.hibernate.boot.registry.StandardServiceRegistry in project hibernate-orm by hibernate.

the class SingleRegisteredProviderTest method testCachingExplicitlyDisabled.

@Test
public void testCachingExplicitlyDisabled() {
    final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.USE_SECOND_LEVEL_CACHE, "false").build();
    assertThat(registry.getService(RegionFactory.class), instanceOf(NoCachingRegionFactory.class));
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) RegionFactory(org.hibernate.cache.spi.RegionFactory) NoCachingRegionFactory(org.hibernate.cache.internal.NoCachingRegionFactory) NoCachingRegionFactory(org.hibernate.cache.internal.NoCachingRegionFactory) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 69 with StandardServiceRegistry

use of org.hibernate.boot.registry.StandardServiceRegistry in project hibernate-orm by hibernate.

the class BasicCdiTest method testIt.

@Test
@SuppressWarnings("unchecked")
public void testIt() {
    final SeContainerInitializer cdiInitializer = SeContainerInitializer.newInstance().disableDiscovery().addBeanClasses(Monitor.class, EventQueue.class, Event.class);
    count = 0;
    try (final SeContainer cdiContainer = cdiInitializer.initialize()) {
        BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder().build();
        final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder(bsr).applySetting(AvailableSettings.CDI_BEAN_MANAGER, cdiContainer.getBeanManager()).applySetting(AvailableSettings.DELAY_CDI_ACCESS, "true").applySetting(AvailableSettings.HBM2DDL_AUTO, Action.CREATE_DROP).build();
        final SessionFactoryImplementor sessionFactory;
        try {
            sessionFactory = (SessionFactoryImplementor) new MetadataSources(ssr).addAnnotatedClass(MyEntity.class).buildMetadata().getSessionFactoryBuilder().build();
        } catch (Exception e) {
            StandardServiceRegistryBuilder.destroy(ssr);
            throw e;
        }
        try {
            inTransaction(sessionFactory, session -> session.persist(new MyEntity(1)));
            assertEquals(1, count);
            inTransaction(sessionFactory, session -> {
                MyEntity it = session.find(MyEntity.class, 1);
                assertNotNull(it);
            });
        } finally {
            inTransaction(sessionFactory, session -> {
                session.createQuery("delete MyEntity").executeUpdate();
            });
            sessionFactory.close();
        }
    }
}
Also used : BootstrapServiceRegistryBuilder(org.hibernate.boot.registry.BootstrapServiceRegistryBuilder) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) MetadataSources(org.hibernate.boot.MetadataSources) SeContainer(javax.enterprise.inject.se.SeContainer) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) SeContainerInitializer(javax.enterprise.inject.se.SeContainerInitializer) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 70 with StandardServiceRegistry

use of org.hibernate.boot.registry.StandardServiceRegistry in project hibernate-orm by hibernate.

the class TransactionsTest method jdbc.

@Test
public void jdbc() {
    // tag::transactions-api-jdbc-example[]
    StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.TRANSACTION_COORDINATOR_STRATEGY, "jdbc").build();
    Metadata metadata = new MetadataSources(serviceRegistry).addAnnotatedClass(Customer.class).getMetadataBuilder().build();
    SessionFactory sessionFactory = metadata.getSessionFactoryBuilder().build();
    Session session = sessionFactory.openSession();
    try {
        // calls Connection#setAutoCommit( false ) to
        // signal start of transaction
        session.getTransaction().begin();
        session.createQuery("UPDATE customer set NAME = 'Sir. '||NAME").executeUpdate();
        // calls Connection#commit(), if an error
        // happens we attempt a rollback
        session.getTransaction().commit();
    } catch (Exception e) {
        // where the exception happened
        if (session.getTransaction().getStatus() == TransactionStatus.ACTIVE || session.getTransaction().getStatus() == TransactionStatus.MARKED_ROLLBACK) {
            session.getTransaction().rollback();
        }
    // handle the underlying error
    } finally {
        session.close();
        sessionFactory.close();
    }
// end::transactions-api-jdbc-example[]
}
Also used : SessionFactory(org.hibernate.SessionFactory) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)169 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)152 Test (org.junit.Test)121 MetadataSources (org.hibernate.boot.MetadataSources)118 Metadata (org.hibernate.boot.Metadata)56 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)39 PersistentClass (org.hibernate.mapping.PersistentClass)39 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)25 TestForIssue (org.hibernate.testing.TestForIssue)24 BootstrapServiceRegistryBuilder (org.hibernate.boot.registry.BootstrapServiceRegistryBuilder)23 Properties (java.util.Properties)20 BootstrapServiceRegistry (org.hibernate.boot.registry.BootstrapServiceRegistry)20 SchemaUpdate (org.hibernate.tool.hbm2ddl.SchemaUpdate)15 SessionFactory (org.hibernate.SessionFactory)14 Column (org.hibernate.mapping.Column)14 Property (org.hibernate.mapping.Property)14 Session (org.hibernate.Session)13 SchemaExport (org.hibernate.tool.hbm2ddl.SchemaExport)12 IdentifierGenerator (org.hibernate.id.IdentifierGenerator)11 RootClass (org.hibernate.mapping.RootClass)11