Question

I am trying to configure second level cache in hibernate application using EhCacheProvider. I have done the configuration in hibernate.cfg.xml as:

    <!-- Second-level cache -->
    <property name="cache.use_second_level_cache">true</property>
    <property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>

and I have annotated my entity as:

    @Entity(name = "UserDetails")
    @Table(name = "user_details")
    @Cacheable(true)
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
    public class UserDetails {

        private int id;
        private String userName;

        public UserDetails(){}

        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "user_id")
        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        @Column(name = "user_name") 
        public String getUserName() {
            return userName;
        }
        public void setUserName(String userName) {
            this.userName = userName;
        }
    }

And in my Test class:

    public class Test {
        public static void main(String[] args){
            SessionFactory factory = new AnnotationConfiguration().configure().buildSessionFactory();
            Session session = factory.openSession();
            Session session2 = factory.openSession();
            try{
                session.get(UserDetails.class, 1);

                session.close();

                session2.get(UserDetails.class, 1);

            }catch(Exception e){
                e.printStackTrace();
            }finally{  
                session2.close();
            }
        }
    }

What I know about second level cache is that if an object fetch for the fist time from db by any session it stored in second level of cache and if we try to get the same object from another session it is given form the second level of cache.

So as per the Test class there should be 1 select query fire by the hibernate, but I am getting 2 select queries on console.

If I change the

  @Cache(usage = CacheConcurrencyStrategy.READ_ONLY) //<-- READ_ONLY instead of READ_WRITE

I am getting only one query fire by hibernate(intended result)

So I am not able to understand why I getting 2 select queries if I set CacheConcurrencyStrategy.READ_WRITE.

I am using Hibernate 3.3.2 and ehcache-2.8.1

Any light on this scenario will appreciated.

Thanks.

No correct solution

OTHER TIPS

Session transaction time should be greater than Cache update time. Change your code like below-

     public class Test {
            public static void main(String[] args){
                SessionFactory factory = new AnnotationConfiguration().configure().buildSessionFactory();
                Session session = factory.openSession();
                try{
                    session.get(UserDetails.class, 1);
                    session.close();
                    Session session2 = factory.openSession();
                    session2.get(UserDetails.class, 1);

                }catch(Exception e){
                    e.printStackTrace();
                }finally{  
                    session2.close();
                }
            }
        }

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top