title: Use Spring and EJB transaction together
body: <p>I am getting the error while try to add the entity into the DB. I have referred <a href="https://stackoverflow.com/questions/39734796/jpa-concurrency-issue-on-release-of-batch-it-still-contained-jdbc-statements">this</a>. But it did not have much info regarding my issue</p>
  
  <p>I have one EJB jar which I use to manage the get,save and update the entity.</p>
  
  <p>Here is my remote ejb</p>
  
  <pre><code>@Remote
  public abstract interface DatalayerService{
     public abstract void add(Object object)
  }
  </code></pre>
  
  <p>Here is the implemetation</p>
  
  <pre><code>@TransactionManagement(TransactionManagementType.CONTAINER)
  @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
  public class DatalayerServiceImpl implements DatalayerService{
    @PersistenceContext(name="myPersistenceUnit")
     EntityManager em = null;
  
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
      public void add(Object object) throws FatalException{
          try {
              em.persist(object);
          }
          catch (Throwable e){
              throw manageDatalayerError(e);
          }
          finally {
  
          }
      }
  }
  </code></pre>
  
  <p>I am trying to use the above ejb in my application</p>
  
  <p>1)</p>
  
  <pre><code>@ComponentScan({"com.springboot"})
  @EnableJpaRepositories
  @SpringBootApplication
  public class SpringEjbApplication extends SpringBootServletInitializer{
  
  
      public static void main(String args[]){
          SpringApplication.run(SpringApplication.class,args);
      }
  
  
  
      @Bean
      public DatalayerService datalayerService() throws NamingException{
          return new DatalayerServiceImpl();
      }
  
  
      @Bean
      public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws NamingException{
          LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
          em.setDataSource(dataSource());
          em.setPackagesToScan(new String[]{"com.springboot.pojo"});
          JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
          em.setPersistenceProviderClass(HibernatePersistenceProvider.class);
          em.setJpaVendorAdapter(vendorAdapter);
          em.setPersistenceUnitName("myPersistenceUnit");
          em.setJpaProperties(additionalProperties());
          return em;
      }
  
  
  
      @Bean
      public DataSource dataSource() throws NamingException{
          return (DataSource) new JndiTemplate().lookup("openejb:Resource/MyDataSource");
      }
  
      @Bean
      public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
          JpaTransactionManager transactionManager = new JpaTransactionManager();
          transactionManager.setEntityManagerFactory(emf);
          return transactionManager;
      }
  
  
      @Bean
      public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
          return new PersistenceExceptionTranslationPostProcessor();
      }
  
  
      private Properties additionalProperties(){
          Properties properties = new Properties();
          properties.setProperty("hibernate.max_fetch_depth", "3");
          properties.setProperty("hibernate.default_batch_fetch_size", "2");
          properties.setProperty("hibernate.jdbc.batch_size", "100");
          properties.setProperty("hibernate.show_sql", "true");
          properties.setProperty("hibernate.format_sql", "false");
          return properties;
      }
  }
  </code></pre>
  
  <p>2)Rest Service</p>
  
  <pre><code>@RestController
  public class HomeEndPoint{
  
      @Autowired
      private IUserService iUserService;
  
      @GetMapping("/createUser")
      @Transactional(rollbackFor = {ServiceExpectedException.class,FatalException.class,MandatoryParameterMissingFatalException.class})
      public void createUser() throws FatalException,ServiceExpectedException{
          iUserService.createUser();
      }
  
  }
  </code></pre>
  
  <p>3)User Service and its impl</p>
  
  <pre><code>public interface IUserService{
     public void createUser() throws FatalException;
  }
  
      @Service
  @Transactional(rollbackFor = {ServiceExpectedException.class,FatalException.class,MandatoryParameterMissingFatalException.class})
  public class UserServiceImpl implements IUserService{
       @Autowired
      private DatalayerGenericService datalayerGenericService;
  
       @Override
      public void createUser() throws FatalException,ServiceExpectedException{
       Team team = new Team(simpleContextService);
          team.setGroupName("MyTeam");
          team.setStoreId(100);
          // Team
          datalayerGenericService.add(team);
          Log.info(this,"add team ");
          // Build user1
          datalayerGenericService.add(user1);
          Log.info(this,"############################# Added User1 ######################################");
          // Build user2
          datalayerGenericService.add(user2);
          Log.info(this,"############################# Added User2 ######################################");
  
  }
  }
  </code></pre>
  
  <p>I tried to run the rest service with url</p>
  
  <pre><code>http://localhost:6180/SpringBootDatalayer/createUser
  </code></pre>
  
  <p>I am getting the below exception.</p>
  
  <blockquote>
    <p>INFO: (com.edifixio.springboot.service.impl.UserServiceImpl)</p>
    
    ####################### teamOid 20 INFO: (com.edifixio.springboot.service.impl.UserServiceImpl)
    
    ####################### Added User1 ##################################### INFO: (com.edifixio.springboot.service.impl.UserServiceImpl)
    
    ####################### Added User2 ##################################### INFO : http-nio-6180-exec-1 : AbstractBatchImpl.release :    HHH000010: On release of batch it still
    
    <p>contained JDBC statements 2018-03-27 16:28:17,683 : ERROR :
    http-nio-6180-exec-1 : BatchingBatch.performExecution :    HHH000315:
    Exception executing batch [java.sql.BatchUpdateException: Batch entry
    0 insert into TEAM (CREATE_TIMESTAMP, PROJECT_GROUP_NAME,
    LAST_UPDATE_TIMESTAMP, STORE_ID, ID) values ('2018-3-27
    16:28:17.447000 +5:30:0', 'MMA_TEST', '2018-3-27 16:28:17.503000
    +5:30:0', 100, 21) was aborted.  Call getNextException to see the cause.], SQL: insert into TEAM (CREATE_TIMESTAMP, PROJECT_GROUP_NAME,
    LAST_UPDATE_TIMESTAMP, STORE_ID, ID) values (?, ?, ?, ?, ?)</p>
  </blockquote>
  
  <p>As you can see from the exception It was creating team,user1 and user2 with the use of datalayerGenericService. But tries to executes again to give AbstractBatchImpl.release :    HHH000010: On release of batch it still contained JDBC statements.</p>
  
  <p>Here is the exception I got:</p>
  
  <blockquote>
    <p>java.sql.BatchUpdateException: Batch entry 1 insert into TEAM
    (CREATE_TIMESTAMP, PROJECT_GROUP_NAME, LAST_UPDATE_TIMESTAMP,
    STORE_ID, ID) values ('2018-3-29 12:9:26.611000 +5:30:0', 'MMA_TEST',
    '2018-3-29 12:9:26.836000 +5:30:0', 100, 23) was aborted.  Call
    getNextException to see the cause.</p>
  </blockquote>
  
  <p>How do I resolve this issue? Why the query executing twice?</p>
  
  <p>If there is any exception occurs in EJB methods, it should rollback the transaction. But it is not happening</p>
  
  <p>Note : If I remove @Transactional annotation from the service it works fine. 
  I am using TomEE 7.0.2 server.</p>