Overview

ShardingSphere provides a JDBC driver, and developers can configure ShardingSphereDriver in Spring Boot to use ShardingSphere.

Usage

Import Maven Dependency

  1. <dependency>
  2. <groupId>org.apache.shardingsphere</groupId>
  3. <artifactId>shardingsphere-jdbc</artifactId>
  4. <version>${shardingsphere.version}</version>
  5. </dependency>

Configure Spring Boot Properties

  1. # Configuring DataSource Drivers
  2. spring.datasource.driver-class-name=org.apache.shardingsphere.driver.ShardingSphereDriver
  3. # Specify a YAML configuration file
  4. spring.datasource.url=jdbc:shardingsphere:classpath:xxx.yaml

The YAML configuration file in ‘spring.datasource.url’ currently support in multiple ways, refer to Known Implementation.

Use Data Source

Use this data source directly; or configure ShardingSphereDataSource to be used in conjunction with ORM frameworks such as JPA, Hibernate, and MyBatis.

Handling for Spring Boot OSS 3

Spring Boot OSS 3 has made a “big bang” upgrade to Jakarta EE and Java 17, with all complications involved.

ShardingSphere’s XA distributed transactions are not yet ready on Spring Boot OSS 3. This limitation also applies to other Jakarta EE 9+ based Web Frameworks, such as Quarkus 3, Micronaut Framework 4 and Helidon 3.

Users only need to configure as follows.

  1. <project>
  2. <dependencies>
  3. <dependency>
  4. <groupId>org.apache.shardingsphere</groupId>
  5. <artifactId>shardingsphere-jdbc</artifactId>
  6. <version>${shardingsphere.version}</version>
  7. </dependency>
  8. </dependencies>
  9. </project>

Special handling for earlier versions of Spring Boot OSS 2

All features of ShardingSphere are available on Spring Boot OSS 2, but earlier versions of Spring Boot OSS may require manually specifying version 2.2 for SnakeYAML. This is reflected in Maven’s pom.xml as follows.

  1. <project>
  2. <dependencies>
  3. <dependency>
  4. <groupId>org.apache.shardingsphere</groupId>
  5. <artifactId>shardingsphere-jdbc</artifactId>
  6. <version>${shardingsphere.version}</version>
  7. </dependency>
  8. <dependency>
  9. <groupId>org.yaml</groupId>
  10. <artifactId>snakeyaml</artifactId>
  11. <version>2.2</version>
  12. </dependency>
  13. </dependencies>
  14. </project>

If the user created the Spring Boot project from https://start.spring.io/, users can simplify configuration by following things.

  1. <project>
  2. <properties>
  3. <snakeyaml.version>2.2</snakeyaml.version>
  4. </properties>
  5. <dependencies>
  6. <dependency>
  7. <groupId>org.apache.shardingsphere</groupId>
  8. <artifactId>shardingsphere-jdbc</artifactId>
  9. <version>${shardingsphere.version}</version>
  10. </dependency>
  11. </dependencies>
  12. </project>