views:

1296

answers:

3

I need to implement a simple DAO bean, which also caches data read from the database. This DAO bean is used by stateless beans.

Because EJB3.0 doesn't support singleton beans, what would be the correct way to implement such a DAO bean? Is it ok to just use static variable to keep the cache?

We are using Oracle AS and not going to use clustering.

+2  A: 

Most Application servers and JPA implementations offer some sort of built-in caching mechanism which is controllable by the user. It might be worthwhile investigating ways of accomplishing what you want without having to build a singleton.

One of the major issues with singletons and J2EE is that there is no easy way to handle them in a clustered environment.

That all said, I did find an article which makes some suggestions for possibly doing what you want under JBoss, and mentions a new @Singleton annotation for EJB3.1 so that may be a possibility. If that works for you, you should write up what you did as another answer to this question.

Adam Batkin
We are using EclipseLink caching already, however we have a couple of cases where it's not sufficient and we have to manually implement caching
tputkonen
A: 

If you are running on Weblogic server, it is actually possible to implement a singleton bean that is also cluster-aware. I don't believe this feature exists on other application servers though.

jm04469
A: 

The "correct" way to do this in a somewhat cross-appserver way is to use a jmx mbean for the caching behavior. mbeans are singletons (per-app server) and can do things like threading and locking. depending on the appserver you use, of course, mbeans can be more or less difficult to work with in practice.

james