java - ejb 中事务未回滚

标签 java transactions ejb-3.0

我不是 ejb 专家。我有一个如下所示的服务类。我将文件保存在服务类的某个位置,并调用 dao 中的方法来保存文件哈希码。由于某些原因,有时我在 dao 层遇到异常。最近我观察到当我得到异常(exception)时,从我的服务层保存的文件没有被删除。

@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事务管理? ejb事务管理只是为数据库事务设计的吗?

最佳答案

不建议在 EJB 中与文件系统进行交互。以下是 EJB Restrictions 的摘录这解释了它。

Why can't EJBs read and write files and directories in the filesystem? And why can't they access file descriptors?

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.

由于文件不是事务性资源,回滚不会对其产生影响。

关于java - ejb 中事务未回滚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22446916/

相关文章:

java - Redis中如何实现带回滚的事务

java - Hibernate 忽略 'lazy' 获取类型并立即加载属性

Java split() 在索引 0 处有空间

php - MySQLi 准备好的语句和事务

javascript - 以原子方式递增 Firebase 实时数据库中的共享计数器

jakarta-ee - session Bean的 session 状态

java - 一起使用 EJB3 和 Struts 2

hibernate - 抛出异常时回滚事务并关闭连接

java - PutApplicationName 与 PutApplName

java - 如何使用 drawString 在多行中重复其字符,然后在每行中添加另一个字符?