1 Reply Latest reply on Oct 11, 2004 5:10 AM by KT KT

    Why default auto commit mode is false?

    KT KT Newbie

      I try to get a connection to database as follow :

      InitialContext initialcontext = new InitialContext();
      DataSource dataSource = (DataSource)initialcontext.lookup("jdbc/mydb");
      Connection conn = dataSource.getConnection();
      System.out.println("Auto Commit Web Services : " + conn.getAutoCommit()); //always return false, why? the default value should be true, rite?
      initialcontext.close();
      //conn.commit(); //return error when i try to commit. The error is << java.sql.SQLException: You cannot commit during a managed transaction!>>
      conn.close();

      I get the database connection from a Stateless Session Bean.
      My question is :
      1.Why the default auto commit mode is false?
      2.When i try to commit the connection, i hit this error << java.sql.SQLException: You cannot commit during a managed transaction!>> why this happen?

      FYI, when i use the same code in servlet, the default auto commit mode is true, which is i think it's correct.

      Please guide, thanks.

        • 1. Re: Why default auto commit mode is false?
          KT KT Newbie

          hi all,
          I have found the answer from somewhere in internet. Post it here to share with all of us.

          1.Why the default auto commit mode is false?
          When we are in session bean, the transaction is handled by Application Server. Therefore, all connection created is with manual commit mode.Anyway, you can change is by modifying ejb-jar.xml (<transaction-type>Container</transaction-type>).


          2.When i try to commit the connection, i hit this error << java.sql.SQLException: You cannot commit during a managed transaction!>> why this happen?
          It's clear that once the connection is handled by Application Server, you are not allowed to commit the connection.Let's application handle this.