简体   繁体   中英

EhCache is not updated after a database insert/update using Hibernate as JPA provider

First of all,

I have an application designed using JPA/Hibernate for persistence, one JMS queue and a SOAP webservice interface with third-party systems. The application runs in a clustered JBoss EAP 6.0 server.

There are 2 databases, the first one is an Oracle 10g database from which I get all the data I want to process and the second one is a PostgreSQL database where I persist the result of the process to. The PostgreSQL database works as a persistent cache, because it's take too much time to process the data from the Oracle 10g database. The whole process works like this:

  • Get data from the Oracle 10g database
  • Process the data and generate the result
  • Send the result to a persistent JMS queue asynchronously and returns the result as WebService XML
  • An MDB consumes the result from the queue and save it in the database using JPA

However, the response time is still too long and we need to reduce it. When I try to use EHCACHE as Second Level Cache with Hibernate, to cache the result from the PostgreSQL database, the cache only works if I query it twice. That said, if there is any update, delete or insert in the database the cache gets out of sync with it. My problem is:

What should I do to update/sync EHCACHE when I update/delete/insert something in the database?

My persistence.xml is:

<persistence-unit name="sDs" transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:jboss/datasources/sDs</jta-data-source>

        <shared-cache-mode>DISABLE_SELECTIVE</shared-cache-mode>

        <mapping-file>META-INF/jpql/NamedQueries.xml</mapping-file>

        <exclude-unlisted-classes>true</exclude-unlisted-classes>

        <class>x.AusenciaX</class>


       <properties>
            <property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.EhCacheProvider" />             
            <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory" />
            <property name="hibernate.cache.default_cache_concurrency_strategy" value="read-write" />
            <property name="hibernate.cache.use_second_level_cache" value="false"/>
            <property name="hibernate.cache.use_query_cache" value="false"/>
            <property name="hibernate.default_schema" value="sisfpj" /> 
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="hibernate.generate_statistics" value="true" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
        </properties>
    </persistence-unit>

ehcache.xml:

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="ehcache.xsd"
    updateCheck="false" monitoring="autodetect"
    dynamicConfig="true">

    <defaultCache
        maxElementsInMemory="1000"
        eternal="false"
        timeToIdleSeconds="86400"
        timeToLiveSeconds="86400"
        overflowToDisk="true"
    />
</ehcache>

Thanks in advance.

Answer to your first part, cache only works if I query it twice. First time it will query the DB and load it in cache, so second time you query it will load from cache and not db.

可能是因为您的persistence.xml文件中的hibernate.cache.use_second_level_cache行必须设置为true否则将不使用您的缓存。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM