How attributes work

  1. Mark a method with one of six transaction attributes:

    • Required

    • RequiresNew

    • Mandatory

    • Supports

    • NotSupported

    • Never

    image with no caption
  2. When the method is called, the container uses the attribute to do one of five things:

    • Run the method in the caller’s transaction.

      OR

    • Suspend the caller’s transaction and start a new transaction.

      OR

    • Suspend the caller’s transaction and run the method without a transaction.

      OR

    • Throw an exception because the caller does not have a transaction.

      OR

    • Throw an exception because the caller does have a transaction.

      void go() {
          aFooBean.setFoo();
      }
      image with no caption
image with no caption

Know your attributes

How an attribute affects behavior depends on one thing:

Is the calling method in a transaction?

image with no caption
  1. Method foo() is in a transaction (tx A).

    image with no caption
  2. Method bar() is marked with the Required transaction attribute.

    <method>
       <ejb-name>MyBean</ejb-name>
       <method-name>bar</method-name>
    </method>
    <trans-attribute>Required</trans-attribute>

    Note

    The bar() method runs in the same transaction as foo() because bar() is marked Required and foo() has a ...

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.