Use Native Data Source

Configuration Item Explanation

  1. spring.shardingsphere.datasource.names= # Actual data source name, multiple split by `,`
  2. # <actual-data-source-name> indicate name of data source name
  3. spring.shardingsphere.datasource.<actual-data-source-name>.type= # Full class name of database connection pool
  4. spring.shardingsphere.datasource.<actual-data-source-name>.driver-class-name= # Class name of database driver, ref property of connection pool
  5. spring.shardingsphere.datasource.<actual-data-source-name>.jdbc-url= # Database URL, ref property of connection pool
  6. spring.shardingsphere.datasource.<actual-data-source-name>.username= # Database username, ref property of connection pool
  7. spring.shardingsphere.datasource.<actual-data-source-name>.password= # Database password, ref property of connection pool
  8. spring.shardingsphere.datasource.<actual-data-source-name>.<xxx>= # ... Other properties for data source pool

Example

In this example, the database driver is MySQL, and connection pool is HikariCP, which can be replaced with other database drivers and connection pools.

  1. # Configure actual data sources
  2. spring.shardingsphere.datasource.names=ds1,ds2
  3. # Configure the 1st data source
  4. spring.shardingsphere.datasource.ds1.type=com.zaxxer.hikari.HikariDataSource
  5. spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
  6. spring.shardingsphere.datasource.ds1.jdbc-url=jdbc:mysql://localhost:3306/ds1
  7. spring.shardingsphere.datasource.ds1.username=root
  8. spring.shardingsphere.datasource.ds1.password=
  9. # Configure the 2nd data source
  10. spring.shardingsphere.datasource.ds2.type=com.zaxxer.hikari.HikariDataSource
  11. spring.shardingsphere.datasource.ds2.driver-class-name=com.mysql.jdbc.Driver
  12. spring.shardingsphere.datasource.ds2.jdbc-url=jdbc:mysql://localhost:3306/ds2
  13. spring.shardingsphere.datasource.ds2.username=root
  14. spring.shardingsphere.datasource.ds2.password=

Use JNDI Data Source

If developer plan to use ShardingSphere-JDBC in Web Server (such as Tomcat) with JNDI data source, spring.shardingsphere.datasource.${datasourceName}.jndiName can be used as an alternative to series of configuration of data source.

Configuration Item Explanation

  1. spring.shardingsphere.datasource.names= # Actual data source name, multiple split by `,`
  2. # <actual-data-source-name> indicate name of data source name
  3. spring.shardingsphere.datasource.<actual-data-source-name>.jndi-name= # JNDI of data source

Example

  1. # Configure actual data sources
  2. spring.shardingsphere.datasource.names=ds1,ds2
  3. # Configure the 1st data source
  4. spring.shardingsphere.datasource.ds1.jndi-name=java:comp/env/jdbc/ds1
  5. # Configure the 2nd data source
  6. spring.shardingsphere.datasource.ds2.jndi-name=java:comp/env/jdbc/ds2