5

Is it allowed (and good practice) to hold some shared informations in our application by using a Singleton Session Bean inside a Stateless Session Bean?

The SSB will be injected into the SLSB.

@Stateless
public class MySLSB {

    @Inject
    MySSB mySSB;

-

@Singleton
@Lock(READ)
public class MySSB implements Serializable {

    private static final long serialVersionUID = 1L;
Gatschet
  • 1,337
  • 24
  • 38
  • Can you please present an example of how you actually use it in your code ? It will be helpful for me. – SacJn Aug 25 '15 at 06:47
  • 1
    As an example, we need some configuration in our application. This configuration is stored as entity in our DB and must be parsed before using. To avoid loading and parsing this configuration every time we need, we do i just one time and store the parsed config in the SSB as object. – Gatschet Aug 25 '15 at 06:50

2 Answers2

6

It is more than allowed. Using Singleton injections in your stateless or statefull EJBs would allow you to call business methods on your SSB in your SLSB. One of the trivial advantages is using SSB concurrent capabilities. In your example all of your method calls toward you SSB would be locked on Read and that means all your threads would be accessing your SSB methods on Read mode unless a thread is holding a lock on Write.

javadev
  • 1,639
  • 2
  • 17
  • 35
1

Yes, it is allowed and I think it is good practice. In projects I am working with there are stored global properties read from file. It is good practice because you store it in one place and if you need these information then you only have to inject your singleton bean. I think it is good example of hollywood principle.

wawek
  • 1,577
  • 1
  • 13
  • 23