Spring JPA事务已提交,但db中没有记录

时间:2015-02-06 15:56:54

标签: spring jpa asynchronous runnable transactional

当我尝试在单独的线程中提交事务时,我遇到了事务问题。这是代码:

UserPhoneService.java
@Transactional
private void initializeAndSavePhoneNumberForUser(...){
....
userPhoneNumberRepository.save(phoneNumber);
        afterCommitExecutor.execute(new Runnable() {
            @Override
            public void run() {
                log.error("After commin");
                final String changeset = ChangesetFactory.createEmailChangeset(ChangesetFactory.ACTION_CREATE,
                        phoneNumber);
                changesetRevisionService.add(saaspassUser.getSaaspassId(), changeset, ChangesetCategoryEnum.PHONE);
            }
        });

提交事务后,程序从ChangeSetRevisionService调用add方法。这是代码

@Transactional
    public Integer add(final String saaspassUserId, final String changeset, final ChangesetCategoryEnum category) {


        System.out.println("Class name : " + this.getClass().getName());
        Integer latestRevision = 0;

        try {
            final SaaspassUser saaspassUser = saaspassUserRepository.findBySaaspassId(saaspassUserId);
            latestRevision = revisionRepository.findLatestRevision(saaspassUser.getId());
            if (latestRevision == null) {
                latestRevision = 0;
            }
            final Revision revision = new Revision();
            revision.setSaaspassUser(saaspassUser);
            revision.setChangeset(changeset);
            revision.setCategory(category);
            revision.setRevisionNumber(++latestRevision);
            log.debug("Going to insert new changeset [{}]", revision);
            final Revision save = revisionRepository.save(revision);      

            log.debug("New changeset inserted [{}]", revision, save);
        } finally {
            locks.unlock(saaspassUserId);
        }
        return latestRevision;
    }

问题是 - 最终版本save = revisionRepository.save(修订版); 执行保存方法但在db中没有记录。 任何的想法 ? 谢谢

0 个答案:

没有答案