Hibernate Cartridge Second Level Cache

Hibernate Cartridge Second Level Cache

Hibernate uses two different caches for objects. The first level cache, and the second level cache.

The first level cache is associated with the Session object, while the second level cache is associated with the hibernate SessionFactory object.

Currently, the cartridge architecture creates one Session instance from the SessionFactory instance for each method that is called at the EJB's services. This means that caching will only occur if a method accesses the same instance more than once. This can be improved by activating the second level cache features provided by Hibernate.

To achieve this task, the cartridge includes support for generating the Hibernate descriptor files will all the possible cache settings included in the hibernate specification.

Additionally, each second level cache manager usually needs its own descriptor file with cache settings specific to this manager. The cartridge provides a template for creating the EhCache Manager descriptor file. For other managers, this template could be overwritten to generate another manager descriptor file.

EHCache

Currently AndroMDA fully supports EhCacheto provide the second level cache features to the cartridge. When enabled, the cartridge will create caches on queries, the Hibernate descriptor files, and the EhCache.xmlfile with the specific configuration.

Working with Second Level Cache

Setup

The cache settings provide support for customizing the cache configuration. The namespace properties for the cache settings are as following:

  • hibernateCacheProvider: Specifies the cache provider implementation that will be used for caching services. Default value is net.sf.hibernate.cache.EhCacheProvider
  • hibernateQueryCacheFactory: Factory class for the caching queries. Default value is net.sf.hibernate.cache.StandardQueryCacheFactory

Currently the cartridge only includes the dependencies for working with EhCache

Activation

To enable cache features on generated files, the following namespace properties should be used.

  • cache: you will need to specify this property in order to let the cartridge know where to output cache configuration files.
  • hibernateEnableCache: this property activates the caching features within the cartridge. When enabled, all Hibernate mapping files will be created with the required cache settings, the EhCache descriptor file will be generated, and the Hibernate configuration file property hibernate.cache.use_query_cache will be set to true.
  • hibernateEnableAssociationsCache: this property activates the custom cache settings for each association. When enabled, each association ends can contain its own specific configuration. default values for undefined properties are allowed.

Entities

The cartridge provides a default namespace property for each entity cache settings configuration. These properties will be applied by default (that is, if there is no specific tagged value on the entity that overrides the value). Namespace properties for entities are:

  • hibernateEntityCache: The cartridge default settings for this property is read-write, other possible values are nonstrict-read-writeand read-only

This property can be overridden using the following tagged value:

EhCache Configuration for Entities

Namespace properties and tagged values are available to customize EhCache Configuration. Namespace properties for EhCache configuration are:

The corresponding tagged values to overwrite the default values are:

Associations

Associations caching configuration can be configured individually. The namespace default property can be overridden using the tagged value. Namespace property for EhCache configuration is:

  • hibernateAssociationCache: The cartridge default settings for this property is read-write, other possible values are nonstrict-read-writeand read-only

Tagged value:

Finder Methods

The cartridge provides default namespace properties for activating the cache on one query. When enabled the generated code will contain an additional query.setCacheable(true)line. The namespace property for finder methods is:

  • hibernateUseQueryCache: The cartridge default settings for this property is read-write, other possible values are nonstrict-read-writeand read-only

This default value will be used for all the queries that are not overridden using the tagged value: