View Javadoc
1   package org.apache.onami.persist;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import com.google.inject.Injector;
23  import com.google.inject.Key;
24  
25  import javax.inject.Provider;
26  import java.lang.annotation.Annotation;
27  import java.lang.annotation.ElementType;
28  import java.lang.annotation.Inherited;
29  import java.lang.annotation.Retention;
30  import java.lang.annotation.RetentionPolicy;
31  import java.lang.annotation.Target;
32  
33  /**
34   * Marks a method or class to be executed within a transaction.
35   * <p/>
36   * This will span a new transaction around the method unless there is already a running transaction.
37   * In the case that there is a running transaction no new transaction is started.
38   * If a rollback happens for a method which did not start the transaction the already existing
39   * transaction will be marked as rollbackOnly.
40   * <p/>
41   * Guice uses AOP to enhance a method annotated with {@link Transactional @Transactional} with a wrapper.
42   * This means the {@link Transactional @Transactional} only works as expected when:
43   * <ul>
44   * <li>
45   * The object on which the method is called has been created by guice.<br/>
46   * This can be achieved by having it (or a {@link Provider}) injected into your class
47   * or by calling {@link Injector#getInstance(Class)} or {@link Injector#getInstance(Key)}.
48   * </li>
49   * <li>
50   * The method which should be run transactional is not private, not static and not final.
51   * </li>
52   * </ul>
53   */
54  @Target( { ElementType.METHOD, ElementType.TYPE } )
55  @Retention( RetentionPolicy.RUNTIME )
56  @Inherited
57  public @interface Transactional
58  {
59  
60      /**
61       * A List of annotations for persistence units on which to start a transaction.
62       * Default is on all persistence units.
63       */
64      Class<? extends Annotation>[] onUnits() default { };
65  
66      /**
67       * A list of exceptions to rollback on. Default is {@link RuntimeException}.
68       */
69      Class<? extends Exception>[] rollbackOn() default RuntimeException.class;
70  
71      /**
72       * A list of exceptions to <b>not<b> rollback on. Use this to exclude one ore more subclasses of
73       * the exceptions defined in rollbackOn(). Default is none.
74       */
75      Class<? extends Exception>[] ignore() default { };
76  
77  }