Class ServiceSessionControl

java.lang.Object
net.sf.basedb.core.SessionControl
net.sf.basedb.core.ServiceSessionControl
All Implemented Interfaces:
AutoCloseable

public class ServiceSessionControl
extends SessionControl
A special session control implementation used for services. It provides extra functionality that an extension can use to create database objects in a separate database schema/catalog for their own use. Note that the extra API in this class depends on Hibernate API and may change without notification in case the Hibernate API is changed (though we will try to give advanced notice).
Since:
3.4
Author:
nicklas
  • Field Details

    • log

      private static final org.slf4j.Logger log
  • Constructor Details

    • ServiceSessionControl

      ServiceSessionControl()
  • Method Details

    • createSessionFactoryConfiguration

      public ServiceSessionControl.SessionFactoryConfiguration createSessionFactoryConfiguration​(String appId, String schema, int schemaVersion, ClassLoader classLoader)
      Create an object that can be used for collecting configuration options and other settings that are needed to create session factories for services.

      An application id, schema and schema version is required. This information is used by BASE to check the currently installed schema version and only update the database if there is a change. Note that the schema version must always increase from one version to the next.

      A class loader is needed so that Hibernate can load the classes from extension classes.

      It is possible to use ServiceSessionControl.SessionFactoryConfiguration.getInstallMode() and ServiceSessionControl.SessionFactoryConfiguration.getInstalledSchemaVersion() to check the current status of the database.

      Use ServiceSessionControl.SessionFactoryConfiguration.getRegistryBuilder() to get a Hibernate object for setting things that are typically configured in hibernate.cfg.xml. Typically, StandardServiceRegistryBuilder.applySetting(String, Object) is the main method to use for this.

      Use ServiceSessionControl.SessionFactoryConfiguration.getMetadataSources() to get a Hibernate object for adding mapped entity classes. Typically, MetadataSources.addAnnotatedClass(Class) is the main method to use for this. Note that once the metadata sources object has been created it is not possible to modify the registry settings.

      Also note that by default the configured session factory need to be "piggy-backed" on a BASE DbControl transaction. Create Hibernate sessions with newSession(org.hibernate.SessionBuilder, DbControl).

      If you want to use connections/transactions that are independent from BASE call ServiceSessionControl.SessionFactoryConfiguration.setUseSeparateConnections(boolean) but make sure you also get connection pooling configured. You can still use "piggy-backed" connections.

      Parameters:
      appId - The ID of the extensions (which should be unique)
      schema - The schema name the extension wants to use for it's database tables
      schemaVersion - The version of the current schema. If this is different from the database scheme it will automatically be updated when buildSessionFactory(SessionFactoryConfiguration) is called.
      classLoader - A class loader is needed for Hibernate to be able to locate the classes. Use the classloader returned from Class.getClassLoader() using the class from one of the classes in the extension
    • getCatalog

      String getCatalog()
    • schemaExists

      boolean schemaExists​(String catalog, String schema)
    • buildSessionFactory

      public SessionFactory buildSessionFactory​(ServiceSessionControl.SessionFactoryConfiguration config)
      Create a session factory for the given configuration. This method will add connection information from the base.config file automatically to the configuration object. Note that sessions can't Classes that have been defined in the configuration should be without a schema definition and will then be put into the specified schema automatically. If the given schema and tables doesn't exists they will be created automatically, otherwise an UPDATE action is performed to modify an existing installation.
      Parameters:
      config - A configuration instance containing information needed to build the session factory
      Returns:
      A session factory instance
    • newSession

      public Session newSession​(SessionBuilder<?> builder, DbControl dc)
      Create a new Hibernate session for the given session factory that is piggy-backed on the given DbControl transaction. Note that the life-cycle of the returned session is managed by the DbControl. There is no need to flush or close the session since this is done automatically and may cause exceptions if done when not needed.
      Parameters:
      builder - A session builder (eg. SessionFactory.withOptions())
      dc - An open DbControl