title: Spring + JPA transaction is created and commited but no result in db
body: <p>I've read many threads complaining to similar issue but none of them help me in my case. Anyway, the problem is that I have written a few classes:</p>
  
  <p><strong>Person entity:</strong></p>
  
  <pre><code>@Entity
  @Table(name="person")
  public class Person implements Serializable {
      private static final long serialVersionUID = 1L;
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;
      @Column(name="name")
      private String name;
  
      public Long getId() {
          return id;
      }
  
      public Person setId(Long id) {
          this.id = id;
          return this;
      }
  
      public String getName() {
          return name;
      }
  
      public Person setName(String name) {
          this.name = name;
          return this;
      }
  
      @Override
      public int hashCode() {
          int hash = 0;
          hash += (id != null ? id.hashCode() : 0);
          return hash;
      }
  
      @Override
      public boolean equals(Object object) {
          // TODO: Warning - this method won't work in the case the id fields are not set
          if (!(object instanceof Person)) {
              return false;
          }
          Person other = (Person) object;
          if ((this.id == null &amp;&amp; other.id != null) || (this.id != null &amp;&amp; !this.id.equals(other.id))) {
              return false;
          }
          return true;
      }
  
      @Override
      public String toString() {
          return "com.myhome.event_manager.entity.Person[ id=" + id + " ]";
      }
  
  }
  </code></pre>
  
  <p><strong>PersonDAO:</strong></p>
  
  <pre><code>@Repository
  public class PersonDAO extends AbstractDAO&lt;Person&gt; {
  
  
      public PersonDAO() {
          setClazz(Person.class);
      }
  
      public Person findByName(String name) {
          return em.createQuery("WHERE p.name = :name", Person.class)
              .setParameter("name", name)
              .getSingleResult();
      }
  }
  </code></pre>
  
  <p><strong>AbstractDAO:</strong></p>
  
  <pre><code>public abstract class AbstractDAO&lt;T extends Serializable&gt; {
  
  @PersistenceContext(unitName="pu-main")
  protected EntityManager em;
  protected Class&lt; T&gt; clazz;
  
  public void setClazz(final Class&lt; T&gt; clazzToSet) {
      this.clazz = clazzToSet;
  }
  
  public void setEm(EntityManager em) {
      this.em = em;
  }
  
  public EntityManager getEm() {
      return em;
  }
  
  public T findById(final Long id) {
      return this.em.find(this.clazz, id);
  }
  
  public List&lt; T&gt; findAll() {
      return this.em.createQuery("from " + this.clazz.getName())
              .getResultList();
  }
  
  public void create(final T entity) {
      this.em.persist(entity);
  }
  
  public void update(final T entity) {
      this.em.merge(entity);
  }
  
  public void delete(final T entity) {
      this.em.remove(entity);
  }
  
  public void deleteById(final Long entityId) {
      final T entity = this.findById(entityId);
      this.delete(entity);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>and simple <strong>PersonService</strong> which is in this case a wrapper to DAO with a @Transactional method createPerson:</p>
  
  <pre><code>@Service
  public class PersonService {
  
  @Autowired
  private PersonDAO personDao;
  
  @Transactional
  public void createPerson(Person person) {
      personDao.create(person);
  }
  
  public List&lt;Person&gt; listAllPersons() {
      return personDao.findAll();
  }
  
  public Person getPersonWithName(String name) {
      return personDao.findByName(name);
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>pom.xml:</strong></p>
  
  <pre><code>&lt;properties&gt;
      &lt;java-version&gt;1.6&lt;/java-version&gt;
      &lt;org.springframework-version&gt;3.2.0.RELEASE&lt;/org.springframework-version&gt;
      &lt;org.aspectj-version&gt;1.6.10&lt;/org.aspectj-version&gt;
      &lt;org.slf4j-version&gt;1.6.6&lt;/org.slf4j-version&gt;
      &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
      &lt;netbeans.hint.deploy.server&gt;Tomcat&lt;/netbeans.hint.deploy.server&gt;
  &lt;/properties&gt;
  &lt;dependencies&gt;
      &lt;!-- Spring --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework-version}&lt;/version&gt;
          &lt;exclusions&gt;
              &lt;!-- Exclude Commons Logging in favor of SLF4j --&gt;
              &lt;exclusion&gt;
                  &lt;groupId&gt;commons-logging&lt;/groupId&gt;
                  &lt;artifactId&gt;commons-logging&lt;/artifactId&gt;
              &lt;/exclusion&gt;
          &lt;/exclusions&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework-version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-orm&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework-version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!--Mysql driver--&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;mysql&lt;/groupId&gt;
          &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
          &lt;version&gt;5.1.21&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.hsqldb&lt;/groupId&gt;
          &lt;artifactId&gt;hsqldb&lt;/artifactId&gt;
          &lt;version&gt;2.2.9&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!--Hibernate--&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;
          &lt;version&gt;4.1.9.Final&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-validator&lt;/artifactId&gt;
          &lt;version&gt;4.3.1.Final&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate.javax.persistence&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-jpa-2.0-api&lt;/artifactId&gt;
          &lt;version&gt;1.0.1.Final&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- AspectJ --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.aspectj&lt;/groupId&gt;
          &lt;artifactId&gt;aspectjrt&lt;/artifactId&gt;
          &lt;version&gt;${org.aspectj-version}&lt;/version&gt;
      &lt;/dependency&gt;   
  
      &lt;!-- Logging --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
          &lt;artifactId&gt;slf4j-api&lt;/artifactId&gt;
          &lt;version&gt;${org.slf4j-version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
          &lt;artifactId&gt;jcl-over-slf4j&lt;/artifactId&gt;
          &lt;version&gt;${org.slf4j-version}&lt;/version&gt;
          &lt;scope&gt;runtime&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
          &lt;artifactId&gt;slf4j-log4j12&lt;/artifactId&gt;
          &lt;version&gt;${org.slf4j-version}&lt;/version&gt;
          &lt;scope&gt;runtime&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;log4j&lt;/groupId&gt;
          &lt;artifactId&gt;log4j&lt;/artifactId&gt;
          &lt;version&gt;1.2.15&lt;/version&gt;
          &lt;exclusions&gt;
              &lt;exclusion&gt;
                  &lt;groupId&gt;javax.mail&lt;/groupId&gt;
                  &lt;artifactId&gt;mail&lt;/artifactId&gt;
              &lt;/exclusion&gt;
              &lt;exclusion&gt;
                  &lt;groupId&gt;javax.jms&lt;/groupId&gt;
                  &lt;artifactId&gt;jms&lt;/artifactId&gt;
              &lt;/exclusion&gt;
              &lt;exclusion&gt;
                  &lt;groupId&gt;com.sun.jdmk&lt;/groupId&gt;
                  &lt;artifactId&gt;jmxtools&lt;/artifactId&gt;
              &lt;/exclusion&gt;
              &lt;exclusion&gt;
                  &lt;groupId&gt;com.sun.jmx&lt;/groupId&gt;
                  &lt;artifactId&gt;jmxri&lt;/artifactId&gt;
              &lt;/exclusion&gt;
          &lt;/exclusions&gt;
          &lt;scope&gt;runtime&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;!-- @Inject --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;javax.inject&lt;/groupId&gt;
          &lt;artifactId&gt;javax.inject&lt;/artifactId&gt;
          &lt;version&gt;1&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Servlet --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
          &lt;artifactId&gt;servlet-api&lt;/artifactId&gt;
          &lt;version&gt;2.5&lt;/version&gt;
          &lt;scope&gt;provided&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;javax.servlet.jsp&lt;/groupId&gt;
          &lt;artifactId&gt;jsp-api&lt;/artifactId&gt;
          &lt;version&gt;2.1&lt;/version&gt;
          &lt;scope&gt;provided&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;!--JSTL--&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;jstl&lt;/groupId&gt;
          &lt;artifactId&gt;jstl&lt;/artifactId&gt;
          &lt;version&gt;1.2&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Test --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;junit&lt;/groupId&gt;
          &lt;artifactId&gt;junit&lt;/artifactId&gt;
          &lt;version&gt;4.11&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.mockito&lt;/groupId&gt;
          &lt;artifactId&gt;mockito-all&lt;/artifactId&gt;
          &lt;version&gt;1.9.0&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.easytesting&lt;/groupId&gt;
          &lt;artifactId&gt;fest-assert&lt;/artifactId&gt;
          &lt;version&gt;1.4&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-test&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework-version}&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
          &lt;type&gt;jar&lt;/type&gt;
      &lt;/dependency&gt;
  &lt;/dependencies&gt;
  </code></pre>
  
  <p>All above works along with configuration included in root-context.xml:</p>
  
  <p><strong>DataSource.xml:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"&gt;
  
  &lt;!--MysQL - main--&gt;
  &lt;bean id="dataSource" 
        class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
      &lt;property name="driverClassName" value="${jdbc.driverClassName}" /&gt;
      &lt;property name="url" value="${jdbc.url}" /&gt;
      &lt;property name="username" value="${jdbc.username}" /&gt;
      &lt;property name="password" value="${jdbc.password}" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
      &lt;property name="database" value="MYSQL" /&gt;
      &lt;property name="showSql" value="true"/&gt;
      &lt;property name="generateDdl" value="true"/&gt;
      &lt;property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="emFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;property name="jpaVendorAdapter" ref="jpaVendorAdapter" /&gt;
      &lt;property name="persistenceUnitName" value="pu-main" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>Transactions.xml:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:tx="http://www.springframework.org/schema/tx"
  
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"&gt;
  
  &lt;!--enable the configuration of transactional behavior based on annotations--&gt; 
  &lt;tx:annotation-driven transaction-manager="txManager" /&gt;
  
  &lt;!--Transactions--&gt;
  &lt;bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /&gt;
  
  &lt;bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt;
      &lt;property name="dataSource" ref="dataSource"/&gt;
  &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>persistence.xml:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="1.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_1_0.xsd"&gt;
    &lt;persistence-unit name="pu-main" transaction-type="RESOURCE_LOCAL"&gt;
      &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
      &lt;non-jta-data-source/&gt;
      &lt;properties/&gt;
    &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>Next I made a jUnit test that creates new Person instance, and calls create method of PersonService which should pass it down to DAO and persistence layer.</p>
  
  <p>Test:</p>
  
  <pre><code>public class PersonServiceTest extends RootContextAwareTest {
  
  @Autowired
  private PersonService instance;
  
  @Test
  public void shouldAddOnePerson() {
      logger.debug("TEST-shouldAddOnePerson");
  
      //given
      Person person = (new Person())
              .setName("Test");
  
      //when
      logger.debug("BEFORE CREATE");
      instance.createPerson(person);
      logger.debug("AFTER CREATE");
  
  
      //then
      assertThat(instance.listAllPersons()).hasSize(6);
      assertThat(instance.getPersonWithName("Test"));
  }
  </code></pre>
  
  <p>}</p>
  
  <p>RootContextAwareTest:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = {"classpath:/config/spring/root-context.xml"})
  public abstract class RootContextAwareTest {
       protected static final Logger logger = LoggerFactory.getLogger(RootContextAwareTest.class);
  }
  </code></pre>
  
  <p>Acctually there are 5 rows in person table so I simply put an assertion after persisting of new entity that checks if it was persisted properly. As it turned out it wasn't and assertion failed but to my amazement logs say that everything goes fine:</p>
  
  <pre><code>    2013-01-28 21:28:07,148 - DEBUG: com.myhome.event_manager.test_base.RootContextAwareTest - TEST-shouldAddOnePerson
  2013-01-28 21:28:07,148 - DEBUG: com.myhome.event_manager.test_base.RootContextAwareTest - BEFORE CREATE
  2013-01-28 21:28:07,154 - DEBUG: org.springframework.jdbc.datasource.DataSourceTransactionManager - Creating new transaction with name [com.myhome.event_manager.service.PersonService.createPerson]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
  2013-01-28 21:28:07,155 - DEBUG: org.springframework.jdbc.datasource.DriverManagerDataSource - Creating new JDBC DriverManager Connection to [jdbc:mysql://localhost:3306/event_manager]
  2013-01-28 21:28:07,172 - DEBUG: org.springframework.jdbc.datasource.DataSourceTransactionManager - Acquired Connection [com.mysql.jdbc.JDBC4Connection@164a38ae] for JDBC transaction
  2013-01-28 21:28:07,178 - DEBUG: org.springframework.jdbc.datasource.DataSourceTransactionManager - Switching JDBC Connection [com.mysql.jdbc.JDBC4Connection@164a38ae] to manual commit
  2013-01-28 21:28:07,208 - DEBUG: org.springframework.orm.jpa.EntityManagerFactoryUtils - Opening JPA EntityManager
  2013-01-28 21:28:07,249 - DEBUG: org.springframework.orm.jpa.EntityManagerFactoryUtils - Registering transaction synchronization for JPA EntityManager
  2013-01-28 21:28:07,267 - DEBUG: org.springframework.orm.jpa.EntityManagerFactoryUtils - Closing JPA EntityManager
  2013-01-28 21:28:07,268 - DEBUG: org.springframework.jdbc.datasource.DataSourceTransactionManager - Initiating transaction commit
  2013-01-28 21:28:07,269 - DEBUG: org.springframework.jdbc.datasource.DataSourceTransactionManager - Committing JDBC transaction on Connection [com.mysql.jdbc.JDBC4Connection@164a38ae]
  2013-01-28 21:28:07,269 - DEBUG: org.springframework.jdbc.datasource.DataSourceTransactionManager - Releasing JDBC Connection [com.mysql.jdbc.JDBC4Connection@164a38ae] after transaction
  2013-01-28 21:28:07,269 - DEBUG: org.springframework.jdbc.datasource.DataSourceUtils - Returning JDBC Connection to DataSource
  2013-01-28 21:28:07,270 - DEBUG: com.myhome.event_manager.test_base.RootContextAwareTest - AFTER CREATE
  </code></pre>
  
  <p>So I thought (after some research) that test env doesn't allow any changes to db by default (which would be quite logical).
  So my second attempt was to add annotations:</p>
  
  <pre><code>@Transactional
  @TransactionConfiguration(transactionManager = "txManager", defaultRollback = false)
  </code></pre>
  
  <p>on a PersonServiceTest class. Result was like just like before transaction created, committed, and released without any error. I've made up some research again and made a simple controller and put there code which should create and persist new person entity.</p>
  
  <p><strong>MainController:</strong></p>
  
  <pre><code>@Controller
  public class MainController {
  
      private static final Logger logger = LoggerFactory.getLogger(MainController.class);
      private ApplicationContext context = new ClassPathXmlApplicationContext("/config/spring/root-context.xml");
      @Autowired
      private PersonService ps;
  
      @RequestMapping(method = RequestMethod.GET, value = "/")
      public String index(Model model) {
          Person person = (new Person())
                  .setName("Test-ąęć");
  
          logger.debug("PERSIST PERSON");
          ps.createPerson(person);
          return "forward:/event/read";
      }
  }
  </code></pre>
  
  <p>The result was the same as above.</p>
  
  <p>I've tried also putting @Transactional annotation do DAO (class and method level) and controller. </p>
  
  <p>That kind of behavior should point on problem with configuration of persistence layer but selecting of data from db works fine.</p>
  
  <p>Also I've come across opionion that hibernate may have some delay before writing data to db so I've added thread.sleep(5000) in test but it doesn't change much.</p>
  
  <p>Maybe I've missed something in configuration but I've run out of ideas right now.</p>
  
  <p>I would be appreciated for any help.</p>