Skip to content

Instantly share code, notes, and snippets.

@stliu
Created October 10, 2013 15:44
Show Gist options
  • Save stliu/6920562 to your computer and use it in GitHub Desktop.
Save stliu/6920562 to your computer and use it in GitHub Desktop.

Service Registry

People who has upgraded to Hibernate ORM 4.x may noticed that the well know way to build SessionFactory (see blow) now is deprecated.

Configuration configuration = new Configuration(); SessionFactory sf = configuration.buildSessionFactory();

The recommended way is

org.hibernate.cfg.Configuration#buildSessionFactory(ServiceRegistry serviceRegistry) 

Well, you can see now it requires a ServiceRegistry to build the SessionFactory.

So, what the hell this ServiceRegistry is?

By the name of this class, it is a registry for services, and it provides an uniform way to load / initialize / query services.

First of all, let's take a look the big picture of this ServiceRegistry

service registry

From the diagram we can tell service registries are hierarchical (see the parent reference), it is kind of lik the classloader's delegate pattern, the difference is classloader firstly delegate to its parent to do the class lookup and then itself if parent fails to find the class, but here in the service registry, it firstly try to look up the service from it self, then delegate to its parent, if any.

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

service registry hierarchy

The currently service registry hierarchy is built on 3 layers:

  • BootstrapServiceRegistry

    The boot-strap registry holds services that absolutely have to be available for most things to work. The main service here is the 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.

  • StandardServiceRegistry

    This is the standard service registry that designed to be shared across SessionFactory.

  • SessionFactoryServiceRegistry

    As what its name is saying, this service registry is SessionFactory scoped.

So, to summary it up, with this hierarchy and delegate design, it is totally transparent to the service registry client that which service is belong to which registry.

Service

Okay, we have talked about Service a lot, what are services exactly?

Quoting from Hibernate ORM Dev Guide:

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).

Service is not new to Hibernat actually, we have these codes already, but separared in everywhere ( w/ different API/SPI ), so, this time, we just extracted those codes and wrapped as service, which as said, gives us an uniform API to load / initialize / query them, also, the service registry design makes it very easy to plug in your own impl.

The picture below lists all services we currently have, for the details of each service, please take a look of our Dev Guide:

services

Building ServiceRegistry

As said, now the recommanded way to build a SessionFactory is passing a ServiceRegistry instance to the Configuration#buildSessionFactory, so firstly, we need to know how to build a ServiceRegistry instance.

StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();
ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
SessionFactory sf = configuration.buildSessionFactory(serviceRegistry);

Okay, this is the simplest case, it assumes you don't want to customize anything on the service regitry nor service.

Let's look it deeper and see what is happening internally.

You already know that internally we have 3 service registries and they are formed hierarchally, so, actually, w/ code example above, you explicitly created the StandardServiceRegistry and Hibenrate helped you created the other two(BootstrapServiceRegistry and SessionFactoryServiceRegistry).

BootstrapServiceRegistry

org.hibernate.service.ServiceRegistryBuilder has two constructors, one requires a BootstrapServiceRegistry instance and the other, which I showed above, doesn't.

You can just guess, the constructor doesn't need a BootstrapServiceRegistry instance just build it by itself, yes, you're right, ServiceRegistryBuilder would build a org.hibernate.boot.registry.internal.BootstrapServiceRegistryImpl if no one passed in.

So, why the hell of hibernate makes this so complicated?

Well, let's take a look of how to build BootstrapServiceRegistry first.

Hibernate already provides a builder org.hibernate.boot.registry.BootstrapServiceRegistryBuilder for you use to build the BootstrapServiceRegistry.

Besides the build method, it also provides methods below for extension:

  • with(Integrator integrator)
  • with(ClassLoader classLoader)
  • withStrategySelectors(AvailabilityAnnouncer availabilityAnnouncer)

(please wait for the next post with details of above concepts)

Again, service registry is designed as a hierarchy arch, and this BootstrapServiceRegistry is the root one, it has to be available for most of things to work.

SessionFactoryServiceRegistry

Okay, we have seen StandardServiceRegistryBuilder and BootstrapServiceRegistryBuilder, so it is reasonable to expect there is a SessionFactoryServiceRegistryBuilder, sadly, the answer is NO.

The reason is that WHEN this SessionFactoryServiceRegistry is controlled by the SessionFactory ifself, BUT, you can control HOW it will be built.

org.hibernate.service.spi.SessionFactoryServiceRegistryFactory is the one that creates SessionFactoryServiceRegistry, and it is a service in the StandardServiceRegistry, which means you can plug in your own impl of this service and control how it builds SessionFactoryServiceRegistryFactory.

Alright, but HOW to plug in my own impl?

Service

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment