0

I'm unable to get my query to show updated results, I've tried various options and solutions to other questions on this and other forums but nothing as yet works.

The only way I can get the updated results is by relaunching my app.

Notes:

  • I'm running each query in a new session (and have confirmed the session is new).
  • I'm editing the data externally so there's no transaction to commit (and confirmed the data is updated).

The Hibernate properties I've tried (from various answers to other questions):

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

The session factory code (non-relevant code removed for brevity):

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

The open session code:

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

The query code:

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;
}
tw332
  • 47
  • 3
  • 11

2 Answers2

0

I got this issue in the past btw and solve it flushing the session. If it is not your issue have a look in this post, maybe can help you: Hibernate reading function shows old data

Community
  • 1
  • 1
fct
  • 311
  • 3
  • 18
  • I've tried various options from that post (before posting my own question). None of the isolation levels have any effect and flushing the session won't matter as each query is running in it's own session. – tw332 Mar 22 '15 at 21:29
  • Due to your answer, I've now worked out why this is happening. As I was testing combinations of the properties in hibernate.cfg.xml... I've established that they weren't applied to the session... Adding the properties in manually to my session factory code works. – tw332 Mar 22 '15 at 22:06
  • You can inspect your config object (manually vs cfg.xml) and see the differences between them – fct Mar 23 '15 at 03:53
0

The Hibernate properties specified in my question weren't being applied to the session. My query now returns updated data after manually adding the properties in my session factory method...

Which now includes:

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");
tw332
  • 47
  • 3
  • 11