LindormDataSource encapsulates ready-to-use best configurations and supports access to all types of Lindorm instances (single-zone and multi-zone). It is the optimal high-performance JDBC connection pool for Lindorm access. When using multi-zone Lindorm wide table instances, LindormDataSource can flexibly adapt to various access strategies, such as nearest zone access, specified zone access, and random zone access.
Prerequisites
You have enabled MySQL protocol compatibility.
Java Development Kit (JDK) 1.8 or later is installed.
You have added the client IP address to the Lindorm whitelist.
Connection parameter description
This parameter description applies to all examples in this topic.
When accessing Lindorm through the MySQL protocol, if no connection configuration is specified in both the connection address and the Properties file, LindormDataSource will automatically fill in parameters to improve SQL access performance. Therefore, you only need to fill in the parameters in the following table. For more information, see Appendix.
Parameter | Description |
JdbcUrl (jdbcUrl) | The Java JDBC connection URL for MySQL protocol. The format is If the database name is not specified, the connection will be established to the default database. To obtain the MySQL Compatible Address, see View connection addresses. Important
|
Username (username) | The username and password used to connect to LindormTable. If you forget your password, you can change the user password through the Lindorm wide table engine cluster management system. |
Password (password) |
JDBC application integration
Before connecting to the Lindorm wide table engine through LindormDataSource, you need to install the relevant dependencies.
For a Maven project, add the following dependencies to the
pom.xmlfile in thedependenciessection.<dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <version>8.3.0</version> </dependency> <dependency> <groupId>com.aliyun.lindorm</groupId> <artifactId>lindorm-sql-datasource</artifactId> <version>2.2.1.4</version> </dependency>You can connect to the Lindorm wide table engine in the following two ways.
Establish a connection in your business code.
import com.aliyun.lindorm.sql.client.datasource.LindormDataSource; import com.aliyun.lindorm.sql.client.datasource.LindormDataSourceConfig; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; public class test { public static void main(String[] args) throws Exception{ LindormDataSourceConfig lindormDataSourceConfig = new LindormDataSourceConfig(); lindormDataSourceConfig.setJdbcUrl("jdbc:mysql://ld-bp1mq0tdzbx1m****-proxy-lindorm-pub.lindorm.aliyuncs.com:33060/database"); lindormDataSourceConfig.setUsername("r***"); lindormDataSourceConfig.setPassword("p***"); lindormDataSourceConfig.setMaximumPoolSize(30); LindormDataSource lindormDataSource = new LindormDataSource(lindormDataSourceConfig); try (Connection connection = lindormDataSource.getConnection()) { String sql = "select * from " + tableName + " where id=?"; try (PreparedStatement ps = connection.prepareStatement(sql)) { ps.setString(1, "001"); ResultSet rs = ps.executeQuery(); while (rs.next()) { String id = rs.getString(1); String name = rs.getString(2); System.out.println("id=" + id); System.out.println("name=" + name); } } } } }Establish a connection through a configuration file.
Create a new
lindorm.propertiesfile in thesrc/main/resourcesdirectory of your Maven project, and add the following content to the file:jdbcUrl=jdbc:mysql://ld-bp1mq0tdzbx1m****-proxy-lindorm-pub.lindorm.aliyuncs.com:33060/default username=r*** password=p*** maximumPoolSize=30Add the following content to your business code.
LindormDataSourceConfig config = new LindormDataSourceConfig("lindorm.properties"); LindormDataSource lindormDataSource = new LindormDataSource(config);
Spring-Boot 2.x application integration
Before connecting to the Lindorm wide table engine through LindormDataSource, you need to install the relevant dependencies.
For a Maven project, add the following dependencies to the
pom.xmlfile in thedependenciessection.<dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <version>8.3.0</version> </dependency> <dependency> <groupId>com.aliyun.lindorm</groupId> <artifactId>lindorm-sql-datasource-springboot-starter</artifactId> <version>2.2.1.4</version> </dependency>Add Spring configuration. Create a new application.yml file in the
src/main/resourcesdirectory of your Maven project, and add the following content to the file.spring: datasource: lindorm: jdbc-url: jdbc:mysql://ld-bp167w8n1ab5p****-proxy-sql-lindorm.lindorm.rds.aliyuncs.com:33060/db1 username: r*** password: t*** maximum-pool-size: 30Write your business code.
@Service public class DatabaseService { @Autowired private DataSource dataSource; public void createUser(User user) throws SQLException { String sql = "INSERT INTO users (name, age) VALUES (?, ?)"; try (Connection conn = dataSource.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) { ps.setString(1, user.getName()); ps.setInt(2, user.getAge()); ps.executeUpdate(); } } }
Spring-Boot 3.x application integration
Before connecting to the Lindorm wide table engine through LindormDataSource, you need to install the relevant dependencies.
For a Maven project, add the following dependencies to the
pom.xmlfile in thedependenciessection.<dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <version>8.3.0</version> </dependency> <dependency> <groupId>com.aliyun.lindorm</groupId> <artifactId>lindorm-sql-datasource-springboot-3-starter</artifactId> <version>2.2.1.4</version> </dependency>Add Spring configuration. Create a new
application.ymlfile in thesrc/main/resourcesfolder of the Maven project, and add the following content to the file.spring: datasource: lindorm: jdbc-url: jdbc:mysql://ld-bp167w8n1ab5p****-proxy-sql-lindorm.lindorm.rds.aliyuncs.com:33060/db1 username: r*** password: r*** maximum-pool-size: 30Write your business code.
@Service public class DatabaseService { @Autowired private DataSource dataSource; public void createUser(User user) throws SQLException { String sql = "INSERT INTO users (name, age) VALUES (?, ?)"; try (Connection conn = dataSource.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) { ps.setString(1, user.getName()); ps.setInt(2, user.getAge()); ps.executeUpdate(); } } }
MyBatis application integration
When using MyBatis, if it is based on the Spring-Boot framework, you can complete the MyBatis configuration in Spring-Boot. If you use MyBatis independently (without relying on Spring-Boot), follow these steps for configuration.
Before connecting to the Lindorm wide table engine through LindormDataSource, you need to install the relevant dependencies.
For a Maven project, add the following dependencies to the
pom.xmlfile in thedependenciessection.<dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <version>8.3.0</version> </dependency> <dependency> <groupId>com.aliyun.lindorm</groupId> <artifactId>lindorm-sql-datasource</artifactId> <version>2.2.1.4</version> </dependency>You can connect to the Lindorm wide table engine in the following two ways.
Establish a connection in your business code.
SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean(); LindormDataSource dataSource = new LindormDataSource(); dataSource.setJdbcUrl("jdbc:mysql://ld-bp1mq0tdzbx1m****-proxy-lindorm-pub.lindorm.aliyuncs.com:33060"); dataSource.setUsername("r***"); dataSource.setPassword("r***"); sessionFactoryBean.setDataSource(dataSource); SqlSessionFactory sessionFactory = sessionFactoryBean.getObject(); try (SqlSession session = sessionFactory.openSession(true)) { UserMapper mapper = session.getMapper(UserMapper.class); mapper.insert(user); }Establish a connection through a configuration file.
Define a DataSourceFactory that complies with MyBatis.
package com.example.datasource; import com.aliyun.lindorm.sql.client.datasource.LindormDataSource; import com.aliyun.lindorm.sql.client.datasource.LindormDataSourceConfig; import org.apache.ibatis.datasource.DataSourceFactory; public class LindormDataSourceFactory implements DataSourceFactory { private Properties props; @Override public void setProperties(Properties props) { this.props = props; } @Override public DataSource getDataSource() { LindormDataSourceConfig config = new LindormDataSourceConfig(props); LindormDataSource dataSource = new LindormDataSource(config); return dataSource; } }Create a new
src/main/resourcesfile namedmybatis-config.propertiesin the Maven project folder, and add the following content to the file:<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="com.example.datasource.LindormDataSourceFactory"> <property name="jdbcUrl" value="jdbc:mysql://ld-8vbn68478unu8****-proxy-sql-lindorm.lindorm.rds.aliyuncs.com:33060/lindorm_test"/> <property name="username" value="r***"/> <property name="password" value="t***"/> <property name="maximumPoolSize" value="30"/> </dataSource> </environment> </environments> <mappers> <mapper class="com.example.mapper.UserMapper"/> </mappers> </configuration>Add the following content to your business code.
public static void main(String[] args) throws SQLException, IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); UserService userService = new UserService(sqlSessionFactory); userService.run(); }
Optional configuration items
Typically, you only need to configure maximumPoolSize (maximum connection pool size). Other configuration items do not require additional configuration.
Configuration item | Description |
maximumPoolSize | The maximum number of connections. The default value is |
minimumIdle | The minimum number of idle connections. The default value is the same as |
connectionTimeout | The connection timeout in milliseconds (ms). The default value is |
keepaliveTime | The connection keepalive interval in milliseconds (ms). The default value is |
idleTimeout | The idle connection timeout in milliseconds (ms). The default value is |
maxLifetime | The maximum connection lifetime in milliseconds (ms). The default value is |
Appendix
When accessing Lindorm through the MySQL protocol, if the following parameters are not specified in both the URL (connection address) and the Properties file, LindormDataSource will automatically fill them in to improve SQL access performance.
The default parameters that are automatically filled in are as follows:
("sslMode", "DISABLED");
("allowPublicKeyRetrieval", "true");
("useServerPrepStmts", "true");
("useLocalSessionState", "true");
("rewriteBatchedStatements", "true");
("cachePrepStmts", "true");
("prepStmtCacheSize", "300");
("prepStmtCacheSqlLimit", "50000000");Therefore, when using LindormDataSource, you only need to fill in the JDBC connection URL for MySQL protocol (url) and specify the target database (database). You do not need to add connection configurations in the connection string. For example: jdbc:mysql://ld-uf6k8yqb741t3****-proxy-sql-lindorm-public.lindorm.rds.aliyuncs.com:33060/default.