0

In our application we have an HibernateSessionFactory class, that is opening and closing connections. Everything is okay, but when we are updating data in the database, it doesn't change in our application. Unfortunately, we see old data from the database. How can I fix this?

public class HibernateSessionFactory {

    private static final ThreadLocal threadLocal = new ThreadLocal();
    private static org.hibernate.SessionFactory sessionFactory;

    private static Configuration configuration = new Configuration();
    private static ServiceRegistry serviceRegistry;

    private static final Logger log = Logger.getLogger(
                                        HibernateSessionFactory.class);

    static {
        try {
            configuration.configure();
            serviceRegistry = new ServiceRegistryBuilder()
                        .applySettings(configuration.getProperties())
                        .buildServiceRegistry();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        } catch (Exception e) {
            log.error("Error Creating SessionFactory", e);
        }
    }

    private HibernateSessionFactory() {}

    public static Session getSession() throws HibernateException {
        Session session = (Session) threadLocal.get();
        if (session == null || !session.isOpen()) {
            if (sessionFactory == null) {
                rebuildSessionFactory();
            }
            session = (sessionFactory != null) ?
                        sessionFactory.openSession() : null;

            threadLocal.set(session);
        }
        return session;
    }

    public static void rebuildSessionFactory() {

        try {
            configuration.configure();
            serviceRegistry = new ServiceRegistryBuilder()
                        .applySettings(configuration.getProperties())
                        .buildServiceRegistry();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        } catch (Exception e) {
            log.error("Error Creating SessionFactory", e);
        }
    }

    public static void closeSession() throws HibernateException {
        Session session = (Session) threadLocal.get();
        threadLocal.set(null);
        if (session != null) {
            session.flush();
            session.close();

        }
    }

    public static org.hibernate.SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static Configuration getConfiguration() {
        return configuration;
    }

}

.

@SuppressWarnings("unchecked")
public List<Tauthor> getAuthors() throws HibernateException {
    log.debug("getting all authors");
    Query queryObject = null;
    List<Tauthor> authors = null;
    Session session = HibernateSessionFactory.getSession();
    try {
        String queryString = "from Tauthor";
        queryObject = session.createQuery(queryString);
        authors = queryObject.list();
    } catch (HibernateException e) {
        log.error("get all authors failed", e);
        throw e;
    } finally {
        HibernateSessionFactory.closeSession();
    }
    return authors;
}
4

1 回答 1

0

您尚未共享将数据写入数据库的代码。没有它,我只能想到为什么您的数据输出是旧数据而不是新数据的几个原因:

  1. 您的交易未提交。
  2. 在您查询数据时,Hibernate 尚未写入数据库。
  3. Hibernate 的缓存没有更新,导致查询返回旧数据。

您应该使用 db 开发人员工具验证数据是否已写入数据库,并尝试禁用所有休眠缓存以查看结果是否更改。

于 2017-11-29T04:17:12.583 回答