【问题标题】:HIbernate Query Issue (Possibly Cache Related)HIbernate 查询问题(可能与缓存相关)
【发布时间】:2015-05-25 19:26:25
【问题描述】:

我无法让我的查询显示更新的结果,我已经尝试了各种选项和解决方案来解决这个论坛和其他论坛上的其他问题,但目前还没有任何效果。

获得更新结果的唯一方法是重新启动我的应用程序。

注意事项:

  • 我正在一个新会话中运行每个查询(并已确认该会话是新的)。
  • 我正在外部编辑数据,因此没有要提交的事务(并确认数据已更新)。

我尝试过的 Hibernate 属性(来自其他问题的各种答案):

<property name="hibernate.cache.use_second_level_cache">false</property>
<property name="hibernate.cache.use_query_cache">false</property>
<property name="hibernate.connection.isolation">1</property>

会话工厂代码(为简洁起见,删除了不相关的代码):

public static SessionFactory createSessionFactory() {

    final Configuration configuration = new Configuration();

    // removed code to read database properties (host, port, user, password etc)
    configuration.addProperties(properties);
    // removed code to add entities
    configuration.addAnnotatedClass(... various entities ...);
    // the particular entity that I'm querying
    configuration.addAnnotatedClass(User.class);

    ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
            configuration.getProperties()).build();

    sessionFactory = configuration.buildSessionFactory(serviceRegistry);

    return sessionFactory;
}

打开会话代码:

public static Session openSession() {
    getDatabasePreferences();
    if (sessionFactory == null) {
        sessionFactory = createSessionFactory();
    }
    return sessionFactory.openSession();
}

查询代码:

public static List getList(Class entity, String order, Boolean reverseOrder, Boolean enabled) throws HibernateException {
    Session session = null;
    List list = null;

    try {
        session = openSession();
        // below causes exception (java.lang.IllegalArgumentException: Non-entity object instance passed to evict : class my.entity.User)
        session.evict(entity);
        // makes no difference
        session.clear();
        // removed code that determines sort order, other filters, etc...
        list = session.createCriteria(entity)
            // below statements (from other answers) have no effect
            .setCacheable(false)
            .setCacheMode(CacheMode.REFRESH)
            .setFlushMode(FlushMode.AUTO)
    } catch (HibernateException e) {
        throw new HibernateException(e);
    } finally {
        try {
            if (session != null) {
                session.close();
            }
        } catch (HibernateException e) {
            e.printStackTrace();
        }
    }

    if (list != null) {
        if (list.isEmpty()) {
            logger.log(Level.INFO, "List " + entity.getSimpleName() + " is empty.");
        } else {
            //logger.log(Level.INFO, "Obtained " + entity.getSimpleName() + " List: " + list);
            logger.log(Level.INFO, "Obtained " + entity.getSimpleName());
        }
    } else {
        logger.log(Level.WARNING, "Unable to obtain " + entity.getSimpleName() + " List.");
    }

    return list;
}

【问题讨论】:

    标签: java hibernate session caching entity


    【解决方案1】:

    顺便说一句,我在过去遇到了这个问题,并在刷新会话时解决了它。如果不是您的问题,请查看这篇文章,也许可以帮助您: Hibernate reading function shows old data

    【讨论】:

    • 我已经尝试了该帖子中的各种选项(在发布我自己的问题之前)。任何隔离级别都没有任何影响,刷新会话无关紧要,因为每个查询都在其自己的会话中运行。
    • 由于您的回答,我现在已经弄清楚为什么会发生这种情况。当我测试 hibernate.cfg.xml 中的属性组合时……我已经确定它们没有应用于会话……手动将属性添加到我的会话工厂代码中。
    • 您可以检查您的配置对象(手动与 cfg.xml)并查看它们之间的差异
    【解决方案2】:

    我的问题中指定的 Hibernate 属性未应用于会话。在我的会话工厂方法中手动添加属性后,我的查询现在返回更新的数据...

    现在包括:

    configuration.setProperty("hibernate.c3p0.max_statements","0");
    configuration.setProperty("hibernate.cache.use_second_level_cache","false");
    configuration.setProperty("hibernate.cache.use_query_cache","false");
    configuration.setProperty("hibernate.connection.isolation","1");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-31
      • 1970-01-01
      • 1970-01-01
      • 2010-11-15
      • 2012-04-29
      • 2014-06-25
      • 2022-11-28
      • 2018-11-18
      相关资源
      最近更新 更多