en utilisant des versions;Le cache de second niveau ne fonctionne jamais avec spring3, hibernate4, ehcache?
<spring.version>3.2.8.RELEASE</spring.version>
<hibernate.version>4.2.11.Final</hibernate.version>
configuration d'hibernation;
...
<bean id="jpaAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
p:database="${jpa.database}" p:showSql="${jpa.showSql}"/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">auto</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.connection.autocommit">true</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
<!--useful for debugging-->
<prop key="hibernate.generate_statistics">true</prop>
</props>
</property>
<property name="packagesToScan" value="info.hevi.learn.spring3hibernate4ehcache"/>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
...
configuration ehcache (ehcache.xml);
<cache name="countryCache"
maxElementsInMemory="300"
eternal="true"
overflowToDisk="false"
timeToIdleSeconds="12000"
timeToLiveSeconds="12000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU" />
<cache name="info.hevi.learn.spring3hibernate4ehcache.domain.Country"
maxElementsInMemory="300"
eternal="true"
overflowToDisk="false"
timeToIdleSeconds="12000"
timeToLiveSeconds="12000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU" />
classes de domaine;
public class Language implements IEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(nullable = false, unique = true)
private String name;
...
}
et
@Entity
@Table(name = "countries")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@NamedQueries({
@NamedQuery(name="Country.findLanguagesByCountryId",query="select language from Country country inner join country.languages language where country.id=:cid")
})
public class Country implements IEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column
private String name;
@Column
private Integer population;
@Column(updatable = false, insertable = false)
@Temporal(TemporalType.TIMESTAMP)
private Calendar creation;
@ManyToMany(targetEntity = Language.class, fetch = FetchType.EAGER)
@JoinTable(name = "country_language", joinColumns = {@JoinColumn(name = "cid")}, inverseJoinColumns = {@JoinColumn(name = "lid")})
private Set<Language> languages;
...
}
et classe de service;
@Service(value = "countryService")
public class CountryService extends AbstractBasicService<Country, Long, ICountryDao> implements ICountryService {
...
@Override
@Cacheable(value = "countryCache")
@Transactional
public List<Country> getAll() {
return super.getAll();
}
...
}
et le code de test;
@Test
public void testCache() {
countryService.getAll();
countryService.getAll();
countryService.getAll();
}
et enfin les statictics;
07-18-2014 02:26:42,991 PM INFO StatisticalLoggingSessionEventListener:275 - Session Metrics {
57541 nanoseconds spent acquiring 1 JDBC connections;
0 nanoseconds spent releasing 0 JDBC connections;
834336 nanoseconds spent preparing 1 JDBC statements;
1394341 nanoseconds spent executing 1 JDBC statements;
0 nanoseconds spent executing 0 JDBC batches;
317686 nanoseconds spent performing 7 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
0 nanoseconds spent performing 0 L2C misses;
655636 nanoseconds spent executing 1 flushes (flushing a total of 11 entities and 7 collections);
109408 nanoseconds spent executing 1 partial-flushes (flushing a total of 0 entities and 0 collections)
}
07-18-2014 02:26:43,003 PM INFO StatisticalLoggingSessionEventListener:275 - Session Metrics {
31202 nanoseconds spent acquiring 1 JDBC connections;
0 nanoseconds spent releasing 0 JDBC connections;
351321 nanoseconds spent preparing 1 JDBC statements;
1095294 nanoseconds spent executing 1 JDBC statements;
0 nanoseconds spent executing 0 JDBC batches;
281218 nanoseconds spent performing 7 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
0 nanoseconds spent performing 0 L2C misses;
579456 nanoseconds spent executing 1 flushes (flushing a total of 11 entities and 7 collections);
11346 nanoseconds spent executing 1 partial-flushes (flushing a total of 0 entities and 0 collections)
}
07-18-2014 02:26:43,015 PM INFO StatisticalLoggingSessionEventListener:275 - Session Metrics {
23502 nanoseconds spent acquiring 1 JDBC connections;
0 nanoseconds spent releasing 0 JDBC connections;
366313 nanoseconds spent preparing 1 JDBC statements;
695348 nanoseconds spent executing 1 JDBC statements;
0 nanoseconds spent executing 0 JDBC batches;
274329 nanoseconds spent performing 7 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
0 nanoseconds spent performing 0 L2C misses;
816100 nanoseconds spent executing 1 flushes (flushing a total of 11 entities and 7 collections);
8509 nanoseconds spent executing 1 partial-flushes (flushing a total of 0 entities and 0 collections)
}
Comme vous pouvez le voir, il ne frappe jamais le cache, tout le temps! J'ai également débogué dans la fonction de service, il exécute réellement la fonction qui n'est pas supposée se produire si c'est vraiment des caches. Qu'est-ce qui ne va pas? Un argument javar me manque-t-il ou fait-il une erreur sémantique?
Malheureusement, cela n'a pas fonctionné. – hevi
Vérifier ma mise à jour –
query.setCacheable (true); travaillé pour moi. Mais comme j'utilise des requêtes au niveau dao, y a-t-il un moyen de déplacer la logique au niveau du service? et comment expulser les caches de requête? – hevi