In Spring Boot application I have configured 2 different transaction managers:
@Bean("jpaTransactionManager")
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory);
return transactionManager;
}
@Bean("neo4jTransactionManager")
public Neo4jTransactionManager transactionManager() {
return new Neo4jTransactionManager(sessionFactory());
}
Also, I have a separate services for PostgreSQL and Neo4j databases:
@Service("postgreSQLUserService")
@Transactional("jpaTransactionManager")
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
@Override
public User create() {
return userRepository.save(new User());
}
.....
@Service("neo4jUserService")
@Transactional("neo4jTransactionManager")
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
@Override
public User create() {
return userRepository.save(new User());
}
Separately such services work like expected and correctly communicate with PostgreSQl and Neo4j database. Right now I need to configure something like XA transactions. I need to be able in scope of one method to atomically create users in PostgreSQL and Neo4j database. In case one of the operation fail, I need also to rollback changes from another database, something like that:
@Transactional
public void createUser() {
postgreSQLUserService.create();
neo4jUserService.create();
}
I think, I may use Atomikos transaction manager in order to achieve it but don't know is it a right way. Could you please show an example how to configure XA transaction manager for such case?
UPDATED
Based on the following answer Does Neo4j still support XA transactions? - Stack Overflow looks like XA support was dropped from Neo4j... So the only way to implement such functionality is to implement Saga microservice pattern?