VOOZH about

URL: https://www.javacodegeeks.com/2018/01/datasource-routing-spring-transactional.html

⇱ DataSource Routing with Spring @Transactional - Java Code Geeks


I was inspired by Carl Papa’s use of aspects with the Spring Framework to determine the DataSource to use (either read-write or read-only). So, I’m writing this post.

I must admit that I have long been familiar with Spring’s AbstractRoutingDataSource. But I did not have a good idea where it can be used. Thanks to Carl and team, and one of their projects. Now, I know a good use case.

@Transactional

With Spring, read-only transactions are typically marked with annotations.

public class ... {
 @Transactional(readOnly=true)
 public void ...() {...}

 @Transactional // read-write
 public void ...() {...}
}

To take advantage of this, we use Spring’s TransactionSynchronizationManager to determine if the current transaction is read-only or not.

👁 Image

AbstractRoutingDataSource

Here, we use Spring’s AbstractRoutingDataSource to route to the read-only replica if the current transaction is read-only. Otherwise, it routes to the default which is the master.

public class ... extends AbstractRoutingDataSource {
 @Override
 protected Object determineCurrentLookupKey() {
 if (TransactionSynchronizationManager
 .isCurrentTransactionReadOnly() ...) {
 // return key to a replica
 }
 return null; // use default
 }
 ...
}

Upon using the above approach, we found out that the TransactionSynchronizationManager is one step behind because Spring will have already called DataSource.getConnection() before a synchronization is established. Thus, a LazyConnectionDataSourceProxy needs to be configured as well.

As we were discussing this, we figured if there was another way to determine if the current transaction is read-only or not (without resorting to LazyConnectionDataSourceProxy). So, we came up with an experimental approach where an aspect captures the TransactionDefinition (from the @Transactional annotation, if any) as a thread-local variable, and an AbstractRoutingDataSource that routes based on the captured information.

The relevant source code can be found on GitHub. Thanks again, Carl! BTW, Carl is also an award-winning movie director. Wow, talent definitely knows no boundaries.

Published on Java Code Geeks with permission by Lorenzo Dee, partner at our JCG program. See the original article here: DataSource Routing with Spring @Transactional

Opinions expressed by Java Code Geeks contributors are their own.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

Tags
Spring
👁 Photo of Lorenzo Dee
Lorenzo Dee
January 3rd, 2018Last Updated: January 3rd, 2018
0 1,513 1 minute read

Lorenzo Dee

Lorenzo is a software engineer, trainer, manager, and entrepreneur, who loves developing software systems that make people and organizations productive, profitable, and happy. He is a co-founder of the now dormant Haybol.ph, a Philippine real estate search site. He loves drinking coffee, root beer, and milk shakes.
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button
Close
wpDiscuz