title: How to insert into database when using JTA + JPA with Spring
body: <p>I have a simple web application using JTA + JPA along with Spring (version 4).
  I am doing a XA transaction using Atomikos JTA transaction Manager. This transaction involves inserting EmployeeA entity into DatabaseA and another EmployeeB entity into another DatabaseB.</p>
  
  <p>Strangely, the application does not throw any error <strong>but no enteries are inserted into the Databases.</strong></p>
  
  <pre><code>public class JtaTest {
  
  public static void main( String[] args ) {
  
      JtaTest test = new JtaTest();
      test.testMandatory();
  }
  
  private static void testMandatory() {
  
      final SpringContextUtil util = SpringContextUtil.instance();
      ApplicationContext ctx = util.getApplicationContext();
  
  
      final JtaEmployeeService employeeService = (JtaEmployeeService)ctx.getBean("jtaEmployeeService");
      EmployeeA a = new EmployeeA();
      a.setName("emp-a-1");
      a.setAge(30);
  
      EmployeeB b = new EmployeeB();
      a.setName("emp-b-1");
      a.setAge(31);
  
     try {
      employeeService.persistEmployees(a, b);
      } catch (Exception e) {
      e.printStackTrace();
  }   
     System.out.println("success");
  }
  }
  </code></pre>
  
  <hr>
  
  <p><strong>The service class implementation:</strong></p>
  
  <pre><code>@Service("jtaEmployeeService")
  public class JtaEmployeeServiceImpl implements JtaEmployeeService {
  
  @Autowired
  @Qualifier("employeea")
  private GenericDao&lt;Integer, EmployeeA&gt; employeeADao;
  
  
  @Autowired
  @Qualifier("employeeb")
  private GenericDao&lt;Integer, EmployeeB&gt; employeeBDao;
  
  @Override
  
  @Transactional( propagation=Propagation.REQUIRED, readOnly=false, isolation=Isolation.DEFAULT, rollbackFor=Exception.class)
  public void persistEmployees(EmployeeA employeeA, EmployeeB employeeB)  throws Exception {
      employeeADao.save(employeeA);
      employeeBDao.save(employeeB);
      System.out.println("Saving employee A and employee B ");
  
  
  }
  }
  </code></pre>
  
  <p><strong>The DAO implementation and interfaces:</strong></p>
  
  <hr>
  
  <pre><code>@Repository("employeeb")
  public class EmployeeBDaoImpl extends AbstractJTADaoDatabaseB implements
      GenericDao&lt;Integer, EmployeeB&gt; {
  
  @Override
  public void save(EmployeeB entity) {
      super.persist(entity);
  
  }
  }
  
  @Repository("employeea")
  public class EmployeeADaoImpl extends AbstractJTADaoDatabaseA implements
      GenericDao&lt;Integer, EmployeeA&gt; {
  
  @Override
  public void save(EmployeeA entity) {
      super.persist(entity);
  
  }
  }
  
  public abstract class AbstractJTADaoDatabaseB {
  
  @PersistenceContext(unitName = "persistenceUnitB")
  @Qualifier("myManager")
  private EntityManager entityManager;
  
  public void persist(Object entity) {
      entityManager.persist(entity);
  
      // entityManager.flush();
  }
  
  protected EntityManager getEntityManager() {
      return entityManager;
  }
  
  }
  
  public abstract class AbstractJTADaoDatabaseA {
  
  @PersistenceContext(unitName = "persistenceUnitA")
  @Qualifier("myManager")
  private EntityManager entityManager;
  
  public void persist(Object entity) {
      entityManager.persist(entity);
      // entityManager.flush();
  }
  
  protected EntityManager getEntityManager() {
      return entityManager;
  }
  
  }
  </code></pre>
  
  <p><strong>Servlet-Context.xml</strong></p>
  
  <hr>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans:beans xmlns="http://www.springframework.org/schema/mvc"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:beans="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"&gt;
  
   &lt;!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --&gt;
   
   &lt;!-- Enables the Spring MVC @Controller programming model --&gt;
   &lt;annotation-driven /&gt;
   
   &lt;!--  Enables annotaion driven transactional support on springs --&gt;
   &lt;!-- &lt;tx:annotation-driven transaction-manager="hTransactionManager"/&gt; --&gt;
   &lt;!--  &lt;tx:jta-transaction-manager transaction-manager="jtaTransactionManager"/&gt; --&gt;
   &lt;tx:annotation-driven transaction-manager="jtaTransactionManager"/&gt;
  
  &lt;!--  list the packages which are annotated with springs annotaions like @controller, @repository, @service, @component only --&gt;
   &lt;context:component-scan base-package="com.goraksh.spring.tutorial.controller.business, com.goraksh.spring.tutorial.controller.rest, com.goraksh.spring.tutorial.service, com.goraksh.spring.tutorial.dao" /&gt;
  
  &lt;beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
        &lt;beans:property name="prefix" value="/WEB-INF/views/" /&gt;
        &lt;beans:property name="suffix" value=".jsp" /&gt;
     &lt;/beans:bean&gt;
     
  
   &lt;beans:bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;
      &lt;beans:property name="location" value="classpath:application.properties"/&gt;
  &lt;/beans:bean&gt;
  
  &lt;!--  &lt;beans:bean id="sessionFactory" class="org.springframework.orm.hibernate4.annotation.AnnotationSessionFactoryBean"&gt;  --&gt;
  &lt;beans:bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
      &lt;beans:property name="dataSource" ref="dataSource" /&gt;
      &lt;beans:property name="hibernateProperties"&gt;
        &lt;beans:props&gt;
          &lt;beans:prop key="hibernate.dialect"&gt;org.hibernate.dialect.SQLServerDialect&lt;/beans:prop&gt;
         
         &lt;!--  &lt;beans:prop key="hibernate.current_session_context_class"&gt;thread&lt;/beans:prop&gt; --&gt;
           &lt;beans:prop key="hibernate.show_sql"&gt;${hibernate.show_sql}&lt;/beans:prop&gt;
            &lt;beans:prop key="hibernate.format_sql"&gt;${hibernate.format_sql}&lt;/beans:prop&gt;
             &lt;beans:prop key="hibernate.cache.region.factory_class"&gt;${hibernate.cache.region.factory_class}&lt;/beans:prop&gt;
              &lt;beans:prop key="hibernate.cache.use_second_level_cache"&gt;${hibernate.cache.use_second_level_cache}&lt;/beans:prop&gt;
               &lt;beans:prop key="hibernate.cache.use_query_cache"&gt;${hibernate.cache.use_query_cache}&lt;/beans:prop&gt;
                &lt;beans:prop key="net.sf.ehcache.configurationResourceName"&gt;${net.sf.ehcache.configurationResourceName}&lt;/beans:prop&gt;
         
        &lt;/beans:props&gt;
      &lt;/beans:property&gt;
     
         &lt;!--&lt;beans:property name="annotatedClasses"&gt;  --&gt;
    &lt;!--  packagesToScan is meant to give the package where the javax.persistence.Entity classes are stored , recognised via their annotations--&gt;
    &lt;beans:property name="packagesToScan"&gt;
        &lt;beans:list&gt;
          &lt;beans:value&gt;com.goraksh.spring.tutorial.model&lt;/beans:value&gt;
        &lt;/beans:list&gt;
      &lt;/beans:property&gt;
      
    &lt;/beans:bean&gt;
  
  &lt;beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
      &lt;beans:property name="driverClassName" value="${jdbc.driverClassName}"/&gt;
      &lt;beans:property name="url" value="${jdbc.url}"/&gt;
      &lt;beans:property name="username" value="${jdbc.username}"/&gt;
      &lt;beans:property name="password" value="${jdbc.password}"/&gt;
  &lt;/beans:bean&gt;
  
      
      &lt;beans:bean id = "hTransactionManager" class = "org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
      &lt;beans:property name = "sessionFactory" ref = "sessionFactory" /&gt;
  &lt;/beans:bean&gt;   
   
  &lt;!--  Jata transactions with Atomikos  --&gt;
  &lt;beans:bean id="dataSourceA" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close"&gt;
   &lt;beans:property name="uniqueResourceName"&gt;&lt;beans:value&gt;DataSourceA&lt;/beans:value&gt;&lt;/beans:property&gt;
   &lt;beans:property name="xaDataSourceClassName"&gt;&lt;beans:value&gt;com.mysql.jdbc.jdbc2.optional.MysqlXADataSource&lt;/beans:value&gt;&lt;/beans:property&gt;
   &lt;beans:property name="xaProperties"&gt;
       &lt;beans:props&gt;
    &lt;beans:prop key="databaseName"&gt;traningdb&lt;/beans:prop&gt;
    &lt;beans:prop key="serverName"&gt;localhost&lt;/beans:prop&gt;
    &lt;beans:prop key="port"&gt;3306&lt;/beans:prop&gt;
    &lt;beans:prop key="user"&gt;root&lt;/beans:prop&gt;
    &lt;beans:prop key="password"&gt;root&lt;/beans:prop&gt;
    &lt;beans:prop key="url"&gt;jdbc:mysql://localhost:3306/traningdb&lt;/beans:prop&gt;
       &lt;/beans:props&gt;
   &lt;/beans:property&gt;
   &lt;beans:property name="minPoolSize"&gt;&lt;beans:value&gt;1&lt;/beans:value&gt;&lt;/beans:property&gt;
  &lt;/beans:bean&gt;
   
  &lt;beans:bean id="dataSourceB" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close"&gt;
   &lt;beans:property name="uniqueResourceName"&gt;&lt;beans:value&gt;DataSourceB&lt;/beans:value&gt;&lt;/beans:property&gt;
   &lt;beans:property name="xaDataSourceClassName"&gt;&lt;beans:value&gt;com.mysql.jdbc.jdbc2.optional.MysqlXADataSource&lt;/beans:value&gt;&lt;/beans:property&gt;
   &lt;beans:property name="xaProperties"&gt;
       &lt;beans:props&gt;
    &lt;beans:prop key="databaseName"&gt;traningdb2&lt;/beans:prop&gt;
    &lt;beans:prop key="serverName"&gt;localhost&lt;/beans:prop&gt;
    &lt;beans:prop key="port"&gt;3306&lt;/beans:prop&gt;
    &lt;beans:prop key="user"&gt;root&lt;/beans:prop&gt;
    &lt;beans:prop key="password"&gt;root&lt;/beans:prop&gt;
    &lt;beans:prop key="url"&gt;jdbc:mysql://localhost:3306/traningdb2&lt;/beans:prop&gt;
       &lt;/beans:props&gt;
   &lt;/beans:property&gt;
   &lt;beans:property name="minPoolSize"&gt;
   &lt;beans:value&gt;1&lt;/beans:value&gt;
   &lt;/beans:property&gt;
  &lt;/beans:bean&gt;
  
  &lt;beans:bean id="entityManagerFactoryA" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
   &lt;beans:property name="persistenceXmlLocation"&gt;
              &lt;beans:value&gt;classpath:META-INF/persistence.xml&lt;/beans:value&gt;
      &lt;/beans:property&gt;
    &lt;beans:property name="persistenceUnitName" value="persistenceUnitA" /&gt;
    &lt;beans:property name="dataSource" ref="dataSourceA" /&gt;
    &lt;beans:property name="jpaVendorAdapter"&gt;
     &lt;beans:bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
      &lt;beans:property name="showSql" value="true" /&gt;
      &lt;beans:property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" /&gt;
     &lt;/beans:bean&gt;
    &lt;/beans:property&gt;
   &lt;/beans:bean&gt;
   
   &lt;beans:bean id="entityManagerFactoryB" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
   &lt;beans:property name="persistenceXmlLocation"&gt;
              &lt;beans:value&gt;classpath:META-INF/persistence.xml&lt;/beans:value&gt;
          &lt;/beans:property&gt;
    &lt;beans:property name="persistenceUnitName" value="persistenceUnitB" /&gt;
    &lt;beans:property name="dataSource" ref="dataSourceB" /&gt;
    &lt;beans:property name="jpaVendorAdapter"&gt;
     &lt;beans:bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
      &lt;beans:property name="showSql" value="true" /&gt;
      &lt;beans:property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" /&gt;
     &lt;/beans:bean&gt;
    &lt;/beans:property&gt;
   &lt;/beans:bean&gt;
  
   &lt;beans:bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"  init-method="init" destroy-method="close"&gt;
    &lt;beans:property name="forceShutdown" value="false" /&gt;
    &lt;!--  &lt;beans:property name="startupTransactionService" value="false" /&gt; --&gt;
   &lt;/beans:bean&gt;
  
   &lt;beans:bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp"&gt;
    &lt;beans:property name="transactionTimeout" value="300" /&gt;
   &lt;/beans:bean&gt;
  
   &lt;beans:bean id="jtaTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"
    depends-on="atomikosTransactionManager,atomikosUserTransaction"&gt;
    &lt;beans:qualifier value="myManager"/&gt;
    &lt;beans:property name="transactionManager" ref="atomikosTransactionManager" /&gt;
    &lt;beans:property name="userTransaction" ref="atomikosUserTransaction" /&gt;
    &lt;beans:property name="allowCustomIsolationLevels" value="true" /&gt;
   &lt;/beans:bean&gt;
  
   
  &lt;/beans:beans&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <hr>
  
  <p><strong>Persistence.xml</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><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="persistenceUnitA" &gt;
    &lt;class&gt;com.goraksh.spring.tutorial.model.EmployeeA&lt;/class&gt;
    &lt;exclude-unlisted-classes /&gt;
    &lt;properties&gt;
  
  
    &lt;property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory" /&gt;
  
    &lt;/properties&gt;
   &lt;/persistence-unit&gt;
    
   &lt;persistence-unit name="persistenceUnitB" &gt;
    &lt;class&gt;com.goraksh.spring.tutorial.model.EmployeeB&lt;/class&gt;
    &lt;exclude-unlisted-classes /&gt;
    &lt;properties&gt;
  
     &lt;property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory" /&gt;
  
    &lt;/properties&gt;
   &lt;/persistence-unit&gt;
    
  &lt;/persistence&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>Any help is highly solicited.</p>