wood burning stoves 2.0*
The moose likes EJB and other Java EE Technologies and the fly likes Transaction Management in EJB 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 Management in EJB" Watch "Transaction Management in EJB" New topic
Author

Transaction Management in EJB

Jotty Tharakan
Greenhorn

Joined: Dec 14, 2001
Posts: 17
I have created an EJB which is updating more than one table by calling one SP which inturn calling othe child sp's.
I have selected container managed transaction and set the
transaction property - Transaction Required for all the methods in EJB.
But whenever error occurs inthe transaction ,it is partially committing the data into datbase.
In exception block i've given context.setRollBackOnly();also.
Even then it happens.I've used iplanet Application server.
why it is happening ?
is it a problem with deployment descriptor ?
Any one can suggest a solution for this
Ram Dhan Yadav K
Ranch Hand

Joined: Aug 13, 2001
Posts: 321
Probably its not reaching the exception block you have given. Try to check whether its reaching the exception block. Goodluck.
Ramdhan YK

Ram Dhan Yadav (SCJP, SCWCD, SCJA-I, IBM EC(483))
"We are what we repeatedly do. Excellence, then, is not an act, but a habit."
Jotty Tharakan
Greenhorn

Joined: Dec 14, 2001
Posts: 17
It is going to the exception block.And if i am providing explicit
transaction management using connection .setAutocommit() and
connection.rollback() , it is working fine.
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
How did you obtain the JDBC Connection? Did you use a JNDI Lookup to obtain a DataSource, or did you use DriverManager? Only the former will be hooked into EJB transaction management...
Kyle
------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
Jotty Tharakan
Greenhorn

Joined: Dec 14, 2001
Posts: 17

we are using seperate bean to lookup the connection
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("jdbc/mapsDB");
java.sql.Connection con = ds.getConnection();
and in EJB we are calling this bean to get the connection
we are creating seperate context in ejb and using that context
we are managing transaction .
in case of rollback we are using ejbcontext.setRollbackOnly().
This method fails when u are modifying more than one table.
For single table updation it is working perfectly.
is it because connection object is in different context
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
Well, what matters is the sequence of operations. So long as it follows:
(1) Establish EJB transaction context (either programatically or using declarative transactions)
(2) Get the connection
(3) execute the SQL
(4) Do the "setRollbackOnly()"
It should work. However, if (1) and (2) are reversed, then it probably won't work.
Kyle
------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.
Jotty Tharakan
Greenhorn

Joined: Dec 14, 2001
Posts: 17
kyle one more doubt
if connection is created in one context(initial context) am using another context(session context)context we are doing roll back .will it create any problem ?.
we are using setSessionContext method to get the context
whether it carries the transaction context also .
what is the diff between session context and initial context ?


Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
I'm confused. I can't think of a good reason why you would ever call setSessionContext() in an EJB program. Are you telling me you're invoking this method directly?
Are you asking about the difference in the javax.naming.InitialContext and the SessionContext?
Kyle
------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.
Jotty Tharakan
Greenhorn

Joined: Dec 14, 2001
Posts: 17

we are calling the setSessionContext method in our EJBProgram
as follows
SessionContext sc;
public void setSessionContext(SessionContext s)
{
sc = s;


}
and using this context(sc) we are doing rollback
sc.setRollBackOnly();

is it correct to call this method ? Otherwise how to roll back without a session context.
If possible can u send an example stateless session bean programe involving DB transaction.
Also i want to know difference between InitialContext and sessionContext.

Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
OK, I understand that you have implemented a setSessionContext() method in your EJB -- you have to. But that's different from YOUR CODE calling the method -- instead the EJB framework calls the method at the appropriate time.
As to an example of a stateless session bean invoking a DB -- read Richard Monson-Haefel's wonderful "Enterprise JavaBeans" book. He has an example of that in chapter 12 of his book. In his chapter on Transactions (Chapter 14) he discusses the two setRollbackOnly() methods (the one in UserTransaction and the one in the EJB context).
He also covers what the InitialContext and EJB Contexts are.
Now, is there any chance you could post some of your code so we could take a look at it?
Kyle
------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.
Jotty Tharakan
Greenhorn

Joined: Dec 14, 2001
Posts: 17
Following is the sample of our code
public class CA_ActivityUpdate_esb implements SessionBean
{
SessionContext sc;
Connection con;
DBConnection db;
public void ejbCreate() { }
public void ejbActivate(){}
public void ejbPassivate(){}
public void ejbRemove() {}
public void setSessionContext(SessionContext s)
{
sc = s;


}
public void insertData()
{
try
{
// seperate bean to obtain connection
//from app server using initial context
db = new DBConnection();
con = db.connect();
// calling sp using the connection con
// main sp is calling other sp's inserting data into more than one table
if (errorcode !=0)
{
sc.setRollBackOnly();
throw new EJBException("errorcode");
}
}
catch(Exception e);
{
throw new EJBException("errorcode");
}
}

}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Transaction Management in EJB
 
Similar Threads
rollback() and setRollbackOnly()
Reentrant EJB ??
Transaction in EJB in Weblogic 6.1
Transaction in EJB in Weblogic 6.1
EJB-> java ->new EJB. What will be Txn status in the new EJB and should ejb-ref?