While I am using the following recommended way in Hibernate 4.3 to create Session Factory,
Configuration configuration = new Configuration().configure(CONFIGURATION_LOCATION);
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
I tried to find how the above code is working by reading Hibernate docs, The Configuration object is used to specify configuration properties which we specify in hibernate.cfg.xml. Session Factory is single for an application(in case application has to connect one database) and used to create sessions.
But I could not understand what this service registry is doing in creation of session factory and why from hibernate 4.3, Standard Service Registry Builder is used? What is the actual use of this class and why it is used to create Session Factory ? Which services are being talked about in Service Registry ?
The ServiceRegistry
allows Hibernate to initialize Services such as:
The role of the ServiceRegistry
is to provide a flexible way of bootstrapping Hibernate services. Some applications use a programmatic-style of creating a SessionFactory, while other environments bootstrap services through an external mechanism, like OSGI.