0
votes

Suppose I have written some transaction related code in my ejb bean method as follows:

UserTransaction utx = sessionContext.getUserTransaction();
try {
    int status = 0;
    utx.begin();
    status = 1;
    //SEGMENT- 1: UPDATE DATABASE

    //SEGMENT- 2: SOME OTHER CODE - which may throw Exception

    utx.commit();
} catch (Exception e) {
    utx.rollback();//SEGMENT- 3: My QUESTION at here, is it bollbak DATABASE changes also? 
                    // OR only rollback value of variable 'status' to '0'
    e.printStackTrace();
}

1. BEGIN: After starting my transaction utx.begin();
Changed java variable status = 1;
Changed database (SEGMENT- 1). e.g. some INSERT and UPDATE at DB
Then execute some other calculation (SEGMENT- 2)

2. COMMIT: Now before commit utx.commit()
Some Exception arise at (SEGMENT- 2)

3. ROLLBACK: So catch block catches the Exception (SEGMENT- 3) and execute transaction rollback utx.rollback().

My QUESTION:
Is utx.rollback() rollback
All database changes and java variable changes?
OR Only database changes
OR Only rollback java variable changes?

2
Memory isn't transactional, so variable value won't be changed. You need to do it manually in catch block. - slwk
@slwk - is utx.rollback() undo database changes made during transaction. - mmuzahid

2 Answers

1
votes

All database changes made following the utx.begin() will be rolled back.

utx.rollback() has no effect on your java variables.

0
votes

I would like to extend Steve C's answer

Another important point is, If we made any database level COMMIT then those changes never rollback by EJB utx.rollback().

For example if we execute

stmt.executeUpdate("COMMIT");

Or execute COMMIT explicitly inside any package or stored procedure which is called then database level commit occurs.