Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

EJB and Transaction  RSS feed

 
sudharshan tettu
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI
I am an ejb beginner , i have few doubts regarding transactions and bean method
1) What difference does it make between executing a bean method within an already exisitng transaction and within a new transaction by suspending already running client's transaction.
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What difference did you see when you tried it out yourself? Basically

>executing a bean method within an already exisitng transaction

This would be a bean whose transaction attribute may be supports or required. You throw a SystemException or call the setRollBackOnly and the original transaction is rolled back.

>transaction and within a new transaction by suspending already running client's transaction.

This would be a bean whose transaction attribute is 'RequiresNew'. A rollback of this transaction may or may not rollback the suspended transaction based on whether your app server returned a new Connection object for use in your inner transaction. The ejb spec says no nested transaction support - so it's really upto your app server.

cheers,
ram.
 
Manuel Alberto Quero
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ram,

I think you gave a pretty good explanation, but i'd like to comment just a couple of things:

-I think we should include in the first case MANDATORY transaction attribute (besides supports and required), right?

-I the second case, if your second method throws a SystemException and the first method does not catch the exception, it willcause the rollback of the first transaction as well, right?

//initiates the first transaction
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
void method1() throws Exception {
...
...
sessionCtx.getBusinessObject(ExampleBean.class).method2(); //call second method

} //Rollback first transaction

//initiates the second transaction
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
void method2() throws Exception {

...
...
String a = null;
a.getBytes(); //throws Runtime exception

} //Rollback second transaction


Best regards,

Manuel
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Manuel,

The difference between mandatory and required is that the container would throw an exception in the former case if the ejb is invoked without a transaction context. In the later case, it would instead start a new transaction. In the context of the question, you can use either one interchangeably to illustrate the example.

For the second question, AFAIK, there is nothing in the ejb spec that prohibits the container to provide a nested transaction. While it is certainly not supported in the specs, a container if it choses to may return different connection objects for the inner transaction (than the one which is suspended) and hence rollback on the inner would not affect the former. It is a theorotical possibility and it would very much depend on the container implementation.

cheers,
ram.
 
sudharshan tettu
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!