dcsimg
Problems with Container Managed Transaction in stateless session bean
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Tanner_Ertan
Posted On:   Tuesday, October 30, 2001 08:30 PM

Simple Question I have a standalone client calling a stateless session bean with container managed transaction. In the bean I have 2 database inserts in a method to SQL 2000 database. The second insert is for a table that does not exists so as per my understanding the transaction should be rolled back and neither of the updates should take place. What I am finding is the first insert takes place even though the second insert is throwing an error!! PLS HELP!!! Below is my EJB code. public class TranTestEJB implements SessionBean { SessionContext ctx; public void ejbActivate() {} public void ejbPassivate() {} public void ejbCreate() {} public void ejbRemove() {}    More>>

Simple Question
I have a standalone client calling a stateless session bean with container managed transaction. In the bean I have 2 database inserts in a method to SQL 2000 database. The second insert is for a table that does not exists so as per my understanding the transaction should be rolled back and neither of the updates should take place. What I am finding is the first insert takes place even though the second insert is throwing an error!! PLS HELP!!!
Below is my EJB code.

public class TranTestEJB implements SessionBean
{
SessionContext ctx;

public void ejbActivate() {}

public void ejbPassivate() {}

public void ejbCreate() {}

public void ejbRemove() {}

public void setSessionContext(SessionContext ctx) {
this.ctx = ctx;
}

public void runTest()
{
String url = "jdbc:odbc:Queue";
String user = "sa";
String password = "";
String sql;
String sql2;
Connection connection;
Statement statement;
try{
//ctx.getUserTransaction().begin();
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection = DriverManager.getConnection(url,user,password);
statement = connection.createStatement();
sql = "Insert into Description values ('HA')";
statement.executeUpdate(sql);
sql2 = "Insert into AAAXXX values ('HB')";
statement.executeUpdate(sql2);
().commit();
}
catch (Exception e)
{
throw new EJBException(e.getMessage);
}

finally
{
sql = null;
sql2 = null;
statement = null;
connection = null;
}

}
}

My deployment descriptor has the following code




TranTest
test.TranTestHome
test.TranTest
test.TranTestEJB
Stateless
Container





TranTest
*

RequiresNew




PLS Help.

   <<Less

Re: Problems with Container Managed Transaction in stateless session bean

Posted By:   Hai_Hoang  
Posted On:   Wednesday, October 31, 2001 11:17 AM

If you are using weblogic, you need to use TxDataSource instead of regular Datasource.

Also, as other reply suggests that you need to sessionContext.setRollBackOnly() in the catch block. To see how this is done by looking at the example I've. Here is the URL http://www.myhtg.com/examples.zip

Re: Problems with Container Managed Transaction in stateless session bean

Posted By:   Raymond_Tay  
Posted On:   Wednesday, October 31, 2001 12:16 AM

In the part where you catch the exception, you should make use of the instance of type SessionContext to call the method setRollbackOnly() so that the container will roll back the changes you made to the database.

Because you didnt instruct the container to roll back all database changes, the first insert completed while the second failed.

Hope it helps.

Regards
About | Sitemap | Contact