How to use Transactions in stateless session bean?

H

harry

Totally confused! read article saying how easy it is using the
SessionSynx.... interface - ah can't coz only allowed in stateful beans!

So failing to find anything convincing on the web I'm not sure what to do
now?

I have a stateless session bean (EJB1 - acting as a session facade) that
calls methods on another stateless session bean (EJB2). I want to
encapsulate several EJB2 method calls within a single EJB1 method. This
method does some validation which might result in an exception being thrown
so obviously I'll need to rollback somehow

Do I have to create another session bean, this time make it stateful & put
all methods that require transactions in there? or can I still achieve this
using a stateless bean?

Many thanks - appologies if not correct wording but a bit new to this!
 
S

Sudsy

harry wrote:
Do I have to create another session bean, this time make it stateful & put
all methods that require transactions in there? or can I still achieve this
using a stateless bean?

Many thanks - appologies if not correct wording but a bit new to this!

Look at the container-transaction element nested within the
assembly-descriptor element which is nested in the ejb-jar
element. Here's a brief outline of an ejb-jar.xml file:

<?xml version="1.0"?>
<!DOCTYPE ejb-jar PUBLIC
"-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN"
"http://java.sun.com/dtd/ejb-jar_2_0.dtd">
<ejb-jar>
<enterprise-beans>
<session>
<ejb-name>MyBean</ejb-name>
<home>com.mine.MyBeanHome</home>
<remote>com.mine.MyBean</remote>
<ejb-class>com.mine.MyBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>MyBean</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>

Looks like you have some reading ahead of you! ;-)
 
H

harry

Thanks Sudsy, I understand that bit but am still confused as to how to use
transaction in stateless session beans? i.e how to start, rollback & commit
them in my code?

cheers

harry
 
M

Michael Borgwardt

harry said:
Thanks Sudsy, I understand that bit but am still confused as to how to use
transaction in stateless session beans? i.e how to start, rollback & commit
them in my code?

You don't. The container does it for you. Basically, a transacion is started
when the method is called, it is committed when the method completes,
and it's rolled back when the method throws an EJBException or when you call
EJBContext.setRollbackOnly().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top