Dan
Dan

Reputation: 2174

unable to get the correct data from database

I have written code for getting data from my database.

When users click a link then the below action is called. It gives me correct data for first time .But if admin updates the table data . Then the below code fails to show the newly updated data from the table. if i refresh the page many times then it shows newly updated data. But again after next refresh it shows the old data.

i am very sure the problem is with the dao or may be in Action class. I don't know what is the problem behind this? Please help me to solve this issue.

BookdetailsDAO.java

    public   List findasc(int o,int l)
{
     List list = null;

        try {
             session = HibernateUtil.getSessionFactory().openSession(); 
             Query q1 = session.createQuery("FROM Bookdetails  ORDER BY authLastname ASC");
             q1.setFirstResult(o);
             q1.setMaxResults(l); 
            list =q1.list(); 
        } catch (Exception e) {
            System.out.println("Exception in  findasc()  :" + e);
        } finally {
            try {
                 session.flush();
                session.close();
            } catch (Exception e) {
                System.out.println("Exception In findasc Resource closing  :" + e);
            }
        } 
        return list; 
}

Upvotes: 1

Views: 183

Answers (2)

Dan
Dan

Reputation: 2174

Problem solved by applying this code

sessionFactory.close(); tx.commit();
session.close();

Upvotes: 0

sorencito
sorencito

Reputation: 2625

Try adding a dedicated commit instead of the flush. Probably Hibernate does not commit instantly when you close the session.

You should also try reading all data in one transaction. According to your code, you open a session for every single call to the DAO. Re-use the session for calls that belong together.

I strongly recommend letting your infrastructure do the session opening, closing and committing for you. Consider using an annotation driven approach e.g.

Upvotes: 1

Related Questions