SessionSynchronization “special moments”

image with no caption

When it’s called

At the beginning of a stateful bean’s transaction, BEFORE the business method that’s going to run in the transaction is called.

Just before the transaction ends, AFTER the business method completes.

After the transaction has ended either in a commit or rollback. The method passes a boolean argument to tell you whether the transaction committed.

Which entity bean method it’s most like

ejbLoad()

ejbStore()

Nothing in the entity bean lifecycle corresponds to this. (Because there’s no need.)

What to do when you’re in it

Load in data from the database, knowing that the database resource is now part of the transaction, so you can cache the data in the bean for the rest of the transaction.

Last chance to update the database before the transaction commits, and the locks on the database are released.

Find out how the transaction went, and do whatever you have to do to stay synced with the database. For example, if you were caching newly-changed data in temporary variables, you can now assign the temporary values to your permanent state variables.Or if the transaction rolled back, you might have to reset some values with data from the database.

Bean things you can do while you’re in it

Call methods on your SessionContext: get your home, get your EJB object, get caller security info, force the transaction to rollback, check the rollback status.

Get Head First EJB now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.