Wednesday, March 01, 2006

Don't use resource injection for anything stateful in Java EE 5 !

EJB3 has an impressive feature - a resource injection. You can mark class attribute by annottation @Resource or @EJB and application server will inject requested resource at runtime. So far so good, but imagine servlet that uses stateful EJB. When you run this servlet in single-threaded model then there is only one instance of servlet with only one injected resource - so as the result, everyone who calls the servlet uses just one stateful bean ! And mostly, this is not what you want allow, mostly you want bind one servlet session to one stateful bean. The same trouble is for example with @PersistenceProvider. However in this case things are even complicated - your application should run smoothly but from time to time you'll experience serious troubles concerns threading and transactions. This is a side effect of fact that @PersitanceManager is not thread safe and can't manage multiply transactions.
Do you want to know more ? Click here !

No comments: