In afterCompletion() you cannot call setRollbackOnly() or getRollbackOnly(), because the transaction has completed. Also you cannot access another enterprise bean or a resource manager. Page 512 of Head First EJB says "You can't do any tranaction-related things because you're no longer in a transaction. You can access ONLY your special JNDI context. It's not safe to access resource managers or other beans."
I've just been reading the spec Transactions section, and thought I understood a bit more about accessing resource managers, and how the container includes access to other resources (databases and JMS resources) within an existing transaction, so it seemed to make sense that you can't access a resource manager (and maybe another bean) from afterCompletion() because it's never within a transaction.
But then I remembered that a session bean's ejbCreate(), ejbRemove(), ejbActivate() and ejbPassivate() run in an unspecified transaction context, and we know that a bean is not passivated if it is in a transaction, so we know that ejbPassivate() will not have a transaction. But while these four methods don't allow calls to setRolbackOnly() and getRollbackOnly(), they *do* allow access to resource managers and other beans, which implies that the ability to do these things is not related to being in a transaction.
Does anybody understand why the rules are different for afterCompletion() and the four ejb* methods? I'm trying to understand why, rather than just memorise a table of what's allowed. Is the ability to access another bean and a resource manager really related to being in a transaction? Is there something I've not understood?