如何在递归调用中使Hibernate Transaction对象

时间:2019-05-10 07:17:07

标签: spring hibernate spring-mvc

当我递归地调用一个方法时,我的控制台出现错误。更新查询运行正常,但不会更新数据库中的记录。

org.springframework.transaction.TransactionSystemException:无法提交休眠事务。嵌套的异常是org.hibernate.TransactionException:事务未成功启动

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 个答案:

答案 0 :(得分:0)

递归在哪里? 回滚。

答案 1 :(得分:0)

您可以尝试这样的事情

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

它应该可以帮助您更好地理解问题。