Thank you. I am really grateful for your help and time.
Quote:
(1) Of course any timings not done in a loop are absolutely worthless.
Why? Because of Just In Time Compiling in Java? Database caching couldn't be a reason - I tested reading with JDBC, ADO, ADO.NET and second readings are not much lower then the firs one.
Quote:
(2) Reading from the second-level cache would not necessarily be any faster than reading from a local database cache
Second and third reading are with emptied both of caches (session.clear(); sessionFactory.evict(Partner.class);), so the time should bi similar to first one.
This is what I don't understand.Quote:
(3) You are not doing any transaction management (depending upon cache concurrency strategy, this means Hibernate will not even /use/ the second-level cache)
Based on my log, the second level cache is used. I hope I understood the log correctly. I added it bellow. (I filled only 3 records in table.)
I added transactions to my code (hopefully correct) and before first reading I read some data form another table to minimize the influence of JIT (had last one before but I didn't include it in a post).
The results are still the same (logging is of course off).
Thanks again,
Marko
Code:
------------------------------------ new code ------------------------------------
SessionFactory sessionFactory;
sessionFactory = new Configuration().configure().buildSessionFactory();
Transaction tx;
//************* Session cache *************
final ITimer timer = TimerFactory.newTimer();
Partner partner;
List list;
Iterator i;
Session session = sessionFactory.openSession();
tx = session.beginTransaction();
list = session.find("from Material material");
tx.commit();
System.gc();
tx = session.beginTransaction();
list = new ArrayList();
i = session.iterate("from Material material");
while (i.hasNext()) {
list.add(i.next());
}
tx.commit();
System.gc();
System.out.println("DEBUG: first reading");
tx = session.beginTransaction();
timer.start();
list = session.find("from Partner partner");
timer.stop();
tx.commit();
out.write("first reading" + "\n");
out.write(timer.getDuration() + "\n");
timer.reset();
session.clear();
sessionFactory.evict(Partner.class);
System.gc();
System.out.println("DEBUG: second reading with empty session and second level cache");
tx = session.beginTransaction();
timer.start();
list = session.find("from Partner partner");
timer.stop();
tx.commit();
out.write("second reading with empty session and second level cache" + "\n");
out.write(timer.getDuration() + "\n");
timer.reset();
session.clear();
sessionFactory.evict(Partner.class);
System.gc();
System.out.println("DEBUG: third reading with empty session and second level cache");
tx = session.beginTransaction();
timer.start();
list = session.find("from Partner partner");
timer.stop();
tx.commit();
out.write("third reading with empty session and second level cache" + "\n");
out.write(timer.getDuration() + "\n");
timer.reset();
System.gc();
System.out.println("DEBUG: reading with using session cache");
list = new ArrayList();
timer.start();
tx = session.beginTransaction();
i = session.iterate("from Partner partner");
while (i.hasNext()) {
list.add(i.next());
}
tx.commit();
timer.stop();
out.write("reading with using session cache" + "\n");
out.write(timer.getDuration() + "\n");
timer.reset();
list.clear();
session.close();
System.gc();
System.out.println("DEBUG: reading with using second level cache");
Session session2 = sessionFactory.openSession();
tx = session2.beginTransaction();
timer.start();
i = session2.iterate("from Partner partner");
while (i.hasNext()) {
list.add(i.next());
}
timer.stop();
tx.commit();
out.write("reading with using second level cache" + "\n");
out.write(timer.getDuration() + "\n");
timer.reset();
session.close();
sessionFactory.close();
System.gc();
-------------------------------- LOG - READING THREE RECORDS -----------------------
INFO - Hibernate 2.1.2
INFO - loaded properties from resource hibernate.properties: {hibernate.cglib.use_reflection_optimizer=true, hibernate.cache.provider_class=net.sf.hibernate.cache.HashtableCacheProvider, hibernate.cache.use_query_cache=true, hibernate.max_fetch_depth=1, hibernate.dialect=net.sf.hibernate.dialect.SQLServerDialect, hibernate.jdbc.use_streams_for_binary=true, hibernate.query.substitutions=true 1, false 0, yes 'Y', no 'N', hibernate.jdbc.batch_size=0, hibernate.proxool.pool_alias=pool1, hibernate.connection.username=sa, hibernate.connection.password=marko, hibernate.connection.pool_size=1}
INFO - using java.io streams to persist binary types
INFO - using CGLIB reflection optimizer
INFO - configuring from resource: /hibernate.cfg.xml
INFO - Configuration resource: /hibernate.cfg.xml
INFO - Mapping resource: si/marko/hibernate/Kategorija.hbm
INFO - Mapping class: si.marko.hibernate.Kategorija -> kategorija
INFO - Mapping resource: si/marko/hibernate/Artikel.hbm
INFO - Mapping class: si.marko.hibernate.Artikel -> artikel
INFO - Mapping resource: si/marko/hibernate/Inventura.hbm
INFO - Mapping class: si.marko.hibernate.Inventura -> inventura
INFO - Mapping resource: si/marko/hibernate/Material.hbm
INFO - Mapping class: si.marko.hibernate.Material -> material
INFO - Mapping resource: si/marko/hibernate/Normativ.hbm
INFO - Mapping class: si.marko.hibernate.Normativ -> normativ
INFO - Mapping resource: si/marko/hibernate/IzdaniRacun.hbm
INFO - Mapping class: si.marko.hibernate.IzdaniRacun -> izdani_racun
INFO - Mapping collection: si.marko.hibernate.IzdaniRacun.kategorija -> izdani_racun_kategorija
INFO - Mapping resource: si/marko/hibernate/PostavkaIzdRac.hbm
INFO - Mapping class: si.marko.hibernate.PostavkaIzdRac -> postavka_izd_rac
INFO - Mapping subclass: si.marko.hibernate.PostavkaIzdRacObic -> postavka_izd_rac
INFO - Mapping subclass: si.marko.hibernate.PostavkaIzdRacArt -> postavka_izd_rac
INFO - Mapping resource: si/marko/hibernate/PrejetiRacun.hbm
INFO - Mapping class: si.marko.hibernate.PrejetiRacun -> prejeti_racun
INFO - Mapping collection: si.marko.hibernate.PrejetiRacun.kategorija -> prejeti_racun_kategorija
INFO - Mapping resource: si/marko/hibernate/PostavkaPrejRac.hbm
INFO - Mapping class: si.marko.hibernate.PostavkaPrejRac -> postavka_prej_rac
INFO - Mapping resource: si/marko/hibernate/NormativIzdRac.hbm
INFO - Mapping class: si.marko.hibernate.NormativIzdRac -> normativ_izd__rac
INFO - Mapping resource: si/marko/hibernate/Partner.hbm
INFO - Mapping class: si.marko.hibernate.Partner -> partner
INFO - Configured SessionFactory: null
INFO - processing one-to-many association mappings
INFO - Mapping collection: si.marko.hibernate.IzdaniRacun.postavke -> postavka_izd_rac
INFO - Mapping collection: si.marko.hibernate.PrejetiRacun.postavke -> postavka_prej_rac
INFO - processing one-to-one association property references
INFO - processing foreign key constraints
INFO - Using dialect: net.sf.hibernate.dialect.SQLServerDialect
INFO - Maximim outer join fetch depth: 1
INFO - Use outer join fetching: true
INFO - C3P0 using driver: com.newatlanta.jturbo.driver.DataSource at URL: jdbc:JTurbo://marko:1433/kovinoplastika
INFO - Connection properties: {user=sa, password=marko}
INFO - Transaction strategy: net.sf.hibernate.transaction.JDBCTransactionFactory
INFO - No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)
INFO - Use scrollable result sets: true
INFO - Use JDBC3 getGeneratedKeys(): false
INFO - Optimize cache for minimal puts: false
INFO - echoing all SQL to stdout
INFO - Query language substitutions: {no='N', true=1, yes='Y', false=0}
INFO - cache provider: net.sf.hibernate.cache.HashtableCacheProvider
INFO - instantiating and configuring caches
INFO - building session factory
INFO - no JNDI name configured
INFO - starting update timestamps cache at region: net.sf.hibernate.cache.UpdateTimestampsCache
INFO - starting query cache at region: net.sf.hibernate.cache.QueryCache
Hibernate: select material0_.ID as ID, material0_.naziv as naziv, material0_.enota as enota, material0_.opomba as opomba from material material0_
Hibernate: select material0_.ID as x0_0_ from material material0_
DEBUG: first reading
Hibernate: select partner0_.ID as ID, partner0_.naziv as naziv, partner0_.naslov as naslov, partner0_.posta as posta, partner0_.drzava as drzava, partner0_.davcna as davcna, partner0_.kraj_za_placila as kraj_za_7_, partner0_.trans_rac_sedez as trans_ra8_, partner0_.trans_rac as trans_rac, partner0_.kont_oseba as kont_oseba, partner0_.tel as tel, partner0_.gsm as gsm, partner0_.email as email, partner0_.www as www, partner0_.opomba as opomba from partner partner0_
DEBUG: second reading with emty session and second level cache
Hibernate: select partner0_.ID as ID, partner0_.naziv as naziv, partner0_.naslov as naslov, partner0_.posta as posta, partner0_.drzava as drzava, partner0_.davcna as davcna, partner0_.kraj_za_placila as kraj_za_7_, partner0_.trans_rac_sedez as trans_ra8_, partner0_.trans_rac as trans_rac, partner0_.kont_oseba as kont_oseba, partner0_.tel as tel, partner0_.gsm as gsm, partner0_.email as email, partner0_.www as www, partner0_.opomba as opomba from partner partner0_
DEBUG: third reading with emty session and second level cache
Hibernate: select partner0_.ID as ID, partner0_.naziv as naziv, partner0_.naslov as naslov, partner0_.posta as posta, partner0_.drzava as drzava, partner0_.davcna as davcna, partner0_.kraj_za_placila as kraj_za_7_, partner0_.trans_rac_sedez as trans_ra8_, partner0_.trans_rac as trans_rac, partner0_.kont_oseba as kont_oseba, partner0_.tel as tel, partner0_.gsm as gsm, partner0_.email as email, partner0_.www as www, partner0_.opomba as opomba from partner partner0_
DEBUG: reading with using session cache
Hibernate: select partner0_.ID as x0_0_ from partner partner0_
DEBUG: reading with using second level cache
Hibernate: select partner0_.ID as x0_0_ from partner partner0_
INFO - closing