Bonjour,
je développe actuellement une application en Java et Hibernate.
Je viens de configurer ce dernier avec le cache de second niveau EHcache.
Voici mon ehcache.xml
et voici mon hibernate.cfg.xml
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118 <?xml version="1.0" encoding="UTF-8"?> <ehcache> <diskStore path="java.io.tmpdir"/> <!-- Cache par défaut --> <defaultCache maxElementsInMemory="10000" eternal="false" overflowToDisk="true" timeToIdleSeconds="120" timeToLiveSeconds="120" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" /> <!-- Cache pour l'objet Deci --> <cache name="cacheDeci" maxElementsInMemory="1000" eternal="true" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="false" /> <!-- Cache pour l'objet ModeleDeci --> <cache name="cacheModeleDeci" maxElementsInMemory="200" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="false" /> <!-- Cache pour l'objet Patient --> <cache name="cachePatient" maxElementsInMemory="1000" eternal="true" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="false" /> <!-- Cache pour l'objet Action --> <cache name="cacheAction" maxElementsInMemory="5000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="false" /> <!-- Cache pour l'objet Rdv --> <cache name="cacheRdv" maxElementsInMemory="5000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="false" /> <!-- Cache pour la collection Deci.patients --> <cache name="cacheDeciPatients" maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="false" /> <!-- Cache pour la collection Patient.decis --> <cache name="cachePatientDecis" maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="false" /> <!-- Cache pour la collection Patient.etudes --> <cache name="cachePatientEtudes" maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="false" /> <!-- Cache pour la collection Patient.rdvs --> <cache name="cachePatientRdvs" maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="false" /> <!-- Cache requêtes --> <cache name="org.hibernate.cache.StandardQueryCache" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false" memoryStoreEvictionPolicy="LRU" /> <cache name="cacheQueryGetAllPatients" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="300" overflowToDisk="false" /> </ehcache>
Nous utilisons les annotations directement dans nos classes ainsi nous n'utilisons pas d'autres fichiers de mapping Hibernate.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property> <property name="hibernate.connection.password">a</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/defib</property> <property name="hibernate.connection.username">a</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <!-- EHCache --> <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property> <property name="hibernate.cache.use_second_level_cache">true</property> <property name="hibernate.cache.use_query_cache">true</property> <!-- Montrer toutes les réquêtes générées --> <property name="show_sql">true</property> <!-- Mapping classes --> <mapping class="objets.Action"/> <mapping class="objets.Cardiopathie"/> <mapping class="objets.Deci"/> <mapping class="objets.Etude"/> <mapping class="objets.FamilleMessage"/> <mapping class="objets.Lecture"/> <mapping class="objets.Message"/> <mapping class="objets.ModeleDeci"/> <mapping class="objets.MotifImplantation"/> <mapping class="objets.Patient"/> <mapping class="objets.Personnel"/> <mapping class="objets.Rdv"/> <mapping class="objets.TypeMessage"/> <!-- Activation du cache pour les classes suivantes --> <!-- Action --> <class-cache usage="nonstrict-read-write" class="objets.Action"/> <!-- Cardiopathie --> <class-cache usage="nonstrict-read-write" class="objets.Cardiopathie"/> <!-- Deci --> <class-cache usage="nonstrict-read-write" class="objets.Deci"/> <!-- Etude --> <class-cache usage="nonstrict-read-write" class="objets.Etude"/> <!-- FamilleMessage --> <class-cache usage="nonstrict-read-write" class="objets.FamilleMessage"/> <!-- Lecture --> <class-cache usage="nonstrict-read-write" class="objets.Lecture"/> <!-- Message --> <class-cache usage="nonstrict-read-write" class="objets.Message"/> <!-- ModeleDeci --> <class-cache usage="nonstrict-read-write" class="objets.ModeleDeci"/> <!-- MotifImplantation --> <class-cache usage="nonstrict-read-write" class="objets.MotifImplantation"/> <!-- Patient --> <class-cache usage="nonstrict-read-write" class="objets.Patient"/> <!-- Personnel --> <class-cache usage="nonstrict-read-write" class="objets.Personnel"/> <!-- Rdv --> <class-cache usage="nonstrict-read-write" class="objets.Rdv"/> <!-- TypeMessage --> <class-cache usage="nonstrict-read-write" class="objets.TypeMessage"/> <!-- Activation du cache pour les collections suivantes --> <!-- Deci.patients --> <collection-cache usage="nonstrict-read-write" collection="objets.Deci.patients"/> <!-- Patient.decis --> <collection-cache usage="nonstrict-read-write" collection="objets.Patient.decis"/> <!-- Patient.etudes --> <collection-cache usage="nonstrict-read-write" collection="objets.Patient.etudes"/> <!-- Patient.rdvs --> <collection-cache usage="nonstrict-read-write" collection="objets.Patient.rdvs"/> </session-factory> </hibernate-configuration>
J'ai cru comprendre que cela suffisait pour alimenter le cache...j'ai donc créer une méthode de test que voici :
pour consulter le cache. Hélas, il est vide quand l'application se lance.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10 public static void getCacheStats(Boolean windowMode) { CacheManager cacheManager = CacheManager.getInstance(); String[] cacheNames = cacheManager.getCacheNames(); for (int i = 0; i < cacheNames.length; i++) { String cacheName = cacheNames[i]; if (!windowMode) System.out.println(cacheName+" - "+ cacheManager.getCache(cacheName).getStatistics().toString()); else JOptionPane.showMessageDialog(null, cacheName+" - "+ cacheManager.getCache(cacheName).getStatistics().toString()); } }
Mes questions sont les suivantes,
Est-ce que mes fichiers de mapping sont bon?
Que dois je faire pour remplir le cache?
Cordialement
Partager