mmuzahid
mmuzahid

Reputation: 2280

Is EJB-2.0 transaction rollback undo/revert database changes?

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?

Upvotes: 0

Views: 1014

Answers (2)

mmuzahid
mmuzahid

Reputation: 2280

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.

Upvotes: 0

Steve C
Steve C

Reputation: 19445

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

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

Upvotes: 2

Related Questions