Hello. I am writing a java program using Hibernate. The code below, should refer once every few seconds to a table to check if there is new data with the state = 0 column.

class Dao { public List<MyTable> getNewRows() throws Exception { Session session = null; List<MyTable> result; try { session = HibernateUtil.getSessionFactory().openSession(); result = session.createQuery("from MyTable where state = 0 ").list(); } catch (Exception e) { throw e; } finally { session.close(); } return result; } } 

It should work like this: I expose some rows in the table with a third-party program state = 0 and the method should pick them up, because it is called in a cycle, with a delay of 3 seconds. But the method picks up only those lines that were set to status 0 before starting the program, but if you set up statuses while the program is running, then the method no longer “clings”, that is, it does not see that new lines have appeared with status 0.

I tried to use StatelessSession instead of Session, and after the HQL request I tried to add session.setCacheMode (CacheMode.IGNORE), but it did not help.

here is util

 public class HibernateUtil { private static SessionFactory sessionFactory; public static SessionFactory getSessionFactory() { if (sessionFactory == null) { // loads configuration and mappings Configuration configuration = new Configuration().configure(); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); // builds a session factory from the service registry sessionFactory = configuration.buildSessionFactory(serviceRegistry); } return sessionFactory; } 

}

here is cfg

  <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/db</property> <property name="hibernate.connection.username">login</property> <property name="hibernate.connection.password">pass</property> 

In addition to these lines in cfg There is nothing else.

Help me figure out why this is happening.

  • Show how the loop looks like it does? Perhaps you do everything in a single transaction for the database. Enable hql logging in the log, show what goes there. - flybox
  • java.io.InvalidClassException: com.firmname.hibernate.entities.MyTable; local class incompatible: stream classdesc serialVersionUID = -5930977514948150750, local class serialVersionUID = 6205997936559234017 that's what logger writes is fantastic
  • I apologize, then the old entry, 4 hours old. And there is nothing new, the log is silent. - fantastic
  • in the hibernate settings there is such a function hibernate.show_sql = false, set it to true, see if the matching functions fall into the log. Does it even cycle every 3 seconds? - flybox
  • Look in sessionFactory. Is it possible to use getCurrentSession? sessionFactory is independently responsible for managing sessions. - flybox

1 answer 1

Understood with my problem.
In short, the replacement of the openSession () method with getCurrentSession () and commit at the end of the request helped.

But if everything is entirely:

In hibernate.cfg.xml we add such line.

 <property name="hibernate.current_session_context_class">thread</property> 

And Dao now looks like this

  class Dao { public List<MyTable> getNewRows() throws Exception { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.getTransaction().begin(); List<MyTable> result = session.createQuery("from MyTable where state = 0 ").list(); session.getTransaction().commit(); return result; } }