0

When we call transaction.rollback(), Hibernate rolls-back the database transaction. Database handles rollback, thus removing newly created object. But I am not able to understand if we should use rollback in catch block even if we have only one row insertion or updation in a single table.

For example this Code, the procedure is written only to write/update data in one table:

public Map<String, Object> updateSOSDetails(SOSUserDetails sosUserDetails) {
    Map<String, Object> result = new HashMap<>();
    result.put(Constants.RESULT, false);
    Session session = null;
    try {
        Map<String, Object> output = null;
        session = cityAppSessionFactory.openSession();
        Query query = session.createNativeQuery("EXEC update_sos_details :userId,:contactName1,"
                        + ":contactNumber1,:contactRelation1,:contactName2,:contactNumber2,:contactRelation2");             
        query.setParameter(Constants.USERID, sosUserDetails.getUserId());
        query.setParameter("contactName1", sosUserDetails.getContactName1());
        query.setParameter("contactNumber1", sosUserDetails.getContactNumber1());
        query.setParameter("contactRelation1", sosUserDetails.getContactRelation1());
        query.setParameter("contactName2", sosUserDetails.getContactName2());
        query.setParameter("contactNumber2", sosUserDetails.getContactNumber2());
        query.setParameter("contactRelation2", sosUserDetails.getContactRelation2());
        session.beginTransaction();
        query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        output = (Map<String, Object>) query.uniqueResult();
        session.getTransaction().commit();
        result.put(Constants.RESULT, true);
        result.put(Constants.OUTPUT, output);
    } catch (Exception e) {
        logger.error("::::::Exception in SOSDaoImpl updateSOSDetails : " + e.getMessage() + e );
        Constants.sessionRollBack(session); 
    } finally {
        Constants.closeSession(session);
    }
    return result;
}

Is the Constants.sessionRollBack(session) necessary in this methods case?

Shivakumar N.R
  • 170
  • 1
  • 12
  • seen to me that your query is executing stored procedure, if it is than it should be necessary to rollback data in db when exception happen – Erwin May 27 '20 at 17:00
  • @Erwin The stored procedure only writes or updates on one row in the table, no joins with other table and all, should I still use? – Shivakumar N.R May 28 '20 at 05:36
  • is there a possibility when an exception happen but the stored procedure is still executed succesfully (so data is updated in db) ? when there is a possibility like that, it should be necessary to rollback data. or u may have any thought that it will not be necessary to roll back when case like that is happening? – Erwin May 28 '20 at 06:56
  • I did ask a couple of friends about this, and as per there suggestion there is no necessity of a rollback when you are adding data to only one row without any other dependent tables, and looking at my code there is hardly any chance of an exception before the commit is called, hence it was not necessary for a rollback in this method. – Shivakumar N.R May 28 '20 at 07:44

0 Answers0