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 javax.inject.Provider;
23  import javax.persistence.EntityManager;
24  
25  /**
26   * Provider for {@link EntityManager}.
27   * <p/>
28   * Even though this class extends the {@link Provider} interface it is not bound as a provider of
29   * {@link EntityManager}. The reason for this is that the {@link EntityManager} objects do have a
30   * life cycle and should therefore not be stored in instance/member variables.
31   * <p/>
32   * The {@link UnitOfWork} defines the life cycle of the {@link EntityManager}. An entity manager
33   * will be created when the unit of work is started. It is open and valid for use during the entire
34   * unit of work. When the unit of work ends the entity manager is closed and can no longer
35   * be used.
36   * <p/>
37   * It is a good practice to store this provider in a instance/member variable and only obtain an
38   * {@link EntityManager} instance in a method where it is used. This ensures that the method always
39   * has access to a valid {@link EntityManager}.
40   * <p/>
41   * The {@link EntityManagerProvider} is thread save.
42   */
43  public interface EntityManagerProvider extends Provider<EntityManager>
44  {
45  
46      /**
47       * @return the {@link EntityManager}.
48       * @throws IllegalStateException if {@link UnitOfWork#isActive()} returns false.
49       */
50      EntityManager get()
51          throws IllegalStateException;
52  
53  }