Hibernate and Memcached

153 views
Skip to first unread message

Duy Tùng Trần

unread,
May 2, 2014, 8:46:50 PM5/2/14
to hibernate...@googlegroups.com
Hello everyone,



I used memcached with hibernate. This is a part of my hibernate.cfg.xml

    <property name="hibernate.current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</property>

    <property name="hibernate.cache.use_second_level_cache">true</property>
   
    <property name="hibernate.cache.provider_class">com.googlecode.hibernate.memcached.MemcachedCacheProvider??</property>
   
    <property name="hibernate.Memcached.servers">localhost:1111</property>
   
    <property name="hibernate.Memcached.cacheTimeSeconds">300</property>
   
    <property name="hibernate.Memcached.connectionFactory">BinaryConnectionFactory</property>
   
    <property name="hibernate.cache.use_query_cache">true</property>



But when I load 2 times, hibernate still pull data from DB

    Session session = null;

    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    try {

    session = sessionFactory.openSession();
    PersonDAOImpl personDAO = new PersonDAOImpl();
    Person person = personDAO.loadPersonById(session, 1);
    System.out.println(person); session.close();
    session = sessionFactory.openSession();
    person = personDAO.loadPersonById(session, 1);
    System.out.println(person);

    } finally {

    if (session != null) {

    session.close();

    }

    }


And the log

2014-05-02 20:19:11.040 INFO net.spy.memcached.MemcachedConnection??: Added {QA sa=localhost/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue

2014-05-02 20:19:11.043 INFO net.spy.memcached.MemcachedConnection??: Connection state changed for sun.nio.ch.SelectionKeyImpl??@45c3e9ba


Hibernate: select person0.id as id0_0, person0.age as age0_0, person0.name as name0_0 from Person person0 where person0.id=?

Person{id=1, name='Dan', age=67}

Hibernate: select person0.id as id0_0, person0.age as age0_0, person0.name as name0_0 from Person person0 where person0.id=?  // pull from DB again

Person{id=1, name='Dan', age=67}


So, what did I miss?

RayKrueger

unread,
May 6, 2014, 10:49:13 PM5/6/14
to hibernate...@googlegroups.com
You need to look at your mappings/annotations and make sure you've set the class up as cacheable.
Reply all
Reply to author
Forward
0 new messages