• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

BMP and CMT...

 
Alan Mc Kernan
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Guys,

Pretty unfamiliar with the EJB territory. Here's my question..
 
Alan Mc Kernan
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And the question this time..

I have a stateless session bean which uses

Connection conn = dbAccess.getConnection();
// Execute SQL query...
conn.commit();
conn.close();

This is an example of bean managed persistence, is this correct?

I have container managed transactions with the Required attribute.. after I do the conn.commit(); lets imagine an exception occurs ( I force throw an EJBException()).. when I look in my db tables my executed and commited SQL code is there..

Should'nt the transaction have been rolled back? i.e the changes to the db are undone.. What am I missing here.. Does CMT and BMP not mix? (as im explicitly commiting my changes.. with conn.commit()).. What are my other options?

Thanks in advance,
Alan
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When using CMT never, ever, ever use conn.commit(). If you obtain your DataSource (and thus your connection) from the container then the connection will automatically be committed or rolled back as appropriate by the transaction manager in the container.

Kyle
 
Valentin Tanase
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alan,

As per J2EE specs the next methods are strictly forbidden with CMT:
  • The commit, setAutoCommit, and rollback methods of java.sql.Connection
  • The getUserTransaction method of javax.ejb.EJBContext
  • Any method of javax.transaction.UserTransaction


  • Hence if your bean is configured to use any transaction attribute (other than TX_BEAN_MANAGED) I would expect your container to throw an appropriate exception before executing conn.commit().
    Regards.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic