1

I am developing an application with Spring MVC , Hibernate and database MySQL Controller handle the request and response. Hibernate handles the database transaction.

My problem is when one or two access the service it works fine, but for more than that work fine for some time, but after that I get the error frequently lock time out

//My sample Controller code

 @RequestMapping(value = "Bank", method = RequestMethod.GET)
    public ResponseEntity<List<Bank>> getAllBank(@RequestHeader int data) {
        try {
            //My DAO implementation class for bank table
            bankdao = new BankDAOImpl();
            List<Bank> bank = bankdao.getAllBank(data);

            return new ResponseEntity<List<Bank>>(bank, HttpStatus.OK);
        } catch (HibernateException he) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

//My DAO implementation code

public List<Bank> getAllBank(int organizationId) throws HibernateException {
//I'm opening session in every function
Session session = SessionFactoryUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            Criteria criteria = session.createCriteria(Bank.class);
            criteria.add(Restrictions.eq("organizationId", organizationId));
            criteria.add(Restrictions.eq("deleteFlag", false));
            criteria.addOrder(Order.asc("bankName"));
            List<Bank> ls=criteria.list();
            return ls;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            session.getTransaction().commit();
            session.close();
        }

That session causing the problem If I create a separate session factory for all functions instead of open a session

Will it solve my problem?

5
  • 1
    try to use, @Autowired BankDAOImpl bankDAOImpl, instead of creating new instance Commented Dec 25, 2015 at 13:45
  • Could you paste the actual stack trace for the lock time out exception? Additionally, are you performing updates elsewhere in code you haven't posted or by any other means (e.g. in a mysql database client)? Commented Dec 25, 2015 at 15:22
  • do you have nested calls of your dao methods? When one dao method call other? How you configure hibernate datasourse? Commented Dec 25, 2015 at 15:23
  • @Ben Rowland simultaneously many users will insert, update ,delete Commented Dec 25, 2015 at 16:59
  • @user1516873 yes in some functions Commented Dec 25, 2015 at 16:59

1 Answer 1

2

You shouldn't create a separate session factory for every method. Just open a session. May be a problem is your incorrect way of working with transactions. You should do it this way

public List<Bank> getAllBank(int organizationId) throws HibernateException {
    //I'm opening session in every function
    Session session = SessionFactoryUtil.getSessionFactory().openSession();
    Transaction tx = null;
            try {
                tx = session.beginTransaction();
                Criteria criteria = session.createCriteria(Bank.class);
                criteria.add(Restrictions.eq("organizationId", organizationId));
                criteria.add(Restrictions.eq("deleteFlag", false));
                criteria.addOrder(Order.asc("bankName"));
                List<Bank> ls=criteria.list();
                tx.commit();// commit here
                return ls;
            } catch (Exception e) {
                e.printStackTrace();
                if (tx != null) {
                  tx.rollback();// rollback here
                } 
                return null;
            } finally {
                session.close();
            }
    } 

May be a problem is your SessionFactoryUtil. It would be interesting to see it.

Sign up to request clarification or add additional context in comments.

3 Comments

sessionFactoryUtil is a class where i have the sessionFactory configuration binding with that cfg file and buliding session
@Sreemat I see. I hope you build a session factory only once.
yes but opening session in all functions.I found one possibility to hibernatecp3 jar to increase the connection pool it helps to increase the performance @v.ladynev

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.