Can I call a Stateful Session Bean through a Stateless Session Bean? Is the client of the SFSB then automatically the client of the SLSB for state preservation purposes? The problem is that because of the design being set in stone, I am not easily allowed to call the SFSB directly.
Call a Stateful Session Bean through a Stateless Session Bean
ejb-3.0
Related Solutions
Even though I'd still like to know why my original setup it works perfectly for stateless session beans but not for stateful session beans, here is the solution I found:
The web-application that is also packaged in the EAR does its authentication via JAAS. For this I have configured a security domain in the JBoss login-config.xml, which looks like this:
<application-policy name="My-SD">
<authentication>
<login-module code="org.jboss.security.auth.spi.DatabaseServerLoginModule" flag="required">
<module-option name="unauthenticatedIdentity">guest</module-option>
<module-option name="dsJndiName">java:/myDS</module-option>
<module-option name="principalsQuery">SELECT PASSWORD FROM LOGIN WHERE LOGIN = ? AND STATUS > 0</module-option>
<module-option name="rolesQuery">SELECT ROLE, 'Roles' FROM USER_ROLE WHERE LOGIN = ?</module-option>
</login-module>
</authentication>
</application-policy>
I have used this security domain in the web-application's jboss-web.xml as well as in the EJB-project's jboss.xml, even though I actually only use it in the web-app (the EJBs are accessible without authentication).
To solve the problem with accessing the SFSB, I just had to remove my security domain from the jboss.xml in the EJB-project. This then makes JBoss use the default security domain which seems to do the right thing and my application client finally can access and use the SFSB.
I'm learning ejb3 and I'm just curious when it's convinient to use SFSB? I can't find any nice example when SFSB realy solve easily some complex problem.
You mean like a shopping cart? That's the obvious answer that I can think of.
Actually I see that SLSB can be used as web-services and this is convenient.
You can think of EJBs as one way to deploy distributed services, but be careful. The term "web services" makes most people think of "SOAP-based web services using HTTP protocol", and that's not what you have in a SFSB.
But I don't know when to use SFSB. I see only problems with it because we should learn something about it, we should write code which consists of annotations a bit less then completely, we should use annoying lookup.. And we don't get anything good in return.
This paragraph is confusing, but I think you're saying you don't like EJBs much.
For example, we can't use SFSB from SLSB because stateful objects can be used only from stateful context.
Right, they're complementary. You use SFSB for use cases that require - wait for it - state to be maintained between calls.
We can't use DI in servlets, instead of it we should manually create SFSB instance using lookup and then put it to HttpSession object. It can't be a web-service.
Where did servlets come from here?
The only profit I can see in SFSB is a transaction management. But I think it's rare case when we really need a transaction and we don't need DB. I can suppose that it can be realy useful when we store our data in xml-file and use transaction management in SFSB to simulate nonrelational DB.
I think you're totally off base here. Sessions beans are the ones that know about units of work and managing transactions. They probably have to work with entity beans to persist some of that state when the use case is done, so transactions aren't as uncommon as you seem to think.
I'm almost sure that I'm totally wrong, so give me some realy nice examples of SFSB usage.
What's your expectation? That someone will post working SFSB? I'm not going to do that, mostly because I'm not a big EJB fan. (I do everything you're alluding to and more with Spring.)
But rest assured that SFSB are sometimes useful. The shopping cart is the obvious example. You need a place to maintain items in the cart until the customer decides to purchase. SFSB is one way to accomplish that.
Best Answer
If you call a Statful session beans inside a business method, no problem.
Correct: if you call context.lookup, YOU HAVE TO DECLARE MyStateful through @EJB annotation as shown abobe.
Advice: it does not makes sense call a stateful inside a stateless.
regards,