我不是 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/