mardi 17 mai 2022

Usage of @Stateless and @Transactional on a POJO class - when and why?

I have a developer in my team who uses both of the annotations @Stateless and @Transactional on POJO classes that do not need any of these to work correctly. Example:

@Stateless
@Transactional(Transactional.TxType.REQUIRED)

public class ObjektAnlegenService {

    public Home setImagesToHome(Home home, ImagesUploadHelper imgUpldHelper) {
        Objects.requireNonNull(home, "Home is Null!");
        Objects.requireNonNull(imgUpldHelper, "ImagesUploadHelper is Null!");
        String mainImage = null;
        for (Image savedImage : imgUpldHelper.getImagesSaved()) {
            if (savedImage.isMainImage()) {
                mainImage = savedImage.getSimpleFileName();
            }
            savedImage.setHome(home);
        }
        home.setHomeImages(imgUpldHelper.getImagesSaved());
        home.setMainImageTabl(mainImage);
        return home;
    }

    public String[] getPlzOrtDetails(String plzOrtInput) {
    
        int indexOfComma = plzOrtInput.indexOf(Constants.COMMA);
        indexOfComma = indexOfComma == -1 ? plzOrtInput.length() : indexOfComma;
        
        String[] plzOrt = PlzOrtUtils.getPlzOrt(plzOrtInput.substring(0, indexOfComma));
        return plzOrt;
    }
}

My questions:

1.) @Transactional only makes sence, when the POJO class (or its methods) shall work with a transactional datasource, such as database or a transactional web-service. Otherwise it only adds a load to the server, right?

2.) @Stateless only makes sence when we want the JEE server container to manage the CDI (context and dependency injection) of the POJO class. But @Stateless adds an additional load to the JEE Server, because @Stateless beans are insantiated, pooled and kept in JNDI, right?

Then: if a POJO can exist and work as a singleton (as is the case with ObjektAnlegenService above), isn't it better to design it as a singleton, instead of a @Stateless?

Aucun commentaire:

Enregistrer un commentaire