These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: ehcache can't work with websphere?
PostPosted: Tue May 09, 2006 2:05 am 
Newbie

Joined: Tue May 09, 2006 12:40 am
Posts: 2
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?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.