Skip to content


Repository files navigation

Spring Data YugabyteDB

Spring Data YugabyteDB brings the power of Distributed SQL to Spring developers by using the familar Spring Data paradigms, Spring Data YugabyteDB supports Spring template and Spring repositories for accessing the data from YugabyteDB. Spring Data YuabyteDB is based on the [Spring Data JDBC]( project which is extended to support YugabyteDB distributed SQL features.

YugabyteDB is a high-performance, cloud-native distributed SQL database that aims to support all PostgreSQL features. It is best to fit for cloud-native OLTP (i.e. real-time, business-critical) applications that need absolute data correctness and require at least one of the following: scalability, high tolerance to failures, or globally-distributed deployments.


In addition to providing most PostgreSQL features supported by Spring Data JDBC, this project aims to enable the following:

  • CRUD operations for YugabyteDB

  • @EnableYsqlRepositories annotation to enable YsqlRepository

  • Yugabyte Distributed SQL transaction manager

  • Support for Follower Reads

  • Eliminate Load Balancer from SQL (cluster-awareness)

  • Develop Geo-Distributed Apps (topology-awareness)

  • Row Level Geo-partitioning support (partition-awareness)

Getting Started

A quick start example of getting started with Spring Data YugabyteDB in JAVA:

public interface ShoppingCartRepository extends YsqlRepository<ShoppingCart, String> {

  ShoppingCart findById(String id);

  List<ShoppingCart> findByUserId(String userId);


public class CartService {

  private final ShoppingCartRepository repository;

  public CartService(CartService repository) {
    this.repository = repository;

  public void doWork() {


    ShoppingCart myShoppingCart = new ShoppingCart();

    ShoppingCart savedCart = repository.findById("cart1");
    List<ShoppingCart> productsInCart = repository.findByUserId("u1001");

public class YsqlConfig extends AbstractYugabyteJdbcConfiguration {

	DataSource dataSource() {

	  String hostName = "";
	  String port = "5433";

	  Properties poolProperties = new Properties();
	  poolProperties.setProperty("dataSourceClassName", "com.yugabyte.ysql.YBClusterAwareDataSource");
	  poolProperties.setProperty("dataSource.serverName", hostName);
	  poolProperties.setProperty("dataSource.portNumber", port);
	  poolProperties.setProperty("dataSource.user", "yugabyte");
	  poolProperties.setProperty("dataSource.password", "");
	  poolProperties.setProperty("dataSource.loadBalance", "true");

	  HikariConfig hikariConfig = new HikariConfig(poolProperties);
	  DataSource ybClusterAwareDataSource = new HikariDataSource(hikariConfig);
	  return ybClusterAwareDataSource;

  JdbcTemplate jdbcTemplate(@Autowired DataSource dataSource) {

    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    return jdbcTemplate;

  NamedParameterJdbcOperations namedParameterJdbcOperations(DataSource dataSource) {
    return new NamedParameterJdbcTemplate(dataSource);

	TransactionManager transactionManager(DataSource dataSource) {
	  return new YugabyteTransactionManager(dataSource);


Maven configuration

Add the Maven dependency:


See also the spring-data-yugabytedb-example app.