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-core</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 two ways, the absolute path ‘absolutepath:’ and CLASSPATH ‘classpath:’, which can be referred to org.apache.shardingsphere.driver.jdbc.core.driver.ShardingSphereURLProvider‘s implementation for details.

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.

Special 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.

For ShardingSphere JDBC that is using the Java EE 8 API and its implementation, if you want to use ShardingSphere JDBC on a Jakarta EE 9+ API-based web framework such as Spring Boot OSS 3, you need to introduce a JAXB implementation of Java EE 8.

This is reflected in Maven’s pom.xml as follows. You can also use other JAXB API implementations. This configuration also applies to other Jakarta EE-based Web Frameworks, such as Quarkus 3, Micronaut Framework 4 and Helidon 3.

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

In addition, ShardingSphere’s XA distributed transactions are not yet ready on Spring Boot OSS 3.

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-core</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-core</artifactId>
  9. <version>${shardingsphere.version}</version>
  10. </dependency>
  11. </dependencies>
  12. </project>