Background information

Use local datasource

The database driver showed in the example is MySQL and the connection pool is HikariCP, either of which can be replaced by other database drivers and connection pools. When using ShardingSphere JDBC, the property names of the JDBC pools depend on its own definition instead of being fixed by ShardingSphere. See relevant procedures at org.apache.shardingsphere.infra.datasource.pool.creator.DataSourcePoolCreator. For example, using url instead of jdbc-url for Alibaba Druid 1.2.9 is the expected behavior.

Use datasource JNDI

If you wish to use JNDI for database configuration, you can replace a series of datasource configurations with spring.shardingsphere.datasource.${datasourceName}.jndiName when you are using ShardingSphere-JDBC on application servers(e.g. Tomcat).

Parameters Explanation

Using local datasource

  1. spring.shardingsphere.datasource.names= # Actual datasource names. Multiple datasources are separated with comma
  2. # <actual-data-source-name> to show actual datasource name
  3. spring.shardingsphere.datasource.<actual-data-source-name>.type= # Full class name of the database connection pool
  4. spring.shardingsphere.datasource.<actual-data-source-name>.driver-class-name= # Database-driven class name, based on the database connection pool's own configuration
  5. spring.shardingsphere.datasource.<actual-data-source-name>.jdbc-url= # Database URL connection, in ine with the connection pool's own configuration
  6. spring.shardingsphere.datasource.<actual-data-source-name>.username= # database user names,in line with the connection pool's own configuration
  7. spring.shardingsphere.datasource.<actual-data-source-name>.password= # database password ,in line with the connection pool's own configuration
  8. spring.shardingsphere.datasource.<actual-data-source-name>.<xxx>= # ... Other properties of the database connection pool

Using JNDI datasource

  1. spring.shardingsphere.datasource.names= # Authentic datasource names. Multiple datasources are separated with comma
  2. # <actual-data-source-name> to show actual datasource name
  3. spring.shardingsphere.datasource.<actual-data-source-name>.jndi-name= # datasource JNDI

Configuration Examples

Using local datasource

  1. # configure actual datasource
  2. spring.shardingsphere.datasource.names=ds1,ds2
  3. # configure the first datasource
  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 second datasource
  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=

Using JNDI datasource

  1. # configure actual datasource
  2. spring.shardingsphere.datasource.names=ds1,ds2
  3. # configure the first datasource
  4. spring.shardingsphere.datasource.ds1.jndi-name=java:comp/env/jdbc/ds1
  5. # configure the second datasource
  6. spring.shardingsphere.datasource.ds2.jndi-name=java:comp/env/jdbc/ds2