Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can maintain the CDI bean state using <a href="http://docs.oracle.com/javaee/7/tutorial/doc/cdi-basic008.htm">CDI Scopes</a>. Actually <code>EntityManagerProducer#create()</code> missing the scope. What ever the RI of CDI you have configure/install in tomact either its Weld or OpenWebBean, you can define your cdi bean state as belwo.</p> <pre><code>@Produces @RequestScoped public EntityManager create() { return emf.createEntityManager(); } </code></pre> <p>Your problem are</p> <pre><code>1. CDI, JSF and JPA2. 2. Managing EntityManager lifecycle when using JPA in a non enterprise environment (e.g. Tomcat) </code></pre> <p><strong>1. CDI, JSF and JPA2.</strong></p> <p>Tomcat container not support the CDI out the box, even not the JSF, you know developers had to supply the JSF jars them self's.JSF 2.2 has new CDI compatible scops @ViewScoped here's the CDI-only @FlowScoped which doesn't have an equivalent for @ManagedBean.</p> <p>(1) Really If you are most interested to use CDI or CDI+JSF+JPA , then upgrade tomcat to TomEE or go with TomEE. Tomcat + Java EE = TomEE.The Java Enterprise Edition of Tomcat,With TomEE you get Tomcat with JPA.</p> <p>(2) If you no control over upgrading the tomcat server, in that case you had to do i. Supply CDI and some others jar and configuration files along with weapp it self. ii. Installing CDI in tomcat (Weld, or OpenWebBeans these both are major CDI implementations)</p> <p>(3) Tomcat 8. Tomcat 8 is aligned with Java EE 7.</p> <p><strong>2) Managing EntityManager lifecycle</strong></p> <p>Managing EntityManager lifecycle when using JPA in a non enterprise environment (e.g. Tomcat) or Java SE is a custom task. In this situation, you should consider the right scope of the EntityManager to use and while working with resources it's always important to ensure they are closed when not longer needed.</p> <pre><code>There are three main types of EntityManagers defined in JPA. Container Managed and Transaction Scoped Entity Managers Container Managed and Extended Scope Entity Managers Application Managed Entity Managers </code></pre> <p>Working with JPA there are two kind of resources we can take care of: EntityManager and transactions. In this case, you should consider the right scope of the EntityManager to use.</p> <pre><code>1. An EntityManager is not a heavyload object. There is no need to use the same EntityManger longer than needed, You can't use an EntityManager instance for the whole application lifecycle (application scope) for the EntityManager is not Thread-safe) 2. It's not safe to traverse lazy-loaded relationships once the EntityManager is closed (This situation will change as of JPA 2.0). i.)Method scope (i.e. instantiate/destroy one EntityManager in each business method). The method scope is not enough for every situation. There could be some scenarios where you'll need a wide scope, such as the following situations: i. When transactions spread multiple business methods. ii. Need to traverse lazy-loaded relationships outside a method (e.g. in a JSF page). In method scope be careful to ensure the EntityManger is always closed ii.)Request scope (on-demand creation of the EntityManager instance to use within the request service) EntityManager per HTTP request strategy with the following features: i. Creation on demand of the EntityManager. ii. Lazy closing of the EntityManager. The main benefit of this scope is derived from the delayed closing of the EntityManager (it will last as long as a HTTP request is in process). Every queried entity will be managed till the end of the request and therefore during the presentation phase (the render phase in JSF for instance). </code></pre> <p>In your case your are using application entity manger and application managed transaction it means its your code which is supposed to handle the transaction. In a nutshell it means:</p> <p>You call:</p> <pre><code>entityManager.getTransaction().begin(); //to start a transaction </code></pre> <p>then if success you will ensure to call</p> <pre><code>entityManager.getTranasaction().commit(); //to commit changes to database </code></pre> <p>or in case of failure you will make sure to call:</p> <pre><code>entityManager.getTransaction().rollBack(); </code></pre> <p>Now imagine you have a container, which knows when to call <code>begin(), commit() or rollback()</code>, thats container managed transaction.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload