• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

BMP and CMT...

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 3892
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
     
    A sonic boom would certainly ruin a giant souffle. But this tiny ad would protect it:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic