Quantcast
Channel: Spring Community Forums - Data
Viewing all articles
Browse latest Browse all 297

Cached Query Opens JDBC Connection

$
0
0
I'm using a cache for the queries:

Code:

  public interface ResourceQuery extends Repository<Resource,Integer>{

    @Query("SELECT r from Resource r WHERE r.resourceIdentifier.resourceServerName = :resourceServerName")
    @QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value ="true") })
    List<Resource> getResourcesByServerCached(@Param("resourceServerName") String resourceServerName);
...

I see the cache filling and cache hits for the entities in the MBean. I see the StandardQueryCache just filled, hit count and miss count is always 0.

I'm wondering, why the logs show an "Obtaining JDBC connection" and a "select", when I use the cache:
(LogicalConnectionImpl.java:295) - Obtaining JDBC connection
....
(SqlStatementLogger.java:104) - select resource0_.
...

Opening the JDBC connection costs a lot of time...


Here is my configuration:

Code:

  <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    ...
    <property name="jpaPropertyMap">
      <util:map>
        <entry key="javax.persistence.sharedCache.mode" value="DISABLE_SELECTIVE"/>
        <entry key="javax.persistence.cache.retrieveMode">
          <util:constant static-field="javax.persistence.CacheRetrieveMode.USE"/>
        </entry>
        <entry key="javax.persistence.cache.storeMode">
          <util:constant static-field="javax.persistence.CacheStoreMode.REFRESH"/>
        </entry>
        <entry key="hibernate.cache.use_second_level_cache"  value="true" />
        <entry key="hibernate.cache.use_query_cache"          value="true" />
        <entry key="hibernate.generate_statistics"            value="true"/>
        <entry key="hibernate.cache.region.factory_class"    value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory"/>
        <entry key="net.sf.ehcache.configurationResourceName" value="/ehcache.xml"/>
      </util:map>
    </property>
    ...

Code:

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

  <defaultCache
          maxElementsInMemory="10000"
          timeToLiveSeconds="300"
          timeToIdleSeconds="120"
          eternal="false"
          overflowToDisk="false"
          statistics="true">
  </defaultCache>

  <!-- HBM query cache -->
  <cache name="org.hibernate.cache.internal.StandardQueryCache"
        maxElementsInMemory="100"
        timeToLiveSeconds="3600"
        timeToIdleSeconds="3600"
        eternal="false"
        overflowToDisk="false"
        statistics="true" logging="true">
  </cache>       

  <cache name="org.hibernate.cache.spi.UpdateTimestampsCache"
        maxElementsInMemory="5000"
        eternal="true"
        overflowToDisk="false"
        statistics="true">
  </cache>

</ehcache>

Thank you!

Kai

Viewing all articles
Browse latest Browse all 297

Trending Articles