This week's book giveaway is in the Big Data forum.
We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes Transaction roll back in session bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Transaction roll back in session bean" Watch "Transaction roll back in session bean" New topic

Transaction roll back in session bean

Michal Glowacki
Ranch Hand

Joined: Mar 14, 2006
Posts: 114

I've a session bean to compute and create complex report. It stores data in 3 tables, and should be completely rolled back when any exception occurs. It's sth like this:

public void createReport() {
reportId = reportEntityBean.create(...).getReportId();

createSubReport(reportId, ...); /* invokes some calculations and subreportEntityBean.create(reportId, ...); */

createReportItem(reportId, ...); /* invokes some calculations and reportItemEntityBean.create(reportId, ...); */


Ok, this code if everything is ok, should create entities in 3 tables. But I want also everything to be rolledback if any exception in this method or methods invoked by it occurs. For example, if report entity is already create and exception in subreportEntintyBean.create method is raised, I want to remove created report. Unfortunately it's not working as expected. In ejb-jar.xml I have set attributes of the methods:

createReport as Required
createSubReport, createReportItem, checkIsValid as Required (also tried with Mandatory)

please help.
Michal Glowacki
Ranch Hand

Joined: Mar 14, 2006
Posts: 114
It's stateless cmp session bean, ejb 2.1 in jboss 4.0.5GA
Shivani Chandna
Ranch Hand

Joined: Sep 18, 2004
Posts: 380
If one throws an application exception from the method with defined attribute of rollback=true then rollback takes place (EJB 3.0 )
Call the method SessionContext / setRollbackOnly(). This flags that the method transaction should be rolled back.

Could you please check if this works with EJB 2.1 for your case as well?

- Shivani

/** Code speaks louder than words */
Michal Glowacki
Ranch Hand

Joined: Mar 14, 2006
Posts: 114
I enclosed all operations in method createReport into try catch block, and I invoke setRollbackOnly() in each catch block. Unfortunately transaction is not rolled back.
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Check your JDBC connection. If you get a JDBC connection from a plain DataSource or directly from the JDBC driver, it will not participate in the EJB container transaction.

SCJP 1.4, SCWCD 1.3, SCBCD 1.3
I agree. Here's the link:
subject: Transaction roll back in session bean