title: CDI transaction management
body: <p>I am working on a migration project from JBoss Seam to CDI. 
  Following is the technology stack : </p>
  
  <p>1)WildFly 8.2.0 (CDI 1.2 with Weld as CDI provider)</p>
  
  <p>2)JSF 2.2 </p>
  
  <p>3)JPA 2 </p>
  
  <p>We are using container managed JTA transactions :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="2.1"
     xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="
          http://xmlns.jcp.org/xml/ns/persistence
          http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"&gt;
     &lt;persistence-unit name="surveillenace" transaction-type="JTA"&gt;
      &lt;provider&gt;org.hibernate.jpa.HibernatePersistenceProvider&lt;/provider&gt;
      &lt;jta-data-source&gt;java:/surveillenaceDS&lt;/jta-data-source&gt; 
  
  
      &lt;!-- other configurations not shown here --&gt;
  
  &lt;/persistence&gt;
  </code></pre>
  
  <p>and using <code>@PersistenceContext</code> annotation to inject EntityManager into DAO object.</p>
  
  <p>We are using <code>@Transaction</code> annotation for container managed transactions.</p>
  
  <p>My questions/Understanding are below . </p>
  
  <p>Can anybody confirm this since this is relatively a new area for me. </p>
  
  <p>1)As I understand , CDI provides support for CMT through @Transaction interceptor. Which is the class / dependency actually implements this interceptor ? 
    Which artifacts we need to import for this in pom.xml ? </p>
  
  <p>2)Since CMT is used , we don't need to demarcate any transaction and container will manage it. We only need to use EntityManager API to persist changes in Db.
    Is this understanding correct ? </p>
  
  <pre><code> @Transactional
      public String finishOperation() {
          log.debug("in finishOperation() ") ;
          try {   
          //operations done on managed entities 
          //no transaction demarcation code is required here 
          dao.getEntityManager.commit();      
  
      }catch(Throwable xx){
      }
      }
  </code></pre>
  
  <p>3)Consider below trivial scenario executed using above configuration: </p>
  
  <p><code>Component1.somemethod()</code> - runs inside transaction and persists an entity (e.g: User) and commits transaction. 
    After this , Component2 is invoked as below :<br>
    <code>Component2.somemethod()</code> - runs inside the transation but the entity User doesn't seem to in managed state that is  <code>em.contains(user)</code> returns false. 
                  I have to merge this entity again to make it managed or reload from persistent store again</p>
  
  <p>Since Seam uses <code>conversation-scoped entity manager</code> , all entity instances remain in managed state (inside persistent-context) even when any component commits a transaction and another component is invoked there after. 
  But in CDI case , as I understand this is happening due to <code>"transaction scoped entity manager"</code>. Once a transaction commits , all entity instances become detached.
  How can we achieve same effect as Seam using CDI ? </p>