Can't roll back transaction.

Discussions

EJB programming & troubleshooting: Can't roll back transaction.

  1. Can't roll back transaction. (3 messages)

    Hi, I'm a newbie ejb programming.

    I develop ejb session bean stateless to execute store procedure by using CMP. So after I call EJBContext.setRollbackOnly();,

    Why it can't roll back data in database??? Anybody please help me or any suggestion is welcome.

    Detail of ejb programming.
    ===

    public class SaveAssociateBean implements SessionBean {
        SessionContext sessionContext;
        private Connection con;
        private DBRetInt buf;
        public void ejbCreate() throws CreateException {
        }

        public void ejbRemove() {
        }

        public void ejbActivate() {
        }

        public void ejbPassivate() {
        }


        public void setSessionContext(SessionContext sessionContext) {
            this.sessionContext = sessionContext;
        }

        public void setConnection(Connection conn) {
            con = conn;
        }

        public Object saveAssociate(String associate_key_in, String equip_id_in, String product_code_in) throws
                SQLException {
            CallableStatement cs = null;
            buf = new DBRetInt();
            try {
                cs = con.prepareCall("Begin xxxx.SaveAssociate(?,?,?,?,?,?); End;");
                cs.setString(1,associate_key_in);
                cs.setString(2,equip_id_in);
                cs.setString(3,product_code_in);
                cs.registerOutParameter(4, java.sql.Types.INTEGER);
                cs.registerOutParameter(5, java.sql.Types.INTEGER);
                cs.registerOutParameter(6, java.sql.Types.VARCHAR);

                cs.execute();

                buf.setRetResult(cs.getInt(4));
                buf.setDBRetCode(cs.getInt(5));
                buf.setDBRetDescription(cs.getString(6));
            } catch (SQLException ex) {
                if (cs != null)
                        cs.close();
                ex.printStackTrace();
            }
            if (cs != null) {
                cs.close();
            }

    // con.close();
            return buf;
        }


    }


    and I have another ejb main to call this bean and I try to call setRollbakOnly from ejb main.

    Threaded Messages (3)

  2. Hi,

    Here is my reasoning:

    Since you are using stateless session bean, you shud not have any instance variables like connection except sessionContext.
    This goes against the concept of stateless session bean.

    In your case you are taking connection before you call the methods on bean.

    When the saveAssociate() is called the container starts a new transaction. But it has no knowledge of connection object you have got before this call. Hence the JDBC transaction of this connection is outside the scope of transaction associated with your method call. And hence setRollbackOnly() does not work.

    Also in your code Where is your CMP ?? Sorry I could not find any. PLease point out if I have missed it in your code.

    If you try to think from abouve line of thinking, you may hit upon somthing.

    shrini
  3. Reply shrini[ Go to top ]

    Sorry, I using CMT, and I set it in DD.

    If I no need to pass connection object into the method, Should I initial connection from context right?

    The reason as I coding like this because I have many stateless session bean to call store procedure and I need to handle initial context at 1 point from ejb main.
  4. Reply shrini[ Go to top ]

    Hi,

    Yes you should do something of following:

    1. Get Initial Context object. Dont pass any init params simply try this:
    ctx = new InitialContext();
    container will take care of initialization.

    2. Look up for JDBC data source.

    3. Exec your SQl statement.

    In my opinion the transaction of the bean and JDBC connection would be same.

    Can you please reply me for your findings.

    Thanks
    shrini