Skip to content

Commit 0d07930

Browse files
committed
fix AuthorizationJdbcDriver get driver bug
1 parent 52cf10f commit 0d07930

7 files changed

Lines changed: 95 additions & 17 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
<groupId>com.codingapi.springboot</groupId>
1717
<artifactId>springboot-parent</artifactId>
18-
<version>2.10.33</version>
18+
<version>2.10.34</version>
1919

2020
<url>https://github.com/codingapi/springboot-framewrok</url>
2121
<name>springboot-parent</name>

springboot-starter-data-authorization/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<artifactId>springboot-parent</artifactId>
88
<groupId>com.codingapi.springboot</groupId>
9-
<version>2.10.33</version>
9+
<version>2.10.34</version>
1010
</parent>
1111

1212
<name>springboot-starter-data-authorization</name>

springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/jdbc/AuthorizationJdbcDriver.java

Lines changed: 89 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,52 +6,130 @@
66
import java.sql.*;
77
import java.util.Enumeration;
88
import java.util.Properties;
9+
import java.util.concurrent.ConcurrentHashMap;
10+
import java.util.concurrent.ConcurrentMap;
11+
import java.util.logging.Level;
912
import java.util.logging.Logger;
1013

1114
public class AuthorizationJdbcDriver implements Driver {
1215

13-
private Driver driver;
16+
private static final ConcurrentMap<String, Driver> DRIVER_CACHE = new ConcurrentHashMap<>();
17+
private static final Logger LOGGER = Logger.getLogger(AuthorizationJdbcDriver.class.getName());
18+
19+
static {
20+
try {
21+
DriverManager.registerDriver(new AuthorizationJdbcDriver());
22+
} catch (Exception e) {
23+
LOGGER.log(Level.SEVERE, "Failed to register AuthorizationJdbcDriver", e);
24+
throw new RuntimeException("Failed to register AuthorizationJdbcDriver", e);
25+
}
26+
LOGGER.info("AuthorizationJdbcDriver initialized and registered");
27+
}
28+
1429

1530
@Override
1631
public Connection connect(String url, Properties info) throws SQLException {
32+
if (url == null) {
33+
throw new SQLException("URL cannot be null");
34+
}
35+
36+
Driver driver = findDriver(url);
37+
if (driver == null) {
38+
throw new SQLException("No suitable driver found for " + url);
39+
}
1740
return new ConnectionProxy(driver.connect(url, info));
1841
}
1942

20-
@Override
21-
public boolean acceptsURL(String url) throws SQLException {
43+
44+
/**
45+
* 查找接受指定 URL 的真实 JDBC 驱动
46+
*
47+
* @param url JDBC URL
48+
* @return 真实的 JDBC 驱动,如果未找到则返回 null
49+
*/
50+
private Driver findDriver(String url) throws SQLException {
51+
if (url == null) {
52+
return null;
53+
}
54+
55+
// 从缓存中查找(使用 URL 前缀作为 key)
56+
Driver cachedDriver = DRIVER_CACHE.get(url);
57+
if (cachedDriver != null) {
58+
try {
59+
// 验证缓存的驱动仍然接受该 URL
60+
if (cachedDriver.acceptsURL(url)) {
61+
return cachedDriver;
62+
} else {
63+
// 如果缓存的驱动不再接受该 URL,从缓存中移除
64+
DRIVER_CACHE.remove(url, cachedDriver);
65+
}
66+
} catch (SQLException e) {
67+
// 如果验证失败,从缓存中移除并继续查找
68+
DRIVER_CACHE.remove(url, cachedDriver);
69+
LOGGER.log(Level.FINE, "Cached driver no longer accepts URL: " + url, e);
70+
}
71+
}
72+
73+
// 遍历所有已注册的驱动
2274
Enumeration<Driver> drivers = DriverManager.getDrivers();
2375
while (drivers.hasMoreElements()) {
2476
Driver driver = drivers.nextElement();
25-
if (driver.acceptsURL(url)) {
26-
this.driver = driver;
27-
return true;
77+
if (driver.getClass().equals(AuthorizationJdbcDriver.class)) {
78+
continue;
79+
}
80+
try {
81+
if (driver.acceptsURL(url)) {
82+
// 缓存驱动(使用 URL 的前缀作为 key,因为同一个数据库类型的 URL 前缀相同)
83+
DRIVER_CACHE.putIfAbsent(url, driver);
84+
return driver;
85+
}
86+
} catch (SQLException e) {
87+
// 忽略单个驱动的异常,继续查找
88+
LOGGER.log(Level.FINE, "Driver " + driver.getClass().getName() + " does not accept URL: " + url, e);
2889
}
2990
}
30-
return false;
91+
return null;
92+
}
93+
94+
@Override
95+
public boolean acceptsURL(String url) throws SQLException {
96+
if (url == null) {
97+
return false;
98+
}
99+
Driver driver = findDriver(url);
100+
return driver != null;
31101
}
32102

33103
@Override
34104
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
105+
Driver driver = findDriver(url);
106+
if (driver == null) {
107+
throw new SQLException("No suitable driver found for " + url);
108+
}
35109
return driver.getPropertyInfo(url, info);
36110
}
37111

38112
@Override
39113
public int getMajorVersion() {
40-
return driver.getMajorVersion();
114+
// 返回代理驱动的主版本号
115+
return 1;
41116
}
42117

43118
@Override
44119
public int getMinorVersion() {
45-
return driver.getMinorVersion();
120+
// 返回代理驱动的次版本号
121+
return 0;
46122
}
47123

48124
@Override
49125
public boolean jdbcCompliant() {
50-
return driver.jdbcCompliant();
126+
// 代理驱动本身不直接兼容 JDBC,它依赖于底层驱动
127+
return false;
51128
}
52129

130+
53131
@Override
54132
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
55-
return driver.getParentLogger();
133+
return LOGGER;
56134
}
57135
}

springboot-starter-data-fast/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>springboot-parent</artifactId>
77
<groupId>com.codingapi.springboot</groupId>
8-
<version>2.10.33</version>
8+
<version>2.10.34</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

springboot-starter-flow/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<artifactId>springboot-parent</artifactId>
88
<groupId>com.codingapi.springboot</groupId>
9-
<version>2.10.33</version>
9+
<version>2.10.34</version>
1010
</parent>
1111

1212
<name>springboot-starter-flow</name>

springboot-starter-security/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<artifactId>springboot-parent</artifactId>
88
<groupId>com.codingapi.springboot</groupId>
9-
<version>2.10.33</version>
9+
<version>2.10.34</version>
1010
</parent>
1111

1212
<artifactId>springboot-starter-security</artifactId>

springboot-starter/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>com.codingapi.springboot</groupId>
77
<artifactId>springboot-parent</artifactId>
8-
<version>2.10.33</version>
8+
<version>2.10.34</version>
99
</parent>
1010
<artifactId>springboot-starter</artifactId>
1111

0 commit comments

Comments
 (0)