0

trying to find what went wrong with my code which worked fine until i moved to JTAtransactionManager it is having issue saving the record in database but fetching the record working fine, below is my sample TransactionConfig class and service class method.

@Configuration
@ComponentScan
@EnableTransactionManagement
public class TransactionConfig {


    @Bean(name = "userTransaction")
    public UserTransaction userTransaction() throws Throwable {
        UserTransactionImp userTransactionImp = new UserTransactionImp();
        //userTransactionImp.setTransactionTimeout(10000);
        return userTransactionImp;
    }

    @Bean(name = "atomikosTransactionManager", initMethod = "init", destroyMethod = "close")
    public TransactionManager atomikosTransactionManager() throws Throwable {
        UserTransactionManager userTransactionManager = new UserTransactionManager();
        userTransactionManager.setForceShutdown(false);

        return userTransactionManager;
    }

    @Bean(name = "transactionManager")
    @DependsOn({ "userTransaction", "atomikosTransactionManager" })
    public PlatformTransactionManager transactionManager() throws Throwable {
        UserTransaction userTransaction = userTransaction();

        TransactionManager atomikosTransactionManager = atomikosTransactionManager();
        return new JtaTransactionManager(userTransaction, atomikosTransactionManager);
    }

}


---Employee Service Class Method---

@Transactional
public void appExample() {

    try {

        Employee emp = new Employee();

        emp.setFirstName("Veer");
        emp.setLastName("kumar");

        empRepo.save(emp);
    } catch (Exception e) {
        log.error(e);
    }

}
veeru
  • 161
  • 1
  • 1
  • 4

1 Answers1

0

I think issue is with empRepo.save() method call. This call is not committing any changes to database as you are using @Transactional for transaction management.

Please try with empRepo.saveAndFlush() which will immediately flush the data into database. You can refer answer Difference between save and saveAndFlush in Spring data jpa

Abhijeet
  • 4,069
  • 1
  • 22
  • 38