title: EntityManager.merge() is not being committed (Wildfly, JPA, JTA)
body: <p>I can persist new data, but I cannot do updates. There are no errors, just no transactions committing the changes. I'm assuming this has something to do with the way that I've set up transactions. I'm trying a bunch of relatively new (to me) set of technologies. Below are the details.</p>
  
  <p>I'm using the following tools/technologies:</p>
  
  <ul>
  <li>Wildfly 8 and Java 7 (which is what my hosting service uses)</li>
  <li>Annotations, with minimal XML being the goal</li>
  <li>Struts 2.3 (using the convention plugin)</li>
  <li>Spring 3.2</li>
  <li>Hibernate 4.3</li>
  <li>JTA (with container managed transactions (CMT))</li>
  <li>JPA 2 (with a Container Managed Persistence Context)</li>
  <li>EJBs (I have a remote client app that runs htmlunit tests)</li>
  <li>Three WAR files and one EJB JAR file deployed</li>
  <li>SpringBeanAutowiringInterceptor to autowire the EJBs (could there be an error in here where transactions don't commit?)</li>
  </ul>
  
  <p>beanRefContext.xml (required by SpringBeanAutowiringInterceptor)</p>
  
  <pre><code>&lt;beans&gt;
    &lt;bean
      class="org.springframework.context.support.ClassPathXmlApplicationContext"&gt;
      &lt;constructor-arg value="classpath:campaignerContext.xml" /&gt;
    &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>campaignerContext.xml</p>
  
  <pre><code>&lt;beans&gt; 
    &lt;context:component-scan base-package="..." /&gt;   
    &lt;jee:jndi-lookup id="dataSource" jndi-name="jdbc/CampaignerDS"/&gt;
    &lt;tx:annotation-driven/&gt; 
    &lt;tx:jta-transaction-manager/&gt;  
    &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
      &lt;property name="persistenceUnitName" value="campaigner" /&gt;
    &lt;/bean&gt;
    &lt;bean id="ehCacheManager" class="net.sf.ehcache.CacheManager" factory-method="create"&gt;
      &lt;constructor-arg type="java.net.URL" value="classpath:/campaigner_ehcache.xml"/&gt;
    &lt;/bean&gt; 
  &lt;/beans&gt;
  </code></pre>
  
  <p>persistence.xml</p>
  
  <pre><code>&lt;persistence&gt;
    &lt;persistence-unit name="campaigner" transaction-type="JTA"&gt;
      &lt;provider&gt;org.hibernate.jpa.HibernatePersistenceProvider&lt;/provider&gt;
      &lt;jta-data-source&gt;java:/jdbc/CampaignerDS&lt;/jta-data-source&gt;
  
      &lt;class&gt;....UserRegistration&lt;/class&gt;
      ...
  
      &lt;shared-cache-mode&gt;ENABLE_SELECTIVE&lt;/shared-cache-mode&gt;
  
      &lt;properties&gt;
        &lt;property name="hibernate.transaction.jta.platform"  value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform" /&gt;
       &lt;/properties&gt;
    &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>SecurityServiceBean.java</p>
  
  <pre><code>@EnableTransactionManagement
  @TransactionManagement(value = TransactionManagementType.CONTAINER)
  @TransactionAttribute(value = TransactionAttributeType.REQUIRES_NEW)
  @Stateless
  @Interceptors(SpringBeanAutowiringInterceptor.class)
  @DeclareRoles("Security Admin")
  public class SecurityServiceBean extends AbstractCampaignerServiceImpl implements
      SecurityServiceLocal, SecurityServiceRemote
  {
    @Override
    @PermitAll
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public UserRegistration confirmRegistration(
      String confirmationCode) throws ApplicationException
    {
        UserRegistration userRegistration = this.userRegistrationDAO
        .find(new UserRegistrationQuery(null, confirmationCode)).uniqueResult(); // Should be attached now
  
      ...
            userRegistration.setConfirmationDate(new Date());
            userRegistration.setState(State.CONFIRMED);
            userRegistration = this.userRegistrationDAO.saveOrUpdate(userRegistration);
      ...
    }
  }
  </code></pre>
  
  <p>UserRegistrationDAO.java</p>
  
  <pre><code>@Override
  public UserRegistration saveOrUpdate(
    UserRegistration obj) throws DAOException
  {
    log.debug("[saveOrUpdate] isJoinedToTransaction? "
              + (this.em.isJoinedToTransaction() ? "Y " : "N"));
  
    try
    {
      if (obj.getId() == null)
      {
        this.em.persist(obj);
  
        log.debug("[saveOrUpdate] called persist()");
  
        return obj;
      }
      else
      {
        UserRegistration attached = this.em.merge(obj);
  
        log.debug("[saveOrUpdate] called merge()");
  
        return attached;
      }
    }
    catch (PersistenceException e)
    {
      throw new DAOException("[saveOrUpdate] obj=" + obj.toString() + ",msg=" + e.getMessage(), e);
    }
  }
  </code></pre>
  
  <p>Are there any settings in Wildfly's standalone.xml that you need to see or that I should be setting?</p>
  
  <p>BTW, this is incredibly annoying and frustrating. This should be an easy one-time setup that I can do and then forget about as I move on to creating my website, which should be where most of my time is spent. The lack of comprehensive documentation anywhere is AMAZING. Right now, development has been halted until this is solved
  /rant</p>
  
  <p>UPDATES</p>
  
  <ul>
  <li>I tried switching to an XA data source, because some sites claimed that was necessary, but that didn't work (didn't think so but had to try). Also tried configuring emf with dataSource instead of persistenceUnitName as some other sites have. No joy.</li>
  <li>I tried replacing the transactionManager with JpaTransactionManager, but that just led to this exception: A JTA EntityManager cannot use getTransaction()</li>
  </ul>