title: Transactions not updating DB when using container-managed(Glassfish) JPA with Spring
body: <p>How can I get JPA(via Hibernate)/Spring and my Container managed connection (via Glassfish 3.1) to play nice?</p>
  
  <p><strong>Update</strong>: Somehow my JPA connection seems to be buffered.  If I modify a record such that I convert its strings to upper case, then if I look it up I get a record with upper case strings. But the underlying DB does not reflect this.  Another example if I delete an entity and then try to delete it again I get an exception "java.lang.IllegalArgumentException: org.hibernate.ObjectDeletedException: deleted instance passed to merge: [db.co05in.Test#]" but again the underlying db is not updated.</p>
  
  <p>Included:</p>
  
  <ul>
  <li>Entry code showing spring injection of TestService</li>
  <li>TestServiceImpl</li>
  <li>applicationContext.xml</li>
  <li>persistance.xml</li>
  </ul>
  
  <p><strong>Solved</strong>: I have no idea why but the issue was somehow caused by wiring by name? Anyways see the comments under "Service implementation code" if you experience a similar issue.</p>
  
  <p>Entry code: </p>
  
  <pre><code>package com.aerose.partgroupmaster.action;
  import com.aerose.mz.db.service.inventory.TestService;
  
  public class DeleteTest{
      public com.aerose.mz.db.co05in.Test test; //the POJO JPA DAO
      public int id;
      private TestService testService;
  
      //GOOD: spring is able to inject the service 
      public void setTestService(TestService testService){
          this.testService = testService;
      }
  
      @Override
      public String execute() {
          test = testService.retreiveTest(id);
          testService.deleteTest(test);
          return SUCCESS;
      } 
  }
  </code></pre>
  
  <p>Service implementation code:</p>
  
  <pre><code>package com.aerose.mz.db.service.inventory;
  
  import db.co05in.Test;
  import javax.persistence.EntityManager;
  import javax.persistence.EntityManagerFactory;
  import javax.persistence.Query;
  import org.springframework.transaction.annotation.Transactional;
  
  public class TestImpl implements TestService{
      @PersistenceContext //needed to add this
      private EntityManager em;
  
      //removed this... and edited applicationContext.xml to remove the wiring
      //public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory){
      //    this.em = entityManagerFactory.createEntityManager();
      //}
  
      @Transactional
      @Override
      public void createTest(Test test) {
          em.persist(test);
      }
  
      @Transactional
      @Override
      public Test retreiveTest(int id) {
          Query query = em.createNamedQuery("Test.findByIdTEST");
          query.setParameter("idTEST", id);
          return (Test) query.getResultList().get(0);
      }
  
      @Transactional
      @Override
      public Test updateTest(Test test) {
          Test test2 = em.merge(test);
          return test2;
      }
  
      @Transactional
      @Override
      public void deleteTest(Test test) {
          em.remove(em.merge(test));
      }
  }
  </code></pre>
  
  <p>applicationContext.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:p="http://www.springframework.org/schema/p"
         xmlns:context="http://www.springframework.org/schema/context"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:aop="http://www.springframework.org/schema/aop"
         xmlns:jdbc="http://www.springframework.org/schema/jdbc"
         xmlns:jee="http://www.springframework.org/schema/jee"
         xsi:schemaLocation="
              http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans.xsd
              http://www.springframework.org/schema/aop
              http://www.springframework.org/schema/aop/spring-aop.xsd
              http://www.springframework.org/schema/tx
              http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
              http://www.springframework.org/schema/jdbc
              http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
              http://www.springframework.org/schema/context
              http://www.springframework.org/schema/context/spring-context-3.0.xsd
              http://www.springframework.org/schema/jee 
              http://www.springframework.org/schema/jee/spring-jee-3.0.xsd"&gt;
      &lt;context:annotation-config/&gt;            
      &lt;context:component-scan base-package="com.aerose" /&gt;
  
      &lt;tx:annotation-driven /&gt;
  
      &lt;jee:jndi-lookup id="dataSource" jndi-name="jdbc/MySQLLocalDataSource" /&gt;
  
      &lt;bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/&gt;
  
      &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
          &lt;property name="persistenceUnitName" value="co05in" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"/&gt;
  
  &lt;bean id="testService" class="com.aerose.mz.db.service.inventory.TestImpl"&gt;
      &lt;!-- following not needed --&gt;
      &lt;!--&lt;property name="entityManagerFactory" ref="entityManagerFactory"/&gt;--&gt;
  &lt;/bean&gt;
  
      &lt;tx:annotation-driven  transaction-manager="transactionManager"/&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>persistance.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"&gt;
      &lt;persistence-unit name="co05in" transaction-type="JTA"&gt;
          &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
          &lt;jta-data-source&gt;jdbc/MySQLLocalDataSource&lt;/jta-data-source&gt;
          &lt;class&gt;db.co05in.Test&lt;/class&gt;
          &lt;exclude-unlisted-classes&gt;true&lt;/exclude-unlisted-classes&gt;
          &lt;properties&gt;
              &lt;property name="hibernate.show_sql" value="true"/&gt;
              &lt;property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.SunONETransactionManagerLookup"/&gt;
              &lt;property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory"/&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>