I have a very simple application where I am trying to enable second level cache, but can't get it to work.
I have a main class which look like this:
------------------------------------
public static void main(String[] args) {
Session session = HibernateUtil.getCurrentSession();
HibernateUtil.beginTransaction();
for (int i = 0; i < 5; i++) {
Timer timer = new Timer("Get players");
List list = session.createCriteria( PlayerEntity.class )
.add(Restrictions.eq( "nickName", "homer" ))
.setCacheable(true).list();
timer.done();
}
HibernateUtil.rollbackTransaction();
}
------------------------------------
The class PlayerEntity looks like this:
------------------------------------
@Entity
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class PlayerEntity implements Serializable{
private String nickName;
private long id;
// Getters and setters...
}
------------------------------------
hibernate.cfg.xml:
------------------------------------
<property name="show_sql">false</property>
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.username">scott</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:HIBER</property>
<property name="hibernate.connection.password">tiger</property>
<property name="hibernate.connection.schema">SCOTT</property>
<property name="hibernate.connection.autocommit">false</property>
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<mapping class="emil.poker.entities.PlayerEntity"/>
</session-factory>
</hibernate-configuration>
------------------------------------
When I run the main method this is what is displayed:
Get players : 78 ms.
Get players : 31 ms.
Get players : 0 ms.
Get players : 16 ms.
Get players : 0 ms.
But if I remove the @Cache from PlayerEntity and remove the three lines
about cache from hibernate.cfg.xml, this is my result:
Get players : 63 ms.
Get players : 31 ms.
Get players : 0 ms.
Get players : 0 ms.
Get players : 16 ms.
It doesn't seem like the cache is working properly.
Does anyone have any ideas?
According to the log I am missing some configurations files:
WARN - Could not find configuration [org.hibernate.cache.UpdateTimestampsCache]; using defaults.
WARN - Could not find configuration [org.hibernate.cache.StandardQueryCache]; using defaults.
Could this be the reason?
|