I am getting below error in my console when I call recursively a method. The update query is running fine but it will not be updating record in the database.

org.springframework.transaction.TransactionSystemException: Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction not successfully started

public boolean abcMethod() {
    Transaction txn = session.beginTransaction();
    String querySqlSold = "UPDATE abc_table SET inventory_type='SOLD', status='ACTIVE' where set_id="
            + setId + " and game_num=" + gameMaster.getGameNum() + " and priceScheme=" + prizeSchemeId;
    SQLQuery querySold = session.createSQLQuery(querySqlSold);
    querySold.executeUpdate();

    String querySqlSelect = "SELECT set_id FROM abc_table where inventory_type='UPCOMING' and `status`='ACTIVE' and game_num="
            + gameMaster.getGameNum() + " and priceScheme=" + prizeSchemeId;
    List list = session.createSQLQuery(querySqlSelect).list();
    int newSetId = Integer.valueOf(list.get(0).toString());

    if (newSetId != 0) {
        String querySqlCurrent = "UPDATE abc_table SET inventory_type='CURRENT' where game_num="
                + gameMaster.getGameNum() + " and priceScheme=" + prizeSchemeId + " and set_id=" + newSetId;
        SQLQuery queryCurrent = session.createSQLQuery(querySqlCurrent);
        queryCurrent.executeUpdate();
        txn.commit();
        return true;
    } else {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("errorCode", "809");
        jsonObject.put("errorMsg", "finished");
        throw new CustomException(jsonObject.toString());
    }

public void xyzMethod() {
    abcMethod();
    abcMethod();
}

2 Answers

0
Community On

Where is recursion here? Do rollback.

0
Sandeep On

You might try something like this

    try {
      tx = session.beginTransaction();
      if (!tx.wasCommitted()){
        tx.commit();
      }
    } catch (Exception exp) {
      tx.rollback();
    }

It should help you understand the problem better.