I'm developing an application which running hibernate inside websphere, for the performance concern, ehcache is used to cache some data from db.
I have configured exactly the same way as user reference told us to do, however, Ehcache failed to cache any data from db.
So, I wirte a simple unit test code to see what's gonning on, it seems the ehcache works well if the session is created by sessionFactory.openSession(), and if a session is created by sessionFactory.openSession(connection), it just can't cache the data.
Please see the code below:
Code:
public class TestCache extends TestCase {
private SessionFactory factory;
protected void setUp() throws Exception {
if(factory == null){
Configuration config = new Configuration();
config.configure(this.getClass().getClassLoader().getResource("hibernate.cfg.xml"));
factory = config.buildSessionFactory();
}
}
public void testCache(){
try {
Session session = factory.openSession();
//the first time to select roles
selectAllRoles(session);
//cache works: select roles with the same session
selectAllRoles(session);
Session newSession = factory.openSession();
//cache works: we do the selection again with different session, but within the same connection
selectAllRoles(newSession);
Connection connection = null;
try { // load the driver
Class.forName("org.hsqldb.jdbcDriver").newInstance();
} catch (Exception e) { // problem loading driver, class not exist?
e.printStackTrace();
return;
}
try {
connection = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/xdb", "sa", "");
System.out.println("-------------Connection successful!---------------------------------");
Session sessionWithNewConnection = factory.openSession(connection);
//cache fails: we created a new connection, and do the query again, and it hits the data base
selectAllRoles(sessionWithNewConnection);
System.out.println("-------------another session with the same connection---------------");
Session newSessionWithNewConnection = factory.openSession(connection);
//cache still fails: we using the same connection, and do the query again, and it hits the data base
selectAllRoles(newSessionWithNewConnection);
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void selectAllRoles(Session session) {
Query query = null;
List roles = null;
try {
query = session.getNamedQuery("role.all");
query.setCacheable(true);
roles = query.list();
} catch (HibernateException e) {
e.printStackTrace();
}
}
}
and the system out log:
Quote:
Hibernate: select role0_.id as id, role0_.name as name from role role0_
-------------Connection successful!---------------------------------
Hibernate: select role0_.id as id0_, role0_.name as name0_ from role role0_ where role0_.id=?
Hibernate: select role0_.id as id0_, role0_.name as name0_ from role role0_ where role0_.id=?
-------------another session with the same connection---------------
Hibernate: select role0_.id as id0_, role0_.name as name0_ from role role0_ where role0_.id=?
Hibernate: select role0_.id as id0_, role0_.name as name0_ from role role0_ where role0_.id=?
since we are working in the websphere, we are making use of the DataSource to get the sql connection. so, does that mean we can't caching with ehcache in websphere?