CMP session beans, transactions and datasources
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Paul_Watson
Posted On:   Friday, February 7, 2003 03:17 AM

Can someone confirm/deny that if I obtain and utilise a datasource/connection from the ENC within a transactional CMP session bean will it become part of the beans transactional scope? I am a little confused at the moment with regards to transactions within CMP session beans. If for example I have the following method; - (void)addMessage(String message) within my session bean and it is declared transaction REQUIRED. Within the method I do this; { Context context = new InitialContext(); DataSource dbSource = (DataSource) context.lookup("java:comp/env/jdbc/EBank"); *** an enc resource variable defined Connection conn = dbSource.getConne   More>>

Can someone confirm/deny that if I obtain and utilise a datasource/connection from the ENC within a transactional CMP session bean will it become part of the beans transactional scope?


I am a little confused at the moment with regards to transactions within CMP session beans. If for example I have the following method;


- (void)addMessage(String message)


within my session bean and it is declared transaction REQUIRED. Within the method I do this;


{

Context context = new InitialContext();

DataSource dbSource = (DataSource) context.lookup("java:comp/env/jdbc/EBank"); *** an enc resource variable defined

Connection conn = dbSource.getConnection();

ps = dbConnx.prepareStatement("INSERT INTO MESSAGE VALUES(1, ?message)");

ps.executeQuery();

conn.close();

throw new RuntimeException(); *** causes transaction rollback

}


Will this be commited to the database or not? I have managed to produce test cases using our application server where dependent on from where I obtain my datasource it works or not...Is there some kind of rules on this?


thanks, Paul

   <<Less

Re: CMP session beans, transactions and datasources

Posted By:   Nick_Maiorano  
Posted On:   Sunday, February 16, 2003 08:07 AM

Paul,



I'm a little confused myself. There is no such thing as CMP session beans. CMP is for entity beans only and means container managed persistence. Judging by your code excerp, you have implemented a session bean that handles its own persistence using JDBC.



There are definite (and rather complex) rules on how the container will behave. If the container started the transaction just before calling addMessage(), it will rollback the transaction immediately since you are throwing a RuntimeException. If, however, the transaction was started earlier in the call stack, the container will only mark the transaction for rollback. When the call stack execution ends, it will be rolled back.



This is somewhat of a simplication of the rules. To get the full story on exception handling and transactions, consult chapter 18 of the ejb spec here.

About | Sitemap | Contact