Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
457 views
in Technique[技术] by (71.8m points)

session - Hibernate reading function shows old data

I've a problem showing data from database. If I update an object, sometimes I get the old data, sometimes the new one. Update function works well (I can see in DB the right update) while the read function seems to get cached data. I've tried to disable both caches, tried to open and close session during update/save, but it's still not working. Both User and Store beans have Lazy fetches. Thanks!

READ FUNCTION:

    public static List<Store> getStoreByUser(User user)
        throws HibernateException {
    List<Store> result = null;
    Session session = sessionFactory.getCurrentSession();   
    Transaction transaction = null;
    try {
        transaction = session.getTransaction();
        Criteria criteria = session.createCriteria(Store.class);
        criteria.add(Restrictions.eq("userUserID", user));
        result = criteria.list();
    } catch (HibernateException he) {
        logger.error("No Store found for user: = " + user, he);
        throw he;
    }
    return result;
}

UPDATE/SAVE FUNCTION:

    public static Integer insertOrUpdateStore(Store store)
        throws HibernateException {
    Integer id = null;
    Session session = sessionFactory.getCurrentSession();   
    Transaction transaction = null;
    try {
                    transaction = session.getTransaction();
        if (store.getStoreID() != null && store.getStoreID() != 0) {

            session.merge(store);
            transaction.commit();

        } else {
                id = (Integer) session.save(store);
            transaction.commit();               
        }
    } catch (HibernateException he) {
        if (transaction != null) {
            transaction.rollback();
        }
    } finally {
    }       
    return id;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Usually, you have an isolation level "read committed". This lets your transaction see changes that have been committed by other transaction. The isolation level is implemented by the underlying dbms, not by hibernate.

You can't disable the first level cache (probably by using the stateless session, which shouldn't be used for general purposes). When executing a query, NH always returns values from the cache when it is found there, to ensure that you don't get the same db record more then once in memory.

If this is an issue for you, you should switch to a higher isolation level. For instance repeatable read (which means what it says: when reading the same data several times, you always get the same result). There is still the chance to see changes from other transaction. Using isolation level serializable, you shouldn't have this kind of issue anymore.

Note: switching to another isolation level is a breaking change to most systems and should be planned carefully.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...