-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
38 changed files
with
2,280 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>com.github.rockylomo</groupId> | ||
<artifactId>rx</artifactId> | ||
<version>2.20.1-SNAPSHOT</version> | ||
</parent> | ||
<artifactId>rxlib-x</artifactId> | ||
<packaging>jar</packaging> | ||
<name>rxlib-x</name> | ||
<description>A set of utilities for Java</description> | ||
|
||
<properties> | ||
<maven.compiler.source>8</maven.compiler.source> | ||
<maven.compiler.target>8</maven.compiler.target> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<lombok.version>1.18.30</lombok.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.github.rockylomo</groupId> | ||
<artifactId>rxlib</artifactId> | ||
<version>${project.parent.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
<version>5.1.49</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.zaxxer</groupId> | ||
<artifactId>HikariCP</artifactId> | ||
<version>4.0.3</version> | ||
<exclusions> | ||
<exclusion> | ||
<artifactId>slf4j-api</artifactId> | ||
<groupId>org.slf4j</groupId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.alibaba</groupId> | ||
<artifactId>druid</artifactId> | ||
<version>1.2.21</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.rx.jdbc; | ||
|
||
/** | ||
* Created by IntelliJ IDEA. | ||
* | ||
* @author wangxiaoming | ||
* Date: 2019/12/10 | ||
*/ | ||
public enum ConnectionPoolKind { | ||
NONE, | ||
HikariCP, | ||
Druid | ||
} |
25 changes: 25 additions & 0 deletions
25
rxlib-x/src/main/java/org/rx/jdbc/ConnectionPoolMXBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package org.rx.jdbc; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* Created by IntelliJ IDEA. | ||
* | ||
* @author wangxiaoming | ||
* Date: 2019/12/10 | ||
*/ | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Data | ||
public class ConnectionPoolMXBean implements Serializable { | ||
private static final long serialVersionUID = -8774671998907063174L; | ||
private String name; | ||
private int idleConnections; | ||
private int activeConnections; | ||
private int totalConnections; | ||
private int threadsAwaitingConnection; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.rx.jdbc; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.io.Serializable; | ||
|
||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Data | ||
public class DataSourceConfig implements Serializable { | ||
private static final long serialVersionUID = 8722778295417630020L; | ||
String jdbcUrl; | ||
String username, password; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.rx.jdbc; | ||
|
||
import lombok.Getter; | ||
import org.rx.bean.NEnum; | ||
import org.rx.core.Arrays; | ||
import org.rx.core.Linq; | ||
import org.rx.core.Strings; | ||
import org.rx.exception.InvalidException; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
/** | ||
* Created by IntelliJ IDEA. | ||
* | ||
* @author wangxiaoming | ||
* Date: 2019/9/2 | ||
*/ | ||
@Getter | ||
public enum DriverClassFlag implements NEnum<DriverClassFlag> { | ||
MySQL(1, "com.mysql.jdbc.Driver", "jdbc:mysql:"), | ||
PostgreSQL(2, "org.postgresql.Driver", "jdbc:postgresql:"), | ||
Oracle(3, "oracle.jdbc.driver.OracleDriver", "jdbc:oracle:"), | ||
SQLServer(4, "com.microsoft.sqlserver.jdbc.SQLServerDriver", "jdbc:sqlserver:", "jdbc:microsoft:sqlserver:"), | ||
H2(5, "org.h2.Driver", "jdbc:h2:"), | ||
MongoDB(6, "com.xdbc.jdbc.mongodb.MongoDriver", "jdbc:mongodb:"); | ||
|
||
private final int value; | ||
private final String driverClassName; | ||
private final List<String> urlPrefixes; | ||
|
||
DriverClassFlag(int value, String driverClassName, String... urlPrefixes) { | ||
this.value = value; | ||
this.driverClassName = driverClassName; | ||
this.urlPrefixes = Collections.unmodifiableList(Arrays.toList(urlPrefixes)); | ||
} | ||
|
||
public static DriverClassFlag recognize(String jdbcUrl) { | ||
DriverClassFlag flag = Linq.from(DriverClassFlag.values()).firstOrDefault(p -> Linq.from(p.urlPrefixes).any(x -> Strings.startsWithIgnoreCase(jdbcUrl, x))); | ||
if (flag == null) { | ||
throw new InvalidException("Recognize url {} fail", jdbcUrl); | ||
} | ||
return flag; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package org.rx.jdbc; | ||
|
||
import lombok.*; | ||
import org.rx.net.Sockets; | ||
|
||
import java.net.InetSocketAddress; | ||
|
||
@Data | ||
@EqualsAndHashCode(callSuper = true) | ||
public class JdbcConfig extends DataSourceConfig { | ||
private static final long serialVersionUID = 2890340670027176789L; | ||
|
||
private long connectionTimeoutMillis = 30000; | ||
private long idleTimeoutMillis = 60000; | ||
private long maxLifetimeMillis = 1800000; | ||
private int minPoolSize = 10; | ||
private int maxPoolSize = 10; | ||
private ConnectionPoolKind poolKind = ConnectionPoolKind.HikariCP; | ||
|
||
private String poolName; | ||
private boolean enableStreamingResults; | ||
private long executeTimeoutMillis = 30000; | ||
private boolean interruptTimeoutExecution = false; | ||
|
||
@Getter(AccessLevel.PRIVATE) | ||
@Setter(AccessLevel.PRIVATE) | ||
private InetSocketAddress endpoint; | ||
@Getter(AccessLevel.PRIVATE) | ||
@Setter(AccessLevel.PRIVATE) | ||
private String databaseName; | ||
|
||
public void setUrl(String url) { | ||
jdbcUrl = url; | ||
endpoint = null; | ||
databaseName = null; | ||
} | ||
|
||
public InetSocketAddress getEndpointFromUrl() { | ||
if (endpoint == null) { | ||
endpoint = getEndpointFromUrl(jdbcUrl); | ||
} | ||
return endpoint; | ||
} | ||
|
||
public String getDatabaseNameFromUrl() { | ||
if (databaseName == null) { | ||
databaseName = getDatabaseNameFromUrl(jdbcUrl); | ||
} | ||
return databaseName; | ||
} | ||
|
||
public static InetSocketAddress getEndpointFromUrl(String url) { | ||
return Sockets.parseEndpoint(findChars(url, "://", "/", 0)); | ||
} | ||
|
||
public static String getDatabaseNameFromUrl(String url) { | ||
return findChars(url, "/", "?", url.indexOf("://") + 3); | ||
} | ||
|
||
private static String findChars(String url, String begin, String end, int startIndex) { | ||
int s = url.indexOf(begin, startIndex); | ||
if (s == -1) { | ||
// throw new InvalidOperationException("begin flag not found"); | ||
return null; | ||
} | ||
int offset = s + begin.length(), e = url.indexOf(end, offset); | ||
return e == -1 ? url.substring(offset) : url.substring(offset, e); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.rx.jdbc; | ||
|
||
import lombok.NonNull; | ||
import org.rx.bean.$; | ||
import org.rx.util.function.BiFunc; | ||
|
||
import java.sql.ResultSet; | ||
import java.util.List; | ||
|
||
public interface JdbcExecutable { | ||
ResultSet executeQuery(String sql, Object[] params); | ||
|
||
ResultSet executeQuery(String sql, Object[] params, long executeTimeoutMillis); | ||
|
||
<T> T executeQuery(String sql, Object[] params, BiFunc<ResultSet, T> func); | ||
|
||
<T> T executeQuery(String sql, Object[] params, BiFunc<ResultSet, T> func, long executeTimeoutMillis); | ||
|
||
int execute(String sql, Object[] params); | ||
|
||
int execute(String sql, Object[] params, long executeTimeoutMillis); | ||
|
||
int execute(String sql, Object[] params, int generatedKeys, $<Long> lastInsertId); | ||
|
||
int execute(String sql, Object[] params, long executeTimeoutMillis, int generatedKeys, $<Long> lastInsertId); | ||
|
||
int[] executeBatch(String sql, List<Object[]> batchParams); | ||
|
||
int[] executeBatch(String sql, @NonNull List<Object[]> batchParams, long executeTimeoutMillis); | ||
|
||
ResultSet executeQuery(String sql); | ||
|
||
ResultSet executeQuery(String sql, long executeTimeoutMillis); | ||
|
||
<T> T executeQuery(String sql, BiFunc<ResultSet, T> func); | ||
|
||
<T> T executeQuery(String sql, BiFunc<ResultSet, T> func, long executeTimeoutMillis); | ||
|
||
int execute(String sql); | ||
|
||
int execute(String sql, long executeTimeoutMillis); | ||
|
||
int execute(String sql, int generatedKeys, $<Long> lastInsertId); | ||
|
||
int execute(String sql, long executeTimeoutMillis, int generatedKeys, $<Long> lastInsertId); | ||
|
||
int[] executeBatch(List<String> batchSql); | ||
|
||
int[] executeBatch(@NonNull List<String> batchSql, long executeTimeoutMillis); | ||
} |
Oops, something went wrong.