• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Container-managed transaction not rolling back after exception...

 
SAFROLE YUTANI
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried using container-managed transactions with a stateless session bean (EJB 2.0) on WL6.1 for the first time, but with no luck. I attempted a very simple example in order to understand the concept and see it work, but the container doesn't seem to be "rolling back" after an SQLException is raised. I am simply inserting 2 rows of data. During the second insert, I am intentionally inserting NULL for a column that does not allow NULL in order to raise SQLException in the hope that the first insert will be rolled back, but it doesnt. Here is the method in my bean class:

public void testCMTRX() throws SQLException {
 Context c = new InitialContext();
 DataSource ds = (DataSource)c.lookup( "jdbc/employee");
 Connection conn = ds.getConnection();
 Statement statement = conn.createStatement();
 statement.execute("INSERT INTO EMPLOYEE VALUES (500, 'Rick Connor'));
 statement.execute();
 Statement statement = conn.createStatement();
/*NULL value is intentional in order to raise SQLException */
 statement.execute("INSERT INTO EMPLOYEE VALUES (NULL, 'Frank Morris'));
 statement.execute();
}

My deployment descriptor (ejb-jar.xml) for the ejb is as follows:

<ejb-jar>
 <enterprise-beans>
  <session>
   <display-name>GoalJDAOTRX</display-name>
    <ejb-name>GoalJDAOTRX</ejb-name>
    <home>godriver.GoalJDAOTRXHome</home>
    <remote>godriver.GoalJDAOTRX</remote>
    <ejb-class>godriver.GoalJDAOTRXBean</ejb-class>
    <session-type>Stateless</session-type>
    <transaction-type>Container</transaction-type>
   </session>
  </enterprise-beans>
  <assembly-descriptor>
   <container-transaction>
    <method>
     <ejb-name>GoalJDAOTRX</ejb-name>
     <method-name>*</method-name>
    </method>
    <trans-attribute>Required</trans-attribute>
   </container-transaction>
  </assembly-descriptor>
</ejb-jar>

I must be missing something, but I cant figure out what it is. Am I missing something in the deplyoment descriptor?
Thank You!
SAF
[ March 13, 2002: Message edited by: SAFROLE YUTANI ]
 
gautam shah
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by SAFROLE YUTANI:
I tried using container-managed transactions with a stateless session bean (EJB 2.0) on WL6.1 for the first time, but with no luck. I attempted a very simple example in order to understand the concept and see it work, but the container doesn't seem to be "rolling back" after an SQLException is raised. I am simply inserting 2 rows of data. During the second insert, I am intentionally inserting NULL for a column that does not allow NULL in order to raise SQLException in the hope that the first insert will be rolled back, but it doesnt. Here is the method in my bean class:

public void testCMTRX() throws SQLException {
 Context c = new InitialContext();
 DataSource ds = (DataSource)c.lookup( "jdbc/employee");
 Connection conn = ds.getConnection();
 Statement statement = conn.createStatement();
 statement.execute("INSERT INTO EMPLOYEE VALUES (500, 'Rick Connor'));
 statement.execute();
 Statement statement = conn.createStatement();
/*NULL value is intentional in order to raise SQLException */
 statement.execute("INSERT INTO EMPLOYEE VALUES (NULL, 'Frank Morris'));
 statement.execute();
}

My deployment descriptor (ejb-jar.xml) for the ejb is as follows:

<ejb-jar>
 <enterprise-beans>
  <session>
   <display-name>GoalJDAOTRX</display-name>
    <ejb-name>GoalJDAOTRX</ejb-name>
    <home>godriver.GoalJDAOTRXHome</home>
    <remote>godriver.GoalJDAOTRX</remote>
    <ejb-class>godriver.GoalJDAOTRXBean</ejb-class>
    <session-type>Stateless</session-type>
    <transaction-type>Container</transaction-type>
   </session>
  </enterprise-beans>
  <assembly-descriptor>
   <container-transaction>
    <method>
     <ejb-name>GoalJDAOTRX</ejb-name>
     <method-name>*</method-name>
    </method>
    <trans-attribute>Required</trans-attribute>
   </container-transaction>
  </assembly-descriptor>
</ejb-jar>

I must be missing something, but I cant figure out what it is. Am I missing something in the deplyoment descriptor?
Thank You!
SAF
[ March 13, 2002: Message edited by: SAFROLE YUTANI ]

try to throw a RuntimeException or Remote Exception means System Exception not Application Exception and then see what happens......
 
SAFROLE YUTANI
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have not tried that yet, but if the second insert fails, which would raise SQLException, then that should be enough to indicate to the container that a rollback is required.
SAF
 
gautam shah
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by SAFROLE YUTANI:
I have not tried that yet, but if the second insert fails, which would raise SQLException, then that should be enough to indicate to the container that a rollback is required.
SAF

just throw RuntimeException and see what happens.
actully in this case there is a big diff in application and system exception
 
SAFROLE YUTANI
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah, you're right! SQLException is only an application exception, which does not force the EJB container to roll back. Actually, I'm catching the SQLException and calling context.setRollbackOnly() to force the rollback. I'll see how that works!
thanks!
SAF
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic