Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Examples or Uses Cases to explain EJB Transaction Attributes

Tags:

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.

like image 895
cwash Avatar asked Nov 18 '09 22:11

cwash


People also ask

What is transaction attribute in EJB?

ejb. TransactionAttributeType constants. If you decorate the enterprise bean class with @TransactionAttribute , the specified TransactionAttributeType is applied to all the business methods in the class. Decorating a business method with @TransactionAttribute applies the TransactionAttributeType only to that method.

What are transactional attributes?

A transaction attribute controls the scope of a transaction. Figure 27–1 illustrates why controlling the scope is important. In the diagram, method-A begins a transaction and then invokes method-B of Bean-2 .

What is the default transaction attribute to a method in an EJB?

Yes, CONTAINER and REQUIRED are the default.

What is transaction management in EJB?

EJBs support two types of transaction management: container managed and bean managed. Container managed transactions provide a transactional context for calls to bean methods, and are defined using Java annotations or the deployment descriptor file ejb-jar. xml.


1 Answers

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.

like image 162
Miguel Ping Avatar answered Oct 05 '22 19:10

Miguel Ping