views:

492

answers:

2

There are some good explanations of EJB Transaction Attributes (and annotations) out there, for example, OpenEJB's.

But sometimes when I try to cover this with someone who hasn't worked with many transactional resources, I see their eyes start to glaze over.

So my question - how would you explain EJB Transaction Attributes to your grandmother?

  • Required
  • RequiresNew
  • Mandatory
  • NotSupported
  • Supports
  • Never

I'm thinking a contrived example, analogy, or a concise real-world use case would be helpful.

+4  A: 

You can think of them in terms of Collaboration. Let's say you are a chef, and you have a dozen ovens to work with. You need to bake some cakes. For this example, you are the called method, the oven is the transaction and the your boss is the caller.

  • Required: if your boss tells you to bake some cakes without telling you what oven you use, you just go ahead and pick one. At the end, you close the oven, making sure nobody else can use it.
  • Requires New: You will always bake the cakes on a free oven you choose. If you're in the middle of the process of baking some cakes and your boss tells you to bake another batch, you interrupt the current baking process, go and bake some cakes at a new oven, the resume the baking for the old ones.
  • Mandatory: You are a dumb cook. Your boss always have to tell you what oven you have to use. If your boss doesn't tell you what oven to use, you shout back "stupid!".
  • NotSupported: Think of this as doing a dissert that doesn't need the oven. If you are in the middle of baking some cakes at the oven, you stop that, create the dissert, and the resume the cake baking.
  • Supports: This is more suited to a cook's helper. You are the helper. If the main cook asks you to bake a cake with oven X, you just do that. If he asks you to do a dissert, you just do that. The main difference in regards to the other ones is that you never ask any question nor choose to do anything. You just follow orders.
  • Never: This is yet another dumb cook. If your boss asks you to do a dissert while you're in the middle of baking a cake, you shout back and say "I quit!". No cake is ever baked. So your boss has to be careful and only ask you to bake some cakes when you are not baking any cakes.

Hope that helps.

Miguel Ping
A: 

I think it makes sense to think about this in terms of the container's interaction with a caller to the EJB method as a true monitor... so I'd like to use a bouncer metaphor in various different scenarios.

See this page for a good description/overview of the transaction attributes.

Required (REQUIRED @TransactionAttribute)
Night club

Show up at the club, need a ticket to enter. If you don't have one it will be (purchased?) given to you at the door.

Transaction is the TICKET.
Container is the BOUNCER.

Requires New (REQUIRES_NEW @TransactionAttribute)
Comedy clubs, 1 drink-minimum, no re-entry

Show up at the club, no outside food/drink, you must leave them at the door. To get in you must purchase 1-drink minimum every time you leave and re-enter.

Transaction is the DRINK.
Container is the BOUNCER.
Suspending the transaction is LEAVING AT THE DOOR.

Supports (SUPPORTS @TransactionAttribute)
House party

Show up at the party, alcohol is permitted. We'll let you in with it if you have your own alcohol, if you don't we'll let you in too.

Transaction is the ALCOHOL.
Container is the HOST.

Mandatory (MANDATORY @TransactionAttribute)
Invite-only party

Show up at the party, need a invitation to enter: If you don't have one and try to get in, the bouncer calls the authorities.

Transaction is the INVITATION.
Container is the HOST.
Throwing an exception is CALLING THE AUTHORITIES.

Not Supported (NOT_SUPPORTED @TransactionAttribute)
Concert, cameras are prohibited.

Show up at the concert, cameras are prohibited. You can leave it at the door and pick it up when you leave.

Transaction is the CAMERA.
Container is the DOORMAN.
Suspending the transaction is LEAVING AT THE DOOR.

Never (NEVER @TransactionAttribute)
High school dance

Show up at the dance, alcohol is prohibited. If you try to get in with it and are caught, the chaperone calls the authorities.

Transaction is the ALCOHOL.
Container is the CHAPERONE. Throwing an exception is CALLING THE AUTHORITIES.

cwash