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.
Java EE 6 EJB tutorial : why not using @Singleton instead of @Stateless
質問
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 ?
解決
他のヒント
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.
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.