添加达梦、MySQL 数据库适配功能
这个提交包含在:
+16
@@ -0,0 +1,16 @@
|
||||
package cn.aixunkeji.common.core.enums;
|
||||
|
||||
/** Platform persistence dialect, determined once from the configured JDBC datasource. */
|
||||
public enum PlatformDatabaseDialect {
|
||||
DAMENG, MYSQL;
|
||||
|
||||
public String sql(String dameng, String mysql) {
|
||||
return this == MYSQL ? mysql : dameng;
|
||||
}
|
||||
|
||||
public static PlatformDatabaseDialect fromProduct(String product) {
|
||||
if ("MySQL".equalsIgnoreCase(product)) return MYSQL;
|
||||
if ("DM DBMS".equalsIgnoreCase(product) || "DM".equalsIgnoreCase(product)) return DAMENG;
|
||||
throw new IllegalArgumentException("Unsupported platform database product");
|
||||
}
|
||||
}
|
||||
@@ -10,9 +10,13 @@
|
||||
</parent>
|
||||
|
||||
<artifactId>aixun-common-maintenance</artifactId>
|
||||
<description>DataGov internal maintenance scheduling and MySQL lease-lock foundation</description>
|
||||
<description>DataGov internal maintenance scheduling and JDBC lease-lock foundation for DM and MySQL</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>cn.aixunkeji</groupId>
|
||||
<artifactId>aixun-common-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
|
||||
+16
-3
@@ -6,27 +6,40 @@ import java.util.Optional;
|
||||
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
/** MySQL implementation using one conditional UPDATE as the lease-acquisition linearization point. */
|
||||
/** JDBC implementation using one conditional UPDATE as the lease-acquisition linearization point. */
|
||||
public final class JdbcMaintenanceLeaseRepository implements MaintenanceLeaseRepository {
|
||||
private final cn.aixunkeji.common.core.enums.PlatformDatabaseDialect databaseDialect;
|
||||
|
||||
|
||||
private static final Instant INITIAL_LEASE_EXPIRY = Instant.parse("1970-01-01T00:00:01Z");
|
||||
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
|
||||
public JdbcMaintenanceLeaseRepository(JdbcTemplate jdbcTemplate) {
|
||||
this(jdbcTemplate, cn.aixunkeji.common.core.enums.PlatformDatabaseDialect.DAMENG);
|
||||
}
|
||||
|
||||
public JdbcMaintenanceLeaseRepository(JdbcTemplate jdbcTemplate, cn.aixunkeji.common.core.enums.PlatformDatabaseDialect databaseDialect) {
|
||||
this.databaseDialect = databaseDialect;
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean tryAcquire(MaintenanceLeaseRequest request) {
|
||||
jdbcTemplate.update(
|
||||
"""
|
||||
databaseDialect.sql("""
|
||||
INSERT /*+ IGNORE_ROW_ON_DUPKEY_INDEX(dg_maintenance_lease) */ INTO dg_maintenance_lease(
|
||||
lease_id, job_name, job_kind, lease_until, last_outcome,
|
||||
attempt_no, create_time, update_time, del_flag, version
|
||||
) VALUES (?, ?, ?, ?, 'IDLE', 0, ?, ?, '0', 0)
|
||||
|
||||
""",
|
||||
""", """
|
||||
INSERT IGNORE INTO dg_maintenance_lease(
|
||||
lease_id, job_name, job_kind, lease_until, last_outcome,
|
||||
attempt_no, create_time, update_time, del_flag, version
|
||||
) VALUES (?, ?, ?, ?, 'IDLE', 0, ?, ?, '0', 0)
|
||||
|
||||
"""),
|
||||
request.leaseId(), request.jobName(), request.jobKind().name(), timestamp(INITIAL_LEASE_EXPIRY),
|
||||
timestamp(request.acquiredAt()), timestamp(request.acquiredAt())
|
||||
);
|
||||
|
||||
@@ -16,6 +16,10 @@
|
||||
</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.aixunkeji</groupId>
|
||||
<artifactId>aixun-common-satoken</artifactId>
|
||||
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package cn.aixunkeji.common.mybatis.config;
|
||||
|
||||
import cn.aixunkeji.common.core.enums.PlatformDatabaseDialect;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.SQLException;
|
||||
|
||||
@AutoConfiguration
|
||||
public class PlatformDatabaseConfiguration {
|
||||
@Bean
|
||||
public PlatformDatabaseDialect platformDatabaseDialect(DataSource dataSource) throws SQLException {
|
||||
try (var connection = dataSource.getConnection()) {
|
||||
return PlatformDatabaseDialect.fromProduct(connection.getMetaData().getDatabaseProductName());
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
@@ -1 +1,2 @@
|
||||
cn.aixunkeji.common.mybatis.config.MybatisPlusConfiguration
|
||||
cn.aixunkeji.common.mybatis.config.PlatformDatabaseConfiguration
|
||||
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
package cn.aixunkeji.common.mybatis.config;
|
||||
|
||||
import cn.aixunkeji.common.core.enums.PlatformDatabaseDialect;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class PlatformDatabaseConfigurationTest {
|
||||
@Test void springResolvesActualConfiguredDatabaseAndClosesProbeConnection() throws Exception {
|
||||
for (String product : new String[]{"DM DBMS", "MySQL"}) {
|
||||
DataSource source = mock(DataSource.class);
|
||||
Connection connection = mock(Connection.class);
|
||||
DatabaseMetaData metadata = mock(DatabaseMetaData.class);
|
||||
when(source.getConnection()).thenReturn(connection);
|
||||
when(connection.getMetaData()).thenReturn(metadata);
|
||||
when(metadata.getDatabaseProductName()).thenReturn(product);
|
||||
try (var context = new AnnotationConfigApplicationContext()) {
|
||||
context.registerBean(DataSource.class, () -> source);
|
||||
context.register(PlatformDatabaseConfiguration.class);
|
||||
context.refresh();
|
||||
assertThat(context.getBean(PlatformDatabaseDialect.class)).isEqualTo(PlatformDatabaseDialect.fromProduct(product));
|
||||
verify(connection).close();
|
||||
}
|
||||
}
|
||||
}
|
||||
@Test void unknownDatabaseFailsWithoutSilentlyUsingMysql() throws Exception {
|
||||
DataSource source = mock(DataSource.class);
|
||||
Connection connection = mock(Connection.class);
|
||||
DatabaseMetaData metadata = mock(DatabaseMetaData.class);
|
||||
when(source.getConnection()).thenReturn(connection);
|
||||
when(connection.getMetaData()).thenReturn(metadata);
|
||||
when(metadata.getDatabaseProductName()).thenReturn("PostgreSQL");
|
||||
assertThatThrownBy(() -> new PlatformDatabaseConfiguration().platformDatabaseDialect(source)).isInstanceOf(IllegalArgumentException.class);
|
||||
verify(connection).close();
|
||||
}
|
||||
@Test void sameClasspathIncludesBothJdbcDrivers() throws Exception {
|
||||
assertThat(Class.forName("dm.jdbc.driver.DmDriver")).isNotNull();
|
||||
assertThat(Class.forName("com.mysql.cj.jdbc.Driver")).isNotNull();
|
||||
}
|
||||
}
|
||||
在新工单中引用
屏蔽一个用户