修改问题
这个提交包含在:
+16
@@ -0,0 +1,16 @@
|
||||
package cn.aixunkeji.common.core.enums;
|
||||
|
||||
/** Platform storage only; external connector dialects are independent. */
|
||||
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; expected Dameng or MySQL");
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,10 @@
|
||||
<description>DataGov internal maintenance scheduling and MySQL lease-lock foundation</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>cn.aixunkeji</groupId>
|
||||
<artifactId>aixun-common-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
|
||||
+15
-2
@@ -8,25 +8,38 @@ import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
/** MySQL 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.jdbcTemplate = jdbcTemplate;
|
||||
this.databaseDialect = java.util.Objects.requireNonNull(databaseDialect);
|
||||
}
|
||||
|
||||
@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())
|
||||
);
|
||||
|
||||
@@ -49,8 +49,11 @@
|
||||
<version>${dynamic-ds.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Platform database. Reuse the pinned driver already used by Integration;
|
||||
target Pack89 compatibility must be verified against a real server. -->
|
||||
<!-- Both platform drivers ship in the same artifact; selection follows the JDBC URL. -->
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.dameng</groupId>
|
||||
<artifactId>DmJdbcDriver18</artifactId>
|
||||
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
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;
|
||||
|
||||
/** Resolve once at startup from the configured platform datasource, never a global mutable flag. */
|
||||
@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
|
||||
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
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 {
|
||||
static class Consumer {
|
||||
@org.springframework.beans.factory.annotation.Autowired PlatformDatabaseDialect dialect;
|
||||
}
|
||||
@Test void sameApplicationWiringSelectsBothDatabasesAndClosesProbeConnection() throws Exception {
|
||||
for (String product : new String[]{"DM DBMS", "MySQL"}) {
|
||||
var ds=mock(DataSource.class);var connection=mock(Connection.class);var metadata=mock(DatabaseMetaData.class);
|
||||
when(ds.getConnection()).thenReturn(connection);when(connection.getMetaData()).thenReturn(metadata);
|
||||
when(metadata.getDatabaseProductName()).thenReturn(product);
|
||||
try(var context=new AnnotationConfigApplicationContext()) {
|
||||
context.registerBean(DataSource.class,()->ds);
|
||||
context.register(PlatformDatabaseConfiguration.class,Consumer.class);context.refresh();
|
||||
var dialect=context.getBean(Consumer.class).dialect;
|
||||
assertThat(dialect).isEqualTo(PlatformDatabaseDialect.fromProduct(product));
|
||||
assertThat(dialect.sql("MERGE INTO test", "INSERT INTO test"))
|
||||
.isEqualTo(product.equals("MySQL")?"INSERT INTO test":"MERGE INTO test");
|
||||
verify(connection).close();
|
||||
}
|
||||
}
|
||||
}
|
||||
@Test void unknownEngineCannotSilentlySelectMysql() throws Exception {
|
||||
var ds=mock(DataSource.class);var c=mock(Connection.class);var m=mock(DatabaseMetaData.class);
|
||||
when(ds.getConnection()).thenReturn(c);when(c.getMetaData()).thenReturn(m);when(m.getDatabaseProductName()).thenReturn("PostgreSQL");
|
||||
assertThatThrownBy(()->new PlatformDatabaseConfiguration().platformDatabaseDialect(ds)).isInstanceOf(IllegalArgumentException.class);
|
||||
verify(c).close();
|
||||
}
|
||||
@Test void bothDriversArePresentInTheSameDependencyGraph() throws Exception {
|
||||
assertThat(Class.forName("dm.jdbc.driver.DmDriver")).isAssignableTo(java.sql.Driver.class);
|
||||
assertThat(Class.forName("com.mysql.cj.jdbc.Driver")).isAssignableTo(java.sql.Driver.class);
|
||||
}
|
||||
}
|
||||
在新工单中引用
屏蔽一个用户