【问题标题】:Hibernate not detecting MySQL changesHibernate 未检测到 MySQL 更改
【发布时间】:2015-06-19 08:36:29
【问题描述】:

我在一个Web应用程序项目中使用Hibernate和MySQL数据库,主要使用JSF,在我手动更改数据库中的数据并提交后,Hibernate没有检测到这些更改,并且hibernate查询返回相同旧结果,直到我重新启动应用程序,更改才变得可检测到。我禁用了二级缓存,但仍然。

public List<Property> findAll() {
    try {
        session = DBUtils.openSession();
        List<Property> resultList = (List<Property>) session.createCriteria(Property.class).add(Restrictions.eq("active", new Integer(1))).list();
        DBUtils.closeSession(session);
        return resultList;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

public class DBUtils {

    public static Configuration config;
    public static SessionFactory factory;

    static {
        try {
            config = new Configuration();
            config.configure();
            StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(config.getProperties());
            factory = config.buildSessionFactory(ssrb.build());
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static SessionFactory getSessionFactory() {
        return factory;
    }

    public static Session openSession() {
        try {
            return getSessionFactory().openSession();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static boolean closeSession(Session session) {
        try {
            if (session != null && session.isOpen()) {
                session.close();
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}

【问题讨论】:

  • 有点不相关,但是如果您开始使用 Hibernate 的 JPA API (EntityManager) 而不是底层的 Hibernate Session,您可能会更容易获得有关 StackOverflow 和 Internet 的帮助。 Hibernate 开发人员自己推荐它:“如果我们将来能以某种方式合并或弃用会话,我们会这样做......”(ref)。
  • 你也可以试试refresh
  • 它被称为缓存,这是使用 Hibernate、EclipseLink 等的问题之一。启用缓存后,您可以更改 JPA 之外的数据,例如使用 mysql 命令行界面进行 CSV 导入数据不会立即提供给 JPA。只有当您通过 JPA 导入数据时,它才可用。关闭缓存或刷新缓存是唯一的选择。
  • @Namphibian,我禁用了缓存使用。 falsefalse

标签: mysql hibernate


【解决方案1】:

在阅读http://www.ovaistariq.net/597/understanding-innodb-transaction-isolation-levels/之后,它似乎被称为隔离级别 并在hibernate cfg.xml中设置,我的问题就解决了

<property name="hibernate.connection.isolation">1</property>

【讨论】:

    猜你喜欢
    • 2015-01-15
    • 1970-01-01
    • 1970-01-01
    • 2016-07-09
    • 2015-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多