aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Transaction handling with Stateless SessionBean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Transaction handling with Stateless SessionBean" Watch "Transaction handling with Stateless SessionBean" New topic
Author

Transaction handling with Stateless SessionBean

shalindra Singh Suryvanshee
Greenhorn

Joined: Aug 05, 2008
Posts: 25
Hi,

I am using stateless session bean in my application (since I do not require to save state). I am calling my DAO ,methods from the bean. In some cases multiple interaction with databse are happening in DAO method, for Example,

1. Table X1 is updated for certain 'where' criteria.
2. Records from Table X2 is deleted for same 'where' criteria.
3. new updated record is/are inserted into Table X2 for same primary key for which data had been deleted.

In such case if step 2 or 3 fails then whole transaction should rollback.
I used bean as container managed (so I can not make con.setAutoCommit(false), can not handle transaction manually and I do not want if container can handle by it's own).

I am using App server as JBoss 4.0.5 and making connection using initialContext.lookup("aaa") where aaa is JNDI name mentioned in JBOSS file.

My doubt is, could it be possible to manage the transaction with using stateless session bean. If not then how with Stateful session bean (I may use stateful as well)

Thanks
Shalindra


Regards
Shalindra Singh
SCJP, SCWCD
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
Study the details of ejb-jar.xml and discover what the container-transaction and trans-attribute elements do for CMT.
[ August 21, 2008: Message edited by: James Clark ]
shalindra Singh Suryvanshee
Greenhorn

Joined: Aug 05, 2008
Posts: 25
Thanks for suggestion!
I gone through it.

I could very very new to EJB's
I am sending you detail of code, if you can help me why it is not working !

"ejb-jar.xml"

<ejb-jar>
<enterprise-beans>
<session>
<ejb-name>SupplierDetailsEJB</ejb-name> <home>com.ejb.functionalmgr.supplierdetails.SupplierDetailsHome</home><remote>com.ejb.functionalmgr.supplierdetails.SupplierDetailsRemote</remote>
<ejb-class>com.ejb.functionalmgr.supplierdetails.SupplierDetailsBean</ejb-class>
<session-type>Stateful</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>SupplierDetailsEJB</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Supports</trans-attribute>
</container-transaction>
<container-transaction>
<method>
<ejb-name>SupplierDetailsEJB</ejb-name>
<method-name>updateSupplierDetails</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>

"Bean Class"

public void updateSupplierDetails(SupplierDetailsVO supplierDetailsVO, String currentRole) throws RemoteException, SQLException, SystemException {

Connection con = null;
try {
con = ConnectionPool.getConnection();
SupplierDetailsDAO supplierDetailsDAO = new SupplierDetailsDAOImpl();
supplierDetailsDAO.updateSupplierDetails(con, supplierDetailsVO, currentRole);
}
catch (SQLException e) {
throw new SQLException(e.getMessage());

}
catch (Exception e) {
throw new RemoteException(e.getMessage());

}
finally {

ConnectionPool.closeConnection(con);
}

}

supplierDetailsDAO.updateSupplierDetails(con, supplierDetailsVO, currentRole) methode having one update in one table, delete from another table and insertion in the same table depending upon certain criteria.

Now if any of above query fails (update delete insert), it is not rolling back fully.

Can you suggest me what wrong I am doing here!!!

Thanks
Shalindra Singh
SCJP, SCWCD
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
This EJB method does a database (1) update record, (2) delete record, and (3) insert record.

What operation fails? ... and why?


What does the following mean?

<session-type>Stateful</session-type>

[ August 25, 2008: Message edited by: James Clark ]
shalindra Singh Suryvanshee
Greenhorn

Joined: Aug 05, 2008
Posts: 25
HI James!

probably My msg conveyed wrong to you.
It is not any DAO methods which are failing or creating problem.
I am asking if I explicitely throw error in (say insertion) then whole transation should rollback. but it is not happening. First two operations 1. Updation 2) Deletion persist in a database while all three operations are in same method (Which mean within single transaction unit, I think).

I am asking if there is something wrong in my code of something missing to achieve above functionality?

Thanks
Shalindra
Jerwin Louise Uy
Ranch Hand

Joined: Oct 27, 2007
Posts: 75
Are you asking if a rollback is possible for joined transactions?

If so, then we could look at it having a remote interface that acts as a facade calling three business methods where the first business method has REQUIRED transaction attribute, that other having SUPPORTS transaction attribute, that way when any exception is thrown, it would rollback the whole transaction.

Im not sure why your business method is stateful when you are using asking for a stateless scenario. Also, the SUPPORTS transaction attribute implies a previous transaction is already existing, else any transactional related functions executed in a method without transaction will cause an error.

Regards.


Uy Jerwin Louise Vergara
Junior Developer / Research and Development at Incuventure Partners Corporation
email: jerwin.uy@incuventure.net
shalindra Singh Suryvanshee
Greenhorn

Joined: Aug 05, 2008
Posts: 25
Yes ! I want to rollback the transaction if any exception occur.
I tried with using 'stateless' or 'stateful'.
I posted code above, I wanted to know whether above code will OK for making rollback if any exception occur, if not please suggest me the way by which I may achieve it.

If I remove SUPPORT section from ejb-jar.xml, even though it is not working.
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
I am asking if I explicitely throw error in (say insertion) then whole transation should rollback. but it is not happening.


In your updateSupplierDetails(...) method above, where does "say insertion" occur?

Are you purposely creating an error? If so, where is the code that is creating the error?

"explicitly" is not spelled with two "e", only one
shalindra Singh Suryvanshee
Greenhorn

Joined: Aug 05, 2008
Posts: 25
I am sorry to bothering you...
Give up...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Transaction handling with Stateless SessionBean