在POJO类中使用@Stateless和 @transactional-何时何时?

发布于 2025-01-29 12:13:15 字数 1480 浏览 3 评论 0原文

我的团队中有一个开发人员,他在POJO类中使用了@Stateless @StaTactional的注释,这些注释不需要这些都可以正常工作。 示例:

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

我的问题:

1。) @transactional仅在POJO类(或其方法)应与交易数据源(例如数据库或交易网络服务)一起使用时才能使人变得更加明智。否则,它仅向服务器增加负载,对吗?

2.) @stateless只有在我们希望JEE服务器容器管理POJO类的CDI(上下文和依赖项注入)时才能使人发呆。但是 @Stateless为JEE服务器增加了一个额外的负载,因为@stateless bean被毫不掩饰,合并并保存在JNDI中,对吗?

然后:如果Pojo可以存在并作为单身人士工作(上面的objektanlegenservice就是这种情况),将其设计成单身人士而不是@stateless不是更好吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小红帽 2025-02-05 12:13:15

@stateless将类标记为无状态会话EJB。因此,它会生存并由EJB容器(因此,正确,池,JNDI等)管理。 CDI容器也可以看到EJBS会话EJB,并可以使用@Inject进行注入,但是负责它们的EJB容器。因此,这并不完全像您所说的那样:“

因此,要回答是否要用@Stateless注释的类问题,您需要决定是否需要这些服务。如果不是,@ ApplicationsCoped

@transactional指示 cdi容器激活此组件的交易。 EJB默认情况下,ejb将交易量@transactional 注释是冗余的

。划分业务层的最高交易,只有在需要的地方,但这只是我的意见

。我的意思是? html“ rel =” nofollow noreferrer“> javax.inject.singleton ,不是 javax.ejb.singleton ,这是EJB。如果是这样,请记住javax.inject.singleton定义了a pseudo-scope ,因此,bean不符合拦截条件。由于@transactional通过定义拦截器来起作用,因此它不能适用于@javax.inject.inject.singleton beans。这就是为什么我在上面提到了@ApplicationsCoped。这实际上是单身人士(如单胎模式),但有资格进行拦截,因此CDI容器的所有预期设施都适用。

@Stateless marks the class as a stateless session EJB. As such it lives and is managed by the EJB container (so, right, pools, JNDI etc). Session EJBs are also visible to the CDI container and available for injection with @Inject, but it is the EJB container that is responsible for them. So, this is not exactly as you say "

So, to answer the question whether you want the class annotated with @Stateless, you need to decide whether you need those services. If not, @ApplicationScoped will do.

@Transactional instructs the CDI container to activate transactions for this component. If you decide that this component needs to be an EJB, remember that the EJB container activates transactions by default to session EJBs. So, the extra @Transactional annotation is redundant.

You may add transaction demarcation annotations in every method to explicitly state that it needs to be transactional. I would prefer to demarcate the transactions at the top level of the business layer and only where needed, but this is only my opinion.

EDIT-CLARIFICATION: Would a singleton POJO be better? By singleton POJO I assume you mean annotating the class with javax.inject.Singleton, not javax.ejb.Singleton, which is an EJB. If so, bear in mind that javax.inject.Singleton defines a pseudo-scope and as such the bean is not eligible for interception. Since the @Transactional annotation works by defining an interceptor, it would not work for @javax.inject.Singleton beans. That is why I mentioned @ApplicationScoped above. This is effectively a singleton (as in the singleton pattern), but is eligible for interception, so all expected facilities of the CDI container apply.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文