Hibernate.orgCommunity Documentation

Chapter 7. Services

Table of Contents

7.1. What are services?
7.2. Service contracts
7.3. Service dependencies
7.3.1. @org.hibernate.service.spi.InjectService
7.3.2. org.hibernate.service.spi.ServiceRegistryAwareService
7.4. ServiceRegistry
7.5. Standard services
7.5.1. org.hibernate.engine.jdbc.batch.spi.BatchBuilder
7.5.2. org.hibernate.engine.config.spi.ConfigurationService
7.5.3. ConnectionProvider
7.5.4. org.hibernate.engine.jdbc.dialect.spi.DialectFactory
7.5.5. org.hibernate.engine.jdbc.dialect.spi.DialectResolver
7.5.6. org.hibernate.engine.jdbc.spi.JdbcServices
7.5.7. org.hibernate.jmx.spi.JmxService
7.5.8. org.hibernate.engine.jndi.spi.JndiService
7.5.9. org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform
7.5.10. MultiTenantConnectionProvider
7.5.11. org.hibernate.persister.spi.PersisterClassResolver
7.5.12. org.hibernate.persister.spi.PersisterFactory
7.5.13. org.hibernate.cache.spi.RegionFactory
7.5.14. org.hibernate.service.spi.SessionFactoryServiceRegistryFactory
7.5.15. org.hibernate.stat.Statistics
7.5.16. org.hibernate.engine.transaction.spi.TransactionFactory
7.5.17. org.hibernate.tool.hbm2ddl.ImportSqlCommandExtractor
7.6. Custom services
7.7. Special service registries
7.7.1. Boot-strap registry
7.7.2. SessionFactory registry
7.8. Using services and registries
7.9. Integrators
7.9.1. Integrator use-cases

Services are classes that provide Hibernate with pluggable implementations of various types of functionality. Specifically they are implementations of certain service contract interfaces. The interface is known as the service role; the implementation class is know as the service implementation. Generally speaking, users can plug in alternate implementations of all standard service roles (overriding); they can also define additional services beyond the base set of service roles (extending).

The basic requirement for a service is to implement the marker interface org.hibernate.service.Service. Hibernate uses this internally for some basic type safety.

Optionally, the service can also implement the org.hibernate.service.spi.Startable and org.hibernate.service.spi.Stoppable interfaces to receive notifications of being started and stopped. Another optional service contract is org.hibernate.service.spi.Manageable which marks the service as manageable in JMX provided the JMX integration is enabled.

Services are allowed to declare dependencies on other services using either of 2 approaches.

The central service API, aside from the services themselves, is the org.hibernate.service.ServiceRegistry interface. The main purpose of a service registry is to hold, manage and provide access to services.

Service registries are hierarchical. Services in one registry can depend on and utilize services in that same registry as well as any parent registries.

Use org.hibernate.boot.registry.StandardServiceRegistryBuilder to build a org.hibernate.service.ServiceRegistry instance.

Notes

Provides an abstraction from the underlying JTA platform when JTA features are used.

Initiator

org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformInitiator

Important

As of 5.0 support has been completely removed for mapping against legacy org.hibernate.transaction.TransactionManagerLookup names and custom implementations. Applications implementing org.hibernate.transaction.TransactionManagerLookup or using the hibernate.transaction.manager_lookup_class setting should update to use JtaPlatform.

Implementations
  • org.hibernate.engine.transaction.jta.platform.internal.BitronixJtaPlatform - Integration with the Bitronix stand-alone transaction manager. Can also be referenced using the Bitronix configuration short name

  • org.hibernate.engine.transaction.jta.platform.internal.BorlandEnterpriseServerJtaPlatform - Integration with the transaction manager as deployed within a Borland Enterprise Server. Can also be referenced using the Borland configuration short name

  • org.hibernate.engine.transaction.jta.platform.internal.JBossAppServerJtaPlatform - Integration with the transaction manager as deployed within a JBoss Application Server. Can also be referenced using the JBossAS configuration short name

  • org.hibernate.engine.transaction.jta.platform.internal.JBossStandAloneJtaPlatform - Integration with the JBoss Transactions stand-alone transaction manager. Can also be referenced using the JBossTS configuration short name

  • org.hibernate.engine.transaction.jta.platform.internal.JOTMJtaPlatform - Integration with the JOTM stand-alone transaction manager. Can also be referenced using the JOTM configuration short name

  • org.hibernate.engine.transaction.jta.platform.internal.JOnASJtaPlatform - Integration with the JOnAS transaction manager. Can also be referenced using the JOnAS configuration short name

  • org.hibernate.engine.transaction.jta.platform.internal.JRun4JtaPlatform - Integration with the transaction manager as deployed in a JRun 4 application server. Can also be referenced using the JRun4 configuration short name

  • org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform - No-op version when no JTA set up is configured

  • org.hibernate.engine.transaction.jta.platform.internal.OC4JJtaPlatform - Integration with transaction manager as deployed in an OC4J (Oracle) application Can also be referenced using the OC4J configuration short name server.

  • org.hibernate.engine.transaction.jta.platform.internal.OrionJtaPlatform - Integration with transaction manager as deployed in an Orion application server. Can also be referenced using the Orion configuration short name

  • org.hibernate.engine.transaction.jta.platform.internal.ResinJtaPlatform - Integration with transaction manager as deployed in a Resin application server. Can also be referenced using the Resin configuration short name

  • org.hibernate.engine.transaction.jta.platform.internal.SunOneJtaPlatform - Integration with transaction manager as deployed in a Sun ONE (7 and above) application server. Can also be referenced using the SunOne configuration short name

  • org.hibernate.engine.transaction.jta.platform.internal.WebSphereExtendedJtaPlatform - Integration with transaction manager as deployed in a WebSphere Application Server (6 and above). Can also be referenced using the WebSphereExtended configuration short name

  • org.hibernate.engine.transaction.jta.platform.internal.WebSphereJtaPlatform - Integration with transaction manager as deployed in a WebSphere Application Server (4, 5.0 and 5.1). Can also be referenced using the WebSphere configuration short name

  • org.hibernate.engine.transaction.jta.platform.internal.WeblogicJtaPlatform - Integration with transaction manager as deployed in a Weblogic application server. Can also be referenced using the Weblogic configuration short name

Notes

A variation of Section 7.5.3, “ConnectionProvider providing access to JDBC connections in multi-tenant environments.

Initiator

N/A

Implementations

Intended that users provide appropriate implementation if needed.

Notes

Strategy defining how Hibernate's org.hibernate.Transaction API maps to the underlying transaction approach.

Initiator

org.hibernate.engine.transaction.internal.TransactionFactoryInitiator

Defines a hibernate.transaction.factory_class setting to allow configuring which TransactionFactory to use. hibernate.transaction.factory_class follows the rules set forth under Section A.1, “Strategy configurations”.

Implementations
  • org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory - A non-JTA strategy in which the transactions are managed using the JDBC java.sql.Connection. This implementation's short name is jdbc.

  • org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory - A JTA-based strategy in which Hibernate is not controlling the transactions. An important distinction here is that interaction with the underlying JTA implementation is done through the javax.transaction.TransactionManager. This implementation's short name is cmt.

  • org.hibernate.engine.transaction.internal.jta.JtaTransactionFactory - A JTA-based strategy in which Hibernate *may* be controlling the transactions. An important distinction here is that interaction with the underlying JTA implementation is done through the javax.transaction.UserTransaction. This implementation's short name is jta.

Once a org.hibernate.service.ServiceRegistry is built it is considered immutable; the services themselves might accept re-configuration, but immutability here means adding/replacing services. So another role provided by the org.hibernate.boot.registry.StandardServiceRegistryBuilder is to allow tweaking of the services that will be contained in the org.hibernate.service.ServiceRegistry generated from it.

There are 2 means to tell a org.hibernate.boot.registry.StandardServiceRegistryBuilder about custom services.

  • Implement a org.hibernate.boot.registry.StandardServiceInitiator class to control on-demand construction of the service class and add it to the org.hibernate.boot.registry.StandardServiceRegistryBuilder via its addInitiator method.

  • Just instantiate the service class and add it to the org.hibernate.boot.registry.StandardServiceRegistryBuilder via its addService method.

Either approach the adding a service approach or the adding an initiator approach are valid for extending a registry (adding new service roles) and overriding services (replacing service implementations).

The boot-strap registry holds services that absolutely have to be available for most things to work. The main service here is the Section 7.7.1.1.1, “org.hibernate.boot.registry.classloading.spi.ClassLoaderService which is a perfect example. Even resolving configuration files needs access to class loading services (resource look ups). This is the root registry (no parent) in normal use.

Instances of boot-strap registries are built using the org.hibernate.boot.registry.BootstrapServiceRegistryBuilder class.


Coming soon...

The org.hibernate.integrator.spi.Integrator is intended to provide a simple means for allowing developers to hook into the process of building a functioning SessionFactory. The The org.hibernate.integrator.spi.Integrator interface defines 2 methods of interest: integrate allows us to hook into the building process; disintegrate allows us to hook into a SessionFactory shutting down.

Note

There is a 3rd method defined on org.hibernate.integrator.spi.Integrator, an overloaded form of integrate accepting a org.hibernate.metamodel.source.MetadataImplementor instead of org.hibernate.cfg.Configuration. This form is intended for use with the new metamodel code scheduled for completion in 5.0

See Section 7.7.1.1.2, “org.hibernate.integrator.spi.IntegratorService

In addition to the discovery approach provided by the IntegratorService, applications can manually register Integrator implementations when building the BootstrapServiceRegistry. See Example 7.1, “Using BootstrapServiceRegistryBuilder”

The main use cases for an org.hibernate.integrator.spi.Integrator right now are registering event listeners and providing services (see org.hibernate.integrator.spi.ServiceContributingIntegrator). With 5.0 we plan on expanding that to allow altering the metamodel describing the mapping between object and relational models.