Distributed transactions with multiple databases, Spring Boot, Spring Data JPA and Atomikos
15 Apr 2014
A couple of weeks ago I was evaluating the possibility to use Spring Boot, Spring Data JPA and Atomikos for distributed transactions involving multiple databases. After looking at the Spring blog article (which involves one database and ActiveMQ) and having done some attempts, I could not get it to work with two databases. The configuration seemed fine, but the Entity Manager did not get notified when persisting my entities. So I wrote this question on StackOverflow, which has been answered directly by Dave Syer and Oliver Gierke. This post is to share and discuss the solution.
Description of the case and entities model
We want to be able to save two entities at the same time into two different databases; the operation must be transactional. So, in this example, we have a Customer entity, which is persisted in the first database, and an Order entity which is persisted in the second database. The two entities are very simple, as they serve only as a demonstration.
The resulting implementation is the following. It's worth noting that they belong to two different packages, for two main reasons:
it's a logical separation that gives order to the project
each repository will scan packages containing only entities that it will be going to manage
See Lombok for annotations like @Data and @EqualsAndHashCode
Write repositories interfaces
Also in this case it's standard, the only thing to notice is that I put the two interfaces in two different packages. The reason is explained in the next step.
Write configuration classes
This is where it becomes interesting. The @DependsOn("transactionManager") annotation is not mandatory, but I needed this to get rid of several warnings at tests (or application) startup, like WARNING: transaction manager not running? in the logs. The next annotation @EnableJpaRepositories is more important:
it specifies which are the packages to scan for annotated components (repository interfaces), and in my case I wanted only repositories related to the customer (and conversely to the order).
it specifies which is the entity manager to be used to manage entities, in my case the customerEntityManager for customer related operations and orderEntityManager for order related operations
it specifies the transaction manager to be used, in my case the transactionManager defined in the MainConfig class. This needs to be the same for every @EnableJpaRepositories to get distributed transactions working
Another important thing here is the definition of the LocalContainerEntityManagerFactoryBean.
the @Bean annotation has a given name, that is the one specified in the @EnableJpaRepositories annotation.
you need to set some properties to the JpaPropertyMap, in particular you need to say that the transaction type is JTA and that the jta platform is AtomikosJtaPlatform.class.getName()
Not setting the second property was the reason why I could not get it work. As Dave Syer wrote "It seems Hibernate4 doesn't work with Atomikos out of the box", so you need to implement the class to be set as hibernate.transaction.jta.platform property by yourself. In my opinion this is not very well documented, but fortunately Oliver Gierke found another StackOverflow discussion about this topic. If you are using another JTA provider, you may find this useful.
Write the AbstractJtaPlatform implementation
As said, this is the most important step, as we need to write the implementation of that class by ourselves since Hibernate does not provide it. Here is the resulting code:
Write the main configuration class
Also in this case it's a pretty standard class, with @EnableTransactionManagement annotation and Atomikos bean definitions. The only very important thing to notice is that we need to set AtomikosJtaPlatform.transactionManager and AtomikosJtaPlatform.transaction attributes.
Here is the resulting structure of the project:
You can see the full source code here: https://github.com/fabiomaffioletti/mul-at, The master branch uses in memory database. Checkout branch named mysql-db to use real databases (see application.properties to tweak your database connection data).