0

I looking at one EJB sample from the Java EE 6 tutorial. In This example, I want to know if I could just use @Singleton instead of @Stateless ?

package converter.ejb;

import java.math.BigDecimal;
import javax.ejb.*;

    @Stateless
    public class ConverterBean {

        private BigDecimal yenRate = new BigDecimal("83.0602");
        private BigDecimal euroRate = new BigDecimal("0.0093016");

        public BigDecimal dollarToYen(BigDecimal dollars) {
            BigDecimal result = dollars.multiply(yenRate);
            return result.setScale(2, BigDecimal.ROUND_UP);
        }

        public BigDecimal yenToEuro(BigDecimal yen) {
            BigDecimal result = yen.multiply(euroRate);
            return result.setScale(2, BigDecimal.ROUND_UP);
        }
    }

Looks to me like a Util methods.

I could have use a static methods on this ConverterBean, if I wasn't using EJB.

And another question. I know that is a simple sample, but if I use this code from a servlet like in the sample, why using EJB only for this ?

1
  • For the second question, can you please elaborate it?I don't see your mean Apr 19, 2013 at 14:07

3 Answers 3

1

Yes, I would say this is a bad use case for EJBs at all. This methods should be definitely in some utility class declared as static methods. And for the Singleton, don't use if you don't need it (of course, you could make this class Singleton but what would you gain?). Keep your code as simple as possible.

0
1

If you just use @Singleton instead of @Stateless, you would lower app performance. Because, there are two ways to access @Singleton beans, container-managed and bean-managed. Default is container-managed and default Lock is LockType.WRITE. So only one client can access to the bean at the same time, others would be blocked.

1
  • thanks. I was expecting something like that if I used @Singleton. I was interessting to know if I could do remplace stateless by singleton, so the answer is yes, and it was a bad usecase to use EJB that was what I want to heard. Apr 19, 2013 at 15:10
0

I really don't see the reason of using an EJB in if I need just a class of utilities. I think you could use it as a simple final class of utilities and use static methods in it. You would use an EJB if you really need to benefit from container transactions, use of JPA, some remote services or something like that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.