• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sun J2EE Tutorial's CMT Stateful session bean Bank doesn't work  RSS feed

 
Tom Chen
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Does anyone ever deploy and test the CMT stateful session bean example from Sun J2EE Tutorial(the source code is in the directory of j2eetutorial\examples\src\ejb\bank)?
I've tried it and found the transaction cannot rollback!(By setting the balance of checking account to less than 40).
In the code provided by Sun, it creates a java.sql.Connection in ejbCreate() and stored it as a instance variable. Then use this connection in the transactional business method transferToSaving().
I try to modify to this way then the transaction works well(can rollback): create java.sql.Connection in the business method transferToSaving().
I wonder whether my solution is correct way to write CMT session bean or something wrong with my deployment( I deployed the bean on weblogic server 6.1)
Thank you,
Tom
 
Tom Chen
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Does anyone know something about this problem?
Thank you in advance,
Tom
 
Rich Raposa
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you invoking the rollback() method of the java.sql.Connection object? If so, that is not a good design for EJBs (and is likely the reason why your transaction is not rolling back).
If you want to rollback a transaction in an EJB, invoke the setRollbackOnly() method on the bean's Context object, assuming you are using container-managed transactions. For example,
public class Customer implements EntityBean
{
private EntityContext context;
public void setEntityContext(EntityContext e)
{
context = e;
}
public void someMethod()
{
if(something_bad)
{
context.setRollbackOnly();
}
}
}
 
Tom Chen
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rich,
Thank you for your reply.
In the example code, it rollback the Container Manage Transaction by calling context.setRollbackOnly(). The same as what you say. However it still cannot rollback unless create the java.sql.Connection in the business method instead of create it once in the ejbCreate() and store it in the instance variable. Why?
You can download the tutorial and take a look at the example code.
BTW, there's another way to let the transaction rollback by throwing a System Exception-- EJBException which is a RuntimeExcepion.

Thank you,
Tom
[ January 18, 2003: Message edited by: Tom Chen ]
 
Rich Raposa
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps the problem lies somewhere in the reference implementation server that comes with the J2EE SDK. It's certainly not a production-level server, and I have noticed several oddities with it in the past.
 
Tom Chen
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I deployed the .ear file on Weblogic 6.1 and it didn't work!
TOM
[ January 18, 2003: Message edited by: Tom Chen ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!