Question

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;
    }

}

Below id my DAO

public class UpgradePackageDaoImpl implements UpgradePackageDao {

    @Override
    public void savePackageHash(String hash) {

        throw new RuntimeException("cannot save");
    }

}

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?

Was it helpful?

Solution

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

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top