Bonjour,
Je fais des tests pour essayer d'améliorer les performances d'une application java qui utilise hibernate.
Du coup j'ai écrit un petit programme qui utilise le mapping de l'application et j'ai commencé à mettre un cache de 2ème niveau. Malheureusement lorsque j'active le cache des collections, l'application met plus de temps que sans cache.
Voila ce que j'ai mit :
Le fichier hibernate.cfg.xml :
Le fichier ehcache.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 <?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="connection.pool_size">1</property> --> <!-- C3P0 connection pool --> <property name="hibernate.c3p0.min_size">5</property> <property name="hibernate.c3p0.max_size">20</property> <property name="hibernate.c3p0.timeout">30000</property> <property name="hibernate.c3p0.max_statements">0</property> <!-- SQL dialect --> <!-- <property name="dialect"> org.hibernate.dialect.SQLServerDialect </property> //--> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Enable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property> <property name="hibernate.cache.use_second_level_cache">true</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">false</property> <!-- Drop and re-create the database schema on startup --> <!-- <property name="hbm2ddl.auto">create</property> --> <property name="hibernate.cache.use_second_level_cache">true</property> <mapping resource="com/kayentis/faspharma/apidb/main/mapping/Form.hbm.xml"/> <mapping resource="com/kayentis/faspharma/apidb/main/mapping/Study.hbm.xml"/> <mapping resource="com/kayentis/faspharma/apidb/main/mapping/FieldStruct.hbm.xml"/> <mapping resource="com/kayentis/faspharma/apidb/main/mapping/User.hbm.xml"/> ... <class-cache class="com.kayentis.faspharma.apidb.main.FieldStruct" usage="read-only" include="all" /> <class-cache class="com.kayentis.faspharma.apidb.main.Form" usage="read-only" include="all" /> <collection-cache collection="com.kayentis.faspharma.apidb.main.Form.fieldsStruct" usage="read-write" /> </session-factory> </hibernate-configuration>
Mes classes :
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 <ehcache> <diskStore path="java.io.tmpdir"/> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" /> <cache name="com.kayentis.faspharma.apidb.main.FieldStruct" maxElementsInMemory="3000" eternal="true" overflowToDisk="false" /> <cache name="com.kayentis.faspharma.apidb.main.Form" maxElementsInMemory="300" eternal="false" overflowToDisk="false" timeToIdleSeconds="300" timeToLiveSeconds="600" /> <cache name="com.kayentis.faspharma.apidb.main.User" maxElementsInMemory="300" eternal="false" overflowToDisk="false" timeToIdleSeconds="300" timeToLiveSeconds="600" /> <cache name="com.kayentis.faspharma.apidb.main.Study" maxElementsInMemory="300" eternal="false" overflowToDisk="false" timeToIdleSeconds="300" timeToLiveSeconds="600" /> </ehcache>
On voit bien avec les stat d'hiberante que les objest lus diminuent fortement quand le cache est activé. Mais le temps d'execution augmente énormément.
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 public class Main { private static final Logger LOG = Logger.getLogger(Main.class); public Main() throws InterruptedException { LOG.info("Main"); int nb = 50; LOG.info("Close current Session"); List listThread = new ArrayList(nb); for (int i = 0; i < nb; i++) { listThread.add(new Thread1()); } for (int i = 0; i < nb; i++) { Thread thread = (Thread) listThread.get(i); thread.start(); thread.join(); } } } public class Thread1 extends Thread { private final Logger LOG; public Thread1() { LOG = Logger.getLogger(this.getName()); LOG.debug("affectation"); } public void run() { LOG.debug("start"); Session session = UserDAO.getInstance().getSession(); session.getSessionFactory().getStatistics().setStatisticsEnabled(true); LOG.debug("session : " + session.hashCode()); LOG.debug("sessionFactory : " + session.getSessionFactory().hashCode()); Transaction transaction = session.beginTransaction(); User user = UserDAO.getInstance().getUserFromLogin("test", session); Set studies = user.getStudies(); LOG.debug("NB study : " + studies.size()); for (Iterator it = studies.iterator(); it.hasNext();) { Study study = (Study) it.next(); Set forms = study.getForms(); LOG.debug("NB forms : " + forms.size()); for (Iterator itt = forms.iterator(); itt.hasNext();) { Form form = (Form) itt.next(); Set fieldsStruct = form.getFieldsStruct(); LOG.debug("NB fieldsStruct : " + fieldsStruct.size()); } } transaction.commit(); LOG.debug("User : " + user.getIdUser()); Statistics tStats = session.getSessionFactory().getStatistics(); LOG.debug("stat SF EntityLoadCount : " + tStats.getEntityLoadCount()); LOG.debug("stat SF QueryCacheHitCount : " + tStats.getQueryCacheHitCount()); LOG.debug("stat SF EntityFetchCount : " + tStats.getEntityFetchCount()); LOG.debug("stat SF QueryExecutionCount : " + tStats.getQueryExecutionCount()); LOG.debug("stat SF PrepareStatementCount : " + tStats.getPrepareStatementCount()); session.close(); LOG.debug("end"); } }
Vous avez une idée du pourquoi?
Youkoun
Partager