Hibernate读取功能显示旧数据 [英] Hibernate reading function shows old data

查看:132
本文介绍了Hibernate读取功能显示旧数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示数据库数据的问题。如果我更新一个对象,有时我会得到旧数据,有时候会是新数据。更新函数运行良好(我可以在DB中看到正确的更新),而读取函数似乎获得缓存数据。我试图禁用这两个缓存,试图在更新/保存期间打开和关闭会话,但仍然无法正常工作。
用户和存储Bean都有延迟提取。
Thanks!

阅读功能:

  public static列表与LT;存储> getStoreByUser(用户用户)
抛出HibernateException {
List< Store> result = null;
Session session = sessionFactory.getCurrentSession();
交易交易=空;
尝试{
transaction = session.getTransaction();
Criteria criteria = session.createCriteria(Store.class);
criteria.add(Restrictions.eq(userUserID,user));
result = criteria.list();
} catch(HibernateException he){
logger.error(找不到用户的商店:=+ user,他);
扔他;
}
返回结果;
}

更新/保存功能:

  public static Integer insertOrUpdateStore(Store store)
throws HibernateException {
Integer id = null;
Session session = sessionFactory.getCurrentSession();
交易交易=空;
尝试{
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();
}
}最后{
}
return id;


解决方案

通常情况下, 读取已提交(隔离级别)读取已提交。这可以让您的交易看到其他交易承诺的变更。隔离级别由底层dbms实现,而不是由hibernate实现。



不能禁用第一级缓存(可能使用无状态会话,用于一般目的)。当执行一个查询时,NH总是从缓存中返回值,当它在那里被发现时,确保你不会在内存中获得多于一次的同一个数据库记录。



如果这对您是个问题,您应该切换到更高的隔离级别。例如可重复读取(这意味着它说:当多次读取相同的数据时,您总会得到相同的结果)。仍有机会看到其他交易的变化。使用可序列化的隔离级别,您不应该再遇到这种问题。



注意:切换到另一个隔离级别对大多数系统是一个重大改变,应该仔细规划。


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;
}

解决方案

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.

这篇关于Hibernate读取功能显示旧数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆