LindormDataSource provides an out-of-the-box optimized configuration and supports access to all types of Lindorm instances, including single-zone and multi-zone instances. It is a high-performance JDBC connection pool for accessing Lindorm. When you use a multi-zone Lindorm wide table instance, LindormDataSource supports multiple access strategies, such as nearest-zone access, specified-zone access, and random-zone access.
Prerequisites
A Java environment with JDK 1.8 or a later version is required.
Add your client IP address to the Lindorm whitelist.
Connection parameter description
The parameter descriptions apply to all examples in this topic.
When you access Lindorm using the MySQL protocol, LindormDataSource automatically fills in optimized settings to improve SQL access performance if no connection configuration is specified in the connection URL or the Properties file. Therefore, you only need to provide the parameters listed in the following table. For more information, see Appendix.
Parameter | Description |
JdbcUrl (jdbcUrl) | The Java JDBC connection URL for the MySQL protocol. Format: If no database name is provided, it defaults to the "default" database. To obtain the MySQL-compatible endpoint, view the connection endpoint. Important
|
Username (username) | The username and password for connecting to the wide table engine. If you forget your password, reset it in the Lindorm wide table engine cluster management system. For more information, see change user password. |
Password (password) |
Integrate JDBC applications
Before you connect to the Lindorm wide table engine using LindormDataSource, you must install the required 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 one of 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 using a configuration file.
Create a
lindorm.propertiesfile in thesrc/main/resourcesdirectory of your Maven project and add the following content:jdbcUrl=jdbc:mysql://ld-bp1mq0tdzbx1m****-proxy-lindorm-pub.lindorm.aliyuncs.com:33060/default username=r*** password=p*** maximumPoolSize=30Add the following code to your business logic:
LindormDataSourceConfig config = new LindormDataSourceConfig("lindorm.properties"); LindormDataSource lindormDataSource = new LindormDataSource(config);
Integrate Spring Boot 2.x applications
Before you connect to the Lindorm wide table engine using LindormDataSource, you must install the required 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 the Spring configuration. Create an application.yml file in the
src/main/resourcesdirectory of your Maven project and add the following content: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(); } } }
Integrate Spring Boot 3.x applications
Before you connect to the Lindorm wide table engine using LindormDataSource, you must install the required 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 the Spring configuration. Create an
application.ymlfile in thesrc/main/resourcesdirectory of your Maven project and add the following content: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(); } } }
Integrate MyBatis applications
When you use MyBatis with Spring Boot, you must configure MyBatis within Spring Boot. If you use MyBatis independently without Spring Boot, follow these steps.
Before you connect to the Lindorm wide table engine using LindormDataSource, you must install the required 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 one of 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 using a configuration file.
Define a DataSourceFactory that is compatible 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
mybatis-config.propertiesfile in thesrc/main/resourcesdirectory of your Maven project and add the following content:<?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 code to your business logic:
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 need to configure only the maximumPoolSize configuration item, which specifies the maximum connection pool size. Other configuration items do not require additional configuration.
Configuration item | Description |
maximumPoolSize | Maximum number of connections. Default value: |
minimumIdle | Minimum number of idle connections. Default value equals |
connectionTimeout | Connection acquisition timeout in milliseconds (ms). Default value: |
keepaliveTime | Keepalive interval in milliseconds (ms). Default value: |
idleTimeout | Idle connection timeout in milliseconds (ms). Default value: |
maxLifetime | Maximum connection lifetime in milliseconds (ms). Default value: |
Appendix
When you access Lindorm using the MySQL protocol, LindormDataSource automatically fills in the following parameters to improve SQL access performance if they are not specified in the URL (endpoint) or the Properties file.
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 you use LindormDataSource, you need to provide only the JDBC connection URL for the MySQL protocol (the `url` parameter) and specify the target database (the `database` parameter). Do not add connection configuration to the connection string. For example, jdbc:mysql://ld-uf6k8yqb741t3****-proxy-sql-lindorm-public.lindorm.rds.aliyuncs.com:33060/default.