JPA的逻辑提交

时间:2017-11-01 18:53:47

标签: java jpa entitymanager

我有两张桌子。 ActionLog& QueueLog表,当用户执行任何操作时,我正在插入动作表&更新队列表中的少量记录。

现在,有时候当用户执行任何操作时,只会插入操作表&队列表没有得到更新&我没有在日志中看到任何异常或错误。我想在其中任何一个失败时添加逻辑提交(第33行和第43行)然后应该回滚所有内容,意味着它不应该插入或更新任何表格

    EntityManager entityManager = null;
    entityManager = EntityManagerUtil.getEntityManager();
    Boolean isdone = false;

    try {

        ActionLog actionLog = new ActionLog();
        TbBamiActionLogPK actionLogPk = new TbBamiActionLogPK();

        entityManager.getTransaction().begin();
        QueueLog QueueLog = entityManager.find(QueueLog.class, refNo,
                LockModeType.PESSIMISTIC_WRITE);

        actionLogPk.setRefno(refNo);
        Integer currVerStr = Integer.valueOf(QueueLog.getCurrVersion().toString());
        Integer currVer = currVerStr + 1;

        LOG.info("AR-VERSION currVerStr" + currVerStr);
        LOG.info("AR-VERSION currVer" + currVer);

        actionLogPk.setVersion(currVer);
        actionLog.setId(actionLogPk);
        actionLog.setAction(action);
        actionLog.setPrevAssignee(assignedTo);
        actionLog.setPrevAssgneeRole(assignedToRole);
        actionLog.setAssignee(assignedTo);
        actionLog.setAssigneeRole(assignedToRole);
        actionLog.setComments(comments);
        actionLog.setEntryDateTime(new Timestamp(System.currentTimeMillis()));

        // Adding Action Log
        entityManager.persist(actionLog);//line 33
        LOG.debug("Inseted into action log");
        // Updating Queue Log
        QueueLog.setAction(action);

        QueueLog.setStatus("CLOSED");
        LOG.debug("Updating status as closed");
        QueueLog.setExitDateTime(new Timestamp(System.currentTimeMillis()));

        QueueLog.setCurrVersion(BigDecimal.valueOf(currVer));
        entityManager.merge(QueueLog);//line 43

        LOG.debug("Going to do commit");
        entityManager.getTransaction().commit();
        LOG.debug("Commited..");
        isdone = true;

    } catch (Exception e) {
        isdone = false;
        //entityManager.lock(QueueLog.class, LockModeType.NONE);
        LOG.error("ERR_MSG", e);
    } finally {
        // entityManager.lock(QueueLog.class, LockModeType.NONE);
        entityManager.close();
    }
    return isdone;

0 个答案:

没有答案