Base de conhecimento

Erro: Second-level cache is not enabled for usage - Como resolver?

3 minutos de leitura
Copiar

Problema

Erro: Second-level cache is not enabled for usage [hibernate.cache.use_second_level_cache | hibernate.cache.use_query_cache].

Essa exceção é ocasionada quando no arquivo de configuração hibernate.properties, usado por todos os serviços que se conectam com o Banco de Dados não possui o parâmetro:

hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider

Quando o parâmetro abaixo está ativo (true):

hibernate.cache.use_second_level_cache = true

Exemplo de configuração no hibernate.properties:

# 2d level cache
hibernate.cache.use_query_cache=false
hibernate.cache.use_second_level_cache=true
#hibernate.cache.region.factory_class=net.sf.ehcache.hibernate.EhCacheRegionFactory
hibernate.cache.region.factory_class=net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory
net.sf.ehcache.configurationResourceName=/ehcache-local.xml

Exemplo da exceção nos logs:

WrapperSimpleApp: Encountered an error running main:
WrapperSimpleApp: javax.persistence.PersistenceException: [PersistenceUnit: oobj-mail] Unable to build EntityManagerFactory
WrapperSimpleApp:     at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:677)
WrapperSimpleApp:     at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:126)
WrapperSimpleApp:     at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:52)
WrapperSimpleApp:     at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:34)
WrapperSimpleApp:     at org.utils.core.dao.jpa.jse.JpaDaoImplJse.createEntityManagerFactory(JpaDaoImplJse.java:33)
WrapperSimpleApp:     at br.com.oobj.mail.MailMain.main(MailMain.java:27)
WrapperSimpleApp:     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
WrapperSimpleApp:     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
WrapperSimpleApp:     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
WrapperSimpleApp:     at java.lang.reflect.Method.invoke(Unknown Source)
WrapperSimpleApp:     at org.tanukisoftware.wrapper.WrapperSimpleApp.run(WrapperSimpleApp.java:236)
WrapperSimpleApp:     at java.lang.Thread.run(Unknown Source)
WrapperSimpleApp: Caused by: org.hibernate.HibernateException: Could not instantiate cache implementation
WrapperSimpleApp:     at org.hibernate.cache.CacheFactory.createCache(CacheFactory.java:64)
WrapperSimpleApp:     at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:214)
WrapperSimpleApp:     at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1300)
WrapperSimpleApp:     at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
WrapperSimpleApp:     at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669)
WrapperSimpleApp:     ... 11 more
WrapperSimpleApp: Caused by: org.hibernate.cache.NoCachingEnabledException: Second-level cache is not enabled for usage 
[hibernate.cache.use_second_level_cache | hibernate.cache.use_query_cache]
WrapperSimpleApp:     at org.hibernate.cache.NoCacheProvider.buildCache(NoCacheProvider.java:21)
WrapperSimpleApp:     at org.hibernate.cache.CacheFactory.createCache(CacheFactory.java:61)
WrapperSimpleApp:     ... 15 more

 

Solução/Procedimento

Para corrigir essa exceção deverá ser editado o arquivo hibernate.properties salvo na pasta “..\config” de cada serviço Oobj. Para isso, abra o arquivo com um editor de texto (sugerimos o uso do NotePad++).

Exemplo:

C:\Oobj\Aplicativos\Oobj\oobj-motor\config\hibernate.properties

Há duas opções para corrigir o problema:

1 – Desativar o parâmetro: hibernate.cache.use_second_level_cache (false)

Exemplo:

hibernate.cache.use_query_cache=false
hibernate.cache.use_second_level_cache=false
#hibernate.cache.region.factory_class=net.sf.ehcache.hibernate.EhCacheRegionFactory
hibernate.cache.region.factory_class=net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory
net.sf.ehcache.configurationResourceName=/ehcache-local.xml

2 – Adicionar o parâmetro: hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider

Exemplo:

# 2d level cache
hibernate.cache.use_query_cache=false
hibernate.cache.use_second_level_cache=true
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
#hibernate.cache.region.factory_class=net.sf.ehcache.hibernate.EhCacheRegionFactory
hibernate.cache.region.factory_class=net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory
net.sf.ehcache.configurationResourceName=/ehcache-local.xml

Referência

Sem referências

Não encontrou o que procurava?