0

Am using Hibernate with MySQL Database in a web application project, mainly using JSF, after I change data in the database manually and committing, Hibernate doesn't detect these changes, and hibernate queries returns same old results, not until I restart the application, the changes becomes detectable. I disabled second level cache but still.

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;
        }
    }
}
Tiny
  • 27,221
  • 105
  • 339
  • 599
Exorcismus
  • 2,243
  • 1
  • 35
  • 68
  • A bit unrelated, but you might have an easier time getting help on StackOverflow and the internet if you start using Hibernate's JPA API (`EntityManager`) instead of the underlying Hibernate Session. The Hibernate devs recommend it themselves: "If we could somehow merge or deprecate the session in the future we would do it ..." ([ref](http://www.theserverside.com/news/2240186700/The-JPA-20-EntityManager-vs-the-Hibernate-Session-Which-one-to-use)). – DavidS Apr 13 '15 at 21:47
  • You could also try [refresh](http://stackoverflow.com/questions/19329338/hibernate-session-not-refreshing-data-from-db-after-initial-commit-failure). – DavidS Apr 13 '15 at 21:52
  • Its called caching and this is one of the gotcha's of working with Hibernate, EclipseLink etc. When caching is enabled and you make changes to the data outside the JPA like doing a CSV import using mysql command line interface the data wont be available to JPA immediately. Only when you import the data via JPA will it be available. Switch off the caching or refresh the cache is the only option. – Namphibian Apr 13 '15 at 22:39
  • @Namphibian, I disabled the cache using. false false – Exorcismus Apr 13 '15 at 22:46

1 Answers1

0

It seems it's something called Isolation level, after reading http://www.ovaistariq.net/597/understanding-innodb-transaction-isolation-levels/ and setting in hibernate cfg.xml, my problem was solved

<property name="hibernate.connection.isolation">1</property>
Exorcismus
  • 2,243
  • 1
  • 35
  • 68