Skip to content

Commit

Permalink
2025-01-06 05:04:06
Browse files Browse the repository at this point in the history
  • Loading branch information
yingzhuo committed Jan 5, 2025
1 parent 35b7edc commit 5529eb9
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 38 deletions.
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.yingzhuo</groupId>
<artifactId>spring-turbo-modules</artifactId>
<version>3.4.1-3</version>
<version>3.4.2-SNAPSHOT</version>
<modules>
<module>spring-turbo-module-configuration</module>
<module>spring-turbo-module-jackson</module>
<module>spring-turbo-module-jdbc</module>
<module>spring-turbo-module-jwt</module>
<module>spring-turbo-module-misc</module>
<module>spring-turbo-module-redis</module>
<module>spring-turbo-module-security</module>
<module>spring-turbo-module-webcli</module>
<module>spring-turbo-module-webmvc</module>
<module>spring-turbo-module-redis</module>
<module>spring-turbo-module-jdbc</module>
</modules>
<packaging>pom</packaging>

Expand Down Expand Up @@ -138,7 +138,7 @@
<java.version>17</java.version>

<spring.turbo.version>${project.version}</spring.turbo.version>
<spring.boot.version>3.4.1</spring.boot.version>
<spring.boot.version>3.4.2-SNAPSHOT</spring.boot.version>
<hutool.version>5.8.35</hutool.version>
<jjwt.version>0.12.6</jjwt.version>
<lombok.version>1.18.36</lombok.version>
Expand Down
2 changes: 1 addition & 1 deletion spring-turbo-module-configuration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.github.yingzhuo</groupId>
<artifactId>spring-turbo-modules</artifactId>
<version>3.4.1-3</version>
<version>3.4.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-turbo-module-configuration</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion spring-turbo-module-jackson/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.github.yingzhuo</groupId>
<artifactId>spring-turbo-modules</artifactId>
<version>3.4.1-3</version>
<version>3.4.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-turbo-module-jackson</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion spring-turbo-module-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.github.yingzhuo</groupId>
<artifactId>spring-turbo-modules</artifactId>
<version>3.4.1-3</version>
<version>3.4.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-turbo-module-jdbc</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package spring.turbo.module.jdbc.autoconfiguration;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
Expand Down Expand Up @@ -30,20 +29,7 @@ public DataSource dataSource(RoutingDataSourceProperties props) {
final var targetDataSources = new HashMap<String, DataSource>();

props.getHikariDataSources().forEach((dataSourceName, dataSourceConfig) -> {
var dataSource = (HikariDataSource) DataSourceFactories.createDataSource(dataSourceConfig, HikariDataSource.class);

dataSource.setPoolName(dataSourceConfig.getPoolName());
dataSource.setMinimumIdle(dataSourceConfig.getMinimumIdle());
dataSource.setMaximumPoolSize(dataSourceConfig.getMaximumPoolSize());
dataSource.setAutoCommit(dataSourceConfig.isAutoCommit());
dataSource.setIdleTimeout(dataSourceConfig.getIdleTimeout());
dataSource.setMaxLifetime(dataSourceConfig.getMaxLifetime());
dataSource.setConnectionTimeout(dataSourceConfig.getConnectionTimeout());
dataSource.setConnectionTestQuery(dataSourceConfig.getConnectionTestQuery());
dataSource.setValidationTimeout(dataSourceConfig.getValidationTimeout());
dataSource.setConnectionInitSql(dataSourceConfig.getConnectionInitSql());
dataSource.setInitializationFailTimeout(dataSourceConfig.getInitializationFailTimeout());

var dataSource = DataSourceFactories.createHikariDataSource(dataSourceConfig, null);
targetDataSources.put(dataSourceName, dataSource);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@
@ToString
public class HikariProperties implements JdbcConnectionDetails, Serializable {

private String driverClassName;
private String jdbcUrl;
private String username;
private String password;
private String driverClassName;

private String poolName = null;
private String poolName = "Hikari";
private int minimumIdle = 10;
private int maximumPoolSize = 30;
private boolean autoCommit = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package spring.turbo.module.jdbc.util;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetails;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;
import spring.turbo.module.jdbc.ds.HikariProperties;

import javax.sql.DataSource;
import java.util.Objects;

/**
* 数据源创建工具
*
* @author 应卓
* @since 3.4.1
*/
@SuppressWarnings("unchecked")
public final class DataSourceFactories {

/**
Expand All @@ -22,14 +24,10 @@ public final class DataSourceFactories {
private DataSourceFactories() {
}

public static <T> T createDataSource(JdbcConnectionDetails connectionDetails, Class<? extends DataSource> type) {
return createDataSource(connectionDetails, type, null);
}

@SuppressWarnings("unchecked")
public static <T> T createDataSource(JdbcConnectionDetails connectionDetails, Class<? extends DataSource> type, @Nullable ClassLoader classLoader) {
// @formatter:off
return (T) DataSourceBuilder.create(Objects.requireNonNullElse(classLoader, ClassUtils.getDefaultClassLoader()))
classLoader = classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader();
return (T) DataSourceBuilder.create(classLoader)
.type(type)
.driverClassName(connectionDetails.getDriverClassName())
.url(connectionDetails.getJdbcUrl())
Expand All @@ -39,4 +37,29 @@ public static <T> T createDataSource(JdbcConnectionDetails connectionDetails, Cl
// @formatter:on
}

// Hikari
// -----------------------------------------------------------------------------------------------------------------

public static HikariDataSource createHikariDataSource(HikariProperties connectionDetails, @Nullable ClassLoader classLoader) {
return createHikariDataSource((JdbcConnectionDetails) connectionDetails, classLoader);
}

public static HikariDataSource createHikariDataSource(JdbcConnectionDetails connectionDetails, @Nullable ClassLoader classLoader) {
final HikariDataSource ds = createDataSource(connectionDetails, HikariDataSource.class, classLoader);
if (connectionDetails instanceof HikariProperties dataSourceConfig) {
ds.setPoolName(dataSourceConfig.getPoolName());
ds.setMinimumIdle(dataSourceConfig.getMinimumIdle());
ds.setMaximumPoolSize(dataSourceConfig.getMaximumPoolSize());
ds.setAutoCommit(dataSourceConfig.isAutoCommit());
ds.setIdleTimeout(dataSourceConfig.getIdleTimeout());
ds.setMaxLifetime(dataSourceConfig.getMaxLifetime());
ds.setConnectionTimeout(dataSourceConfig.getConnectionTimeout());
ds.setConnectionTestQuery(dataSourceConfig.getConnectionTestQuery());
ds.setValidationTimeout(dataSourceConfig.getValidationTimeout());
ds.setConnectionInitSql(dataSourceConfig.getConnectionInitSql());
ds.setInitializationFailTimeout(dataSourceConfig.getInitializationFailTimeout());
}
return ds;
}

}
2 changes: 1 addition & 1 deletion spring-turbo-module-jwt/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.github.yingzhuo</groupId>
<artifactId>spring-turbo-modules</artifactId>
<version>3.4.1-3</version>
<version>3.4.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-turbo-module-jwt</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion spring-turbo-module-misc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.github.yingzhuo</groupId>
<artifactId>spring-turbo-modules</artifactId>
<version>3.4.1-3</version>
<version>3.4.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-turbo-module-misc</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion spring-turbo-module-redis/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.github.yingzhuo</groupId>
<artifactId>spring-turbo-modules</artifactId>
<version>3.4.1-3</version>
<version>3.4.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-turbo-module-redis</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion spring-turbo-module-security/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.github.yingzhuo</groupId>
<artifactId>spring-turbo-modules</artifactId>
<version>3.4.1-3</version>
<version>3.4.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-turbo-module-security</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion spring-turbo-module-webcli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.github.yingzhuo</groupId>
<artifactId>spring-turbo-modules</artifactId>
<version>3.4.1-3</version>
<version>3.4.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-turbo-module-webcli</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion spring-turbo-module-webmvc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.github.yingzhuo</groupId>
<artifactId>spring-turbo-modules</artifactId>
<version>3.4.1-3</version>
<version>3.4.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-turbo-module-webmvc</artifactId>
Expand Down

0 comments on commit 5529eb9

Please sign in to comment.