title: Inserting data into db is not working with Spring Data JPA + Eclipselink + JTA
body: <p>I'm trying to configure multiple datasources with spring data jpa + eclipselink, seems like using jta is my only choice, after lots of research on this topic, I got below output, that said I am trying to insert two entity into two tables from different db -- department table and employee table. Query is fine but insertion operations are not working without errors thrown.</p>
  
  <p>Pls take a look at my code</p>
  
  <p>app-context.xml:</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:context="http://www.springframework.org/schema/context"
         xmlns:jdbc="http://www.springframework.org/schema/jdbc"
         xmlns:jpa="http://www.springframework.org/schema/data/jpa"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xmlns:jee="http://www.springframework.org/schema/jee"
         xmlns:aop="http://www.springframework.org/schema/aop"
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
          http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
          http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
          http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd"&gt;
  
      &lt;context:property-placeholder location="classpath:jdbc.properties"/&gt;
  
      &lt;!--&lt;jee:jndi-lookup jndi-name="test" id="dataSource" resource-ref="true"/&gt;--&gt;
  
      &lt;bean abstract="true" id="baseDS" class="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" lazy-init="true"&gt;
          &lt;property name="pinGlobalTxToPhysicalConnection" value="true" /&gt;
          &lt;property name="user" value="root" /&gt;
          &lt;property name="password" value="root" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="dataSource1" parent="baseDS"&gt;
          &lt;property name="url"&gt;
              &lt;value&gt;${jdbc1.url}&lt;/value&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="dataSource2" parent="baseDS"&gt;
          &lt;property name="url"&gt;
              &lt;value&gt;${jdbc2.url}&lt;/value&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="baseJTADS" class="com.atomikos.jdbc.AtomikosDataSourceBean" abstract="true" init-method="init" destroy-method="close"&gt;
          &lt;property name="poolSize" value="3" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="jtaDs1" parent="baseJTADS"&gt;
          &lt;property name="uniqueResourceName" value="XA1DBMS" /&gt;
          &lt;property name="xaDataSource" ref="dataSource1" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="jtaDs2" parent="baseJTADS"&gt;
          &lt;property name="uniqueResourceName" value="XA2DBMS" /&gt;
          &lt;property name="xaDataSource" ref="dataSource2" /&gt;
      &lt;/bean&gt;
  
  
  
      &lt;bean abstract="true" id="baseEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
          &lt;property name="jpaVendorAdapter"&gt;
              &lt;bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter"/&gt;
          &lt;/property&gt;
          &lt;property name="packagesToScan" value="org.fxbird.springjpa.model"/&gt;
          &lt;property name="jpaProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="databasePlatform"&gt;org.eclipse.persistence.platform.database.MySQLPlatform&lt;/prop&gt;
                  &lt;prop key="eclipselink.jdbc.cache-statements"&gt;true&lt;/prop&gt;
                  &lt;prop key="eclipselink.weaving"&gt;false&lt;/prop&gt;
                  &lt;prop key="eclipselink.logging.level"&gt;fine&lt;/prop&gt;
                  &lt;prop key="eclipselink.allow-zero-id"&gt;true&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="emfCompany" parent="baseEmf"&gt;
          &lt;!--&lt;property name="persistenceUnitName" value="company" /&gt;--&gt;
          &lt;property name="dataSource" ref="jtaDs1"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="emfEmp" parent="baseEmf"&gt;
          &lt;!--&lt;property name="persistenceUnitName" value="employee" /&gt;--&gt;
          &lt;property name="dataSource" ref="jtaDs2"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp"&gt;
          &lt;property name="transactionTimeout" value="300"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init"
            destroy-method="close"&gt;
          &lt;property name="forceShutdown" value="true"/&gt;
          &lt;property name="transactionTimeout" value="300"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"&gt;
          &lt;property name="transactionManager" ref="atomikosTransactionManager"/&gt;
          &lt;property name="userTransaction" ref="atomikosUserTransaction"/&gt;
          &lt;property name="allowCustomIsolationLevels" value="true"/&gt;
      &lt;/bean&gt;
  
      &lt;context:component-scan base-package="org.fxbird.springjpa.service"/&gt;
  
      &lt;jpa:repositories base-package="org.fxbird.springjpa.repo.company"
                        entity-manager-factory-ref="emfCompany" transaction-manager-ref="transactionManager"&gt;
      &lt;/jpa:repositories&gt;
  
      &lt;jpa:repositories base-package="org.fxbird.springjpa.repo.emp"
                        entity-manager-factory-ref="emfEmp" transaction-manager-ref="transactionManager"&gt;
      &lt;/jpa:repositories&gt;
  
      &lt;tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" /&gt;
  
      &lt;aop:aspectj-autoproxy proxy-target-class="true" /&gt;
  
      &lt;context:annotation-config/&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>DeptRepo:</p>
  
  <pre><code>@Repository()
  public interface DeptRepo extends JpaRepository&lt;Department,Integer&gt; {
  
  }
  </code></pre>
  
  <p>EmployeeRepo:</p>
  
  <pre><code>public interface EmployeeRepo extends JpaRepository&lt;Employee,Integer&gt; {
  }
  </code></pre>
  
  <p>Service:</p>
  
  <pre><code>@Service
  public class DepartmentService {
      @Autowired
      DeptRepo deptRepo;
      @Autowired
      EmployeeRepo employeeRepo;
  
      @Transactional
      public void addDepartment(Department department){
          deptRepo.save(department);
      }
  
      public List&lt;Department&gt; findAllDepartments(){
          return deptRepo.findAll();
      }
  
      @Transactional
      public void addEmployee(Employee employee){
          employeeRepo.save(employee);
      }
  }
  </code></pre>
  
  <p>test class:</p>
  
  <pre><code>public class App {
      public static void main(String[] args) {
          GenericXmlApplicationContext context=new GenericXmlApplicationContext("/app-context.xml");
  
          DepartmentService departmentService=(DepartmentService)context.getBean("departmentService");
  
          Department department=new Department();
          department.setCreateDate(new Date());
          department.setDeptName("HR");
  
          departmentService.addDepartment(department); //not working at all!!!
  
          List&lt;Department&gt; allDepartments = departmentService.findAllDepartments();
          System.out.println(allDepartments.size());
  
          Employee employee=new Employee();
          employee.setEmpName("Kurt");
          employee.setDeptNo(department.getDeptNo());
          departmentService.addEmployee(employee); //not working at all!!!
      }
  }
  </code></pre>
  
  <p>there are two db -- company and employee which has table department and employee respectively, the structure as below:</p>
  
  <pre><code>CREATE TABLE `department` (
    `dept_no` int(11) NOT NULL AUTO_INCREMENT,
    `dept_name` varchar(255) DEFAULT NULL,
    `create_date` datetime DEFAULT NULL,
    PRIMARY KEY (`dept_no`)
  )
  
  CREATE TABLE `employee` (
    `emp_no` int(11) NOT NULL AUTO_INCREMENT,
    `emp_name` varchar(255) DEFAULT NULL,
    `dept_no` int(11) DEFAULT NULL,
    `create_date` datetime DEFAULT NULL,
    PRIMARY KEY (`emp_no`)
  )
  </code></pre>
  
  <p>Can anyone throw a light on this? I had a hard time figuring out the reason, thanks</p>
  
  <p>Kurt</p>