【问题标题】:JPA : Transaction rollback not workingJPA:事务回滚不起作用
【发布时间】:2018-05-03 16:49:35
【问题描述】:

我的代码实际上很大,所以这里是代码。 我尝试在一个事务中将一组文件的流存储到数据库中,并且该事务已提交。

在我使用相同的事务后不久,我重写了数据库中的文件流和其他数据。所以我再次开始同一个事务并在这个过程完成后提交它。但是在出现异常的情况下,事务不会回滚,并且数据库会使用新值进行更新。这可能是什么原因?

请提前帮助和感谢。

try {
            Map<String, String> constants = AppConstants.getAppConstants();
            String hrEmailIds = constants.get(FileUploadDownloadConstants.HR_EMAIL);

            entityManager.getTransaction().begin();
            fileDao.setEmployeekeychangestatus(employeeID, KeyChangeJobStatus.STARTED.getValue());
            entityManager.getTransaction().commit();
            entityManager.flush();

            setOldFileStreams(employeeID);

            List <Integer>tempFileIds = fileDao.getTempFileIds(employeeID);
            entityManager.clear();

            KeyPair keyPair = cryptographyHelper.getKeyPairs();
            PublicKey newPublicKey = keyPair.getPublic();
            PrivateKey newPrivateKey = keyPair.getPrivate();
            PrivateKey oldPrivateKey = job.getPrivateKey();
            Tempfile tempFile;
            ByteArrayOutputStream fileEncryptedWithNewKey;

            for(int tempFileId : tempFileIds)
            {
                tempFile = fileDao.getTempFileById(tempFileId);
                fileEncryptedWithNewKey = decryptAndEncryptStream(tempFile, oldPrivateKey, newPublicKey);

                absolutePath = tempFile.getFilemetadata().getFilepath();
                path = absolutePath.substring(0,absolutePath.lastIndexOf(File.separator));
                fileDao.storeFile(fileEncryptedWithNewKey, path, tempFile.getFilemetadata().getFilename());
                fileCompletionCount++;
                fileEncryptedWithNewKey.close();
                entityManager.clear();

                intkeyChangeProgress = findPercentage(fileCompletionCount,numberOfFiles,FileUploadDownloadConstants.PRECENTAGE_TO_UPDATE_FILE_STREAM);
                intkeyChangeProgress += FileUploadDownloadConstants.PRECENTAGE_TO_SET_OLD_FILE_STREAMS;
                EmployeeOperationStatus.putEmployeeKeyChangeProgress(employeeID,intkeyChangeProgress);
            }

            intkeyChangeProgress = findPercentage(fileCompletionCount,numberOfFiles,FileUploadDownloadConstants.PRECENTAGE_TO_UPDATE_FILE_STREAM);
            intkeyChangeProgress += FileUploadDownloadConstants.PRECENTAGE_TO_SET_OLD_FILE_STREAMS;
            EmployeeOperationStatus.putEmployeeKeyChangeProgress(employeeID,intkeyChangeProgress);

            entityManager.getTransaction().begin();

            fileDao.deleteTempFilesForEmployee(employeeID);


            ByteArrayOutputStream byteEncryptedPrvateKey = encryptKey(employeeID, newPrivateKey);
            constants = AppConstants.getAppConstants();
            String privateKeyPath = constants.get(FileUploadDownloadConstants.BASE_PRIVATE_KEY_FILE_PATH);

            employee = fileDao.getEmployee(employeeID);
            keyDao.updatePublicKey(employee, newPublicKey);

            fileDao.removeEmployeekeychangestatus(employeeID);

            fileDao.storeFile(byteEncryptedPrvateKey, privateKeyPath, employee.getUserid()+FileUploadDownloadConstants.PRIVATE_KEY_EXTENSION);
            privateKeyByteArray = cryptographyHelper.getPrivateKeyBytes(newPrivateKey);
            mailingService.sendMailForKeyNotification(employee.getEmail(), hrEmailIds, privateKeyByteArray, 
                    employee.getUserid()+FileUploadDownloadConstants.PRIVATE_KEY_EXTENSION, FileUploadDownloadConstants.CHANGE_KEY);

            entityManager.getTransaction().commit();
            entityManager.clear();

        } catch (EmprisException e) {
              e.printStackTrace();
              entityManager.getTransaction().rollback();

        } catch (Exception e ) {
             e.printStackTrace();
             entityManager.getTransaction().rollback();
        }
    }

【问题讨论】:

    标签: java jpa entitymanager rollback


    【解决方案1】:

    您打开了两个单独的交易。 当第二个事务引发异常时,第一个异常不会回滚,它已经提交了。

    【讨论】:

    • 实际上,当我在第二个事务块中遇到异常时,第二个事务并没有回滚
    • 你能发布一些堆栈跟踪吗?
    • 实际上堆栈跟踪与实体管理器完全无关。我通过更改数据库中某个字段的值来制作这个场景。
    猜你喜欢
    • 1970-01-01
    • 2015-10-25
    • 2011-01-17
    • 1970-01-01
    • 2012-02-04
    • 1970-01-01
    • 2023-02-23
    • 2015-10-18
    • 2013-05-08
    相关资源
    最近更新 更多