事务未在ejb中回滚 [英] Transaction is not rolled back in ejb

查看:103
本文介绍了事务未在ejb中回滚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是ejb专家。我有一个如下的服务课程。我将文件保存在服务类中的某个位置,并在dao中调用一个方法来保存文件哈希码。由于某些原因,我有时在dao层中遇到异常。最近,我发现从服务层保存的文件在被排除后不会被删除。

I am not an ejb expert. I have a service class like below. I am saving a file in some location in my service class and calling a method in dao to save the file hash code. Due to some reasons some time I get an exception in my dao layer. Recently I observed the file which is saved from my service layer is not deleted when I get excxeptions.

@Stateless
@Local
@TransactionManagement
public class ImportUpgradeServiceImpl implements ImportUpgradeService {

    @Inject
    private UpgradePackageDao upgradePackageDao;

    @Override
    public boolean savePackage() {
        //For the sake of simplicity I simplified the code here
        File file = new File("d:\\ejbtest.log");

        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

        upgradePackageDao.savePackageHash(null);

        return false;
    }

}

在我的DAO下方

public class UpgradePackageDaoImpl implements UpgradePackageDao {

    @Override
    public void savePackageHash(String hash) {

        throw new RuntimeException("cannot save");
    }

}

然后,我用@TransactionManagement。我想念什么?还是我误解了ejb交易管理?

Then I annotated my service class with @TransactionManagement. What am I missing? Or am I misunderstanding ejb transaction management? Is ejb transaction mamangement designed only for database transactions?

推荐答案

EJB中不建议与文件系统进行交互。下面是 EJB限制的摘录,对此进行了解释。

Interaction with the file system isn't recommended within EJB. Below is the excerpt from the EJB Restrictions which explains it.


为什么EJB无法在文件系统中读写文件和目录?
为什么它们不能访问文件描述符?

不允许企业bean访问文件的主要原因是因为
文件不是交易资源。允许EJB访问文件
或文件系统中的目录,或者使用文件描述符,将使
损害组件的可分发性,并且会带来安全隐患。

Enterprise beans aren't allowed to access files primarily because files are not transactional resources. Allowing EJBs to access files or directories in the filesystem, or to use file descriptors, would compromise component distributability, and would be a security hazard.

由于文件不是事务性资源,因此回滚对其没有影响。

As file isn't a transactional resource, rollback will have no effect on it.

这篇关于事务未在ejb中回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆