2 Replies Latest reply on Feb 19, 2005 4:28 AM by tianxie

    [CMT] Rollback not working

    Alen Ribic Newbie

      I was hopping someone could maybe help me solve a problem with regards to JBoss
      application server.
      Here it goes...

      I'm using JBoss 3.2.1 at this stage and am trying to make use of the JBossTX
      for handling managed transactions.

      Scenario:

      I have deployed a Stateless Session Bean (using CMT with */Required) that
      has a method named createPerson(...).
      This method manipulates a DTO and passes it on to my underlying persistence
      manager that stores the person's info into the underlying datastore. My
      datastore is a MySQL 3.x DB and I'm using a MySQL Connector/J 3.0.7 Stable
      driver.

      Session bean code snippet
      ----------------------------------

      public void createPerson(Person p) {
      Session s = null;
      try {
      Context ctx = new InitialContext();
      SessionFactory factory =
      (SessionFactory) ctx.lookup("java:/hibernate/HibernateFactory");

      s = factory.openSession();

      s.save(p);
      //s.flush();

      if (1==1) {
      //sessionContext.setRollbackOnly();
      throw new Exception("JTS test...");
      }
      } catch(Exception e) {
      e.printStackTrace();
      // this throw should automatically perform a rollback
      throw new EJBException(
      "Transaction failed: " + e.getMessage());
      } finally {
      if(s!=null) try { s.close(); } catch(Exception e) {}
      }
      }

      Now as you can see above, I throw an Exception that rethrows a system-level
      exception named EJBException.
      In this case, Container should automatically rollback the running
      transaction.
      However my database shows that INSERT statement has been committed as it
      reflects the inserted change.

      Alternatively, I have tried to set the ROLLBACK-ONLY using the session
      context's
      setRollbackOnly method and then throw an application-level exception.
      Unfortunately this didn't work either.

      My transaction-service.xml is running with default settings.
      And I'm running a service for MySQL datasource using the
      local-tx-datasource:

      mysql-ds.xml
      --------------------


      <local-tx-datasource>
      <jndi-name>jdbc/ejbPool</jndi-name>
      <connection-url>jdbc:mysql://localhost:3306/MyDB</connection-url>
      <driver-class>com.mysql.jdbc.Driver</driver-class>
      <user-name></user-name>

      </local-tx-datasource>


      Do I perhaps require to use the XA protocol for some reason?

      Any suggestions or ideas that I can try to help me find the solution to my
      problem, much appreciated.
      Maybe a way in which I can monitor the JBossTX invoking the begins, commits
      and rollbacks on my methods?