English 中文(简体)
Examples or Uses Cases to explain EJB Transaction Attributes
原标题:

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.

最佳回答

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.

问题回答

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 dessert that doesn t need the oven. If you are in the middle of baking some cakes at the oven, you stop that, create the dessert, 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 dessert, 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 dessert 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.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签