Gopal00005
Gopal00005

Reputation: 2151

Why Hibernate session is not reflecting changes done outside application

I am developing a Java Application which rapidly checks for updates in client database table and if any update is found then transfer it to the server database using Hibernate and MySQL.

  1. It works well when we changes database using hibernate session BUT whenever I change the database table value using MySQL Workbench then it will return the records which are as before.

How can I get updated with hibernate session on outside changes in table.

public List<com.ctpl.models.client.MasterTable> getAllClientMasterTableWithServerFlag() {
    try {
        clientSession = ClientHibernateUtil.getSessionFactory().openSession();
        clientSession.flush();

        Criteria criteria = clientSession.createCriteria(com.ctpl.models.client.MasterTable.class);
        criteria.add(Restrictions.eq("serverFlag", 0));

        List<com.ctpl.models.client.MasterTable> clientMasterTables = criteria.list();
        clientSession.close();
        return clientMasterTables;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

What am i missing here ?

Upvotes: 3

Views: 1550

Answers (2)

Gopal00005
Gopal00005

Reputation: 2151

We just need to add connection pooling in hibernate like following...

  1. C3P0 (Easy to implement)
  2. BonCP
  3. Tomcat JDBC Connection Pool
  4. DBPool
  5. HikariCP
  6. Proxool

Found best way to implement it easily using this example

Upvotes: 1

Godwin
Godwin

Reputation: 522

Have a look at Session.clear() or evict method

Refer to this article:

http://howtodoinjava.com/2013/07/01/understanding-hibernate-first-level-cache-with-example/

Hope that helps

Upvotes: 2

Related Questions