3
respostas

Cache não esta funcionando

Estava fazendo o primeiro vídeo da aula 5 Conhecendo e Utilizando Cache no JavaEE, e depois de algumas alterações no código, o cache que antes estava sendo feito, não está mais para meus livros. Segue o código das classes: Meu persistence.xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    version="2.1" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
    http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="casadocodigo" transaction-type="JTA">
        <description>Dev persistence unit</description>
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <!-- java transaction api || JNDI -->
        <jta-data-source>java:jboss/datasources/casadocodigoDS</jta-data-source>
        <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
        <properties>
               <property name="hibernate.cache.infinispan.entity.expiration.lifespan" value="900000"/>
               <property name="hibernate.cache.infinispan.entity.expiration.max_idle" value="300000"/>        
            <property name="hibernate.cache.use_query_cache" value="true"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>           
        </properties>
    </persistence-unit>
</persistence>

Meu LivroDao:

public List<Livro> ultimosLancamentos() {
        Cache cache = manager.getEntityManagerFactory().getCache();
        cache.evict(Livro.class);
        String jpql = "select l from Livro l order by l.id desc";
        return manager.createQuery(jpql, Livro.class)
                .setMaxResults(2)
                .setHint(QueryHints.HINT_CACHEABLE, true)
                .setHint(QueryHints.HINT_CACHE_REGION, "home")
                .getResultList();
    }


    public List<Livro> demaisLivros() {
        String jpql = "select l from Livro l order by l.id desc";
        return manager.createQuery(jpql, Livro.class)
                .setHint(QueryHints.HINT_CACHEABLE, true)
                .setHint(QueryHints.HINT_CACHE_REGION, "home")
                .getResultList();
    }

    public void limpaCache() {
        Cache cache = manager.getEntityManagerFactory().getCache();
        cache.evict(Livro.class);
        cache.evictAll();

        SessionFactory factory = manager.getEntityManagerFactory().unwrap(SessionFactory.class);
        factory.getCache().evictAllRegions();
        factory.getCache().evictQueryRegion("home");

    }

}

e a classe Livro:


@Entity
@Cacheable
public class Livro {

3 respostas

Que erro que tá dando?

ele simplesmente não faz o cache, está sempre refazendo os select no livro, a principio estava funcionando, não sei porque ele parou de fazer o cache

reinicia tudo, dá um rebuild no projeto e vê se funciona.

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software