DataJdbcSource优化

This commit is contained in:
redkale
2023-04-18 22:30:25 +08:00
parent 0fca7cd47b
commit 5e9c85e0fc
2 changed files with 28 additions and 36 deletions

View File

@@ -371,6 +371,10 @@ public abstract class Client<C extends ClientConnection<R, P>, R extends ClientR
return name;
}
public int getMaxConns() {
return connLimit;
}
public int getReadTimeoutSeconds() {
return readTimeoutSeconds;
}

View File

@@ -2786,30 +2786,15 @@ public class DataJdbcSource extends AbstractDataSqlSource {
this.connectAttrs.put("user", newUser);
this.connectAttrs.put("password", newPassword);
if (newMaxconns != this.maxConns) {
ArrayBlockingQueue<Connection> newQueue = new ArrayBlockingQueue<>(newMaxconns);
ArrayBlockingQueue<Connection> oldQueue = this.queue;
Semaphore oldSemaphore = this.canNewSemaphore;
this.queue = newQueue;
this.maxConns = newMaxconns;
this.canNewSemaphore = new Semaphore(this.maxConns);
Connection c;
while ((c = oldQueue.poll()) != null) {
try {
if (c.getClientInfo() != null) {
c.getClientInfo().put("version", "-1");
}
} catch (SQLException e) {
}
offerConnection(c, oldSemaphore);
}
changeMaxConns(newMaxconns);
}
}
private void resetMaxConnection() {
if ("mysql".equals(dbtype()) || "postgresql".equals(dbtype())) {
int newMaxconns = this.maxConns;
Connection conn = pollConnection();
try {
Connection conn = driver.connect(url, connectAttrs);
Statement stmt = conn.createStatement();
if ("mysql".equals(dbtype())) {
ResultSet rs = stmt.executeQuery("SHOW VARIABLES LIKE 'max_connections'");
@@ -2823,31 +2808,34 @@ public class DataJdbcSource extends AbstractDataSqlSource {
}
}
stmt.close();
} catch (SQLException e) {
} finally {
offerConnection(conn);
conn.close();
} catch (Exception e) {
}
if (this.maxConns > newMaxconns) { //配置连接数过大
ArrayBlockingQueue<Connection> newQueue = new ArrayBlockingQueue<>(newMaxconns);
ArrayBlockingQueue<Connection> oldQueue = this.queue;
Semaphore oldSemaphore = this.canNewSemaphore;
this.queue = newQueue;
this.maxConns = newMaxconns;
this.canNewSemaphore = new Semaphore(this.maxConns);
Connection c;
while ((c = oldQueue.poll()) != null) {
try {
if (c.getClientInfo() != null) {
c.getClientInfo().put("version", "-1");
}
} catch (SQLException e) {
}
offerConnection(c, oldSemaphore);
}
changeMaxConns(newMaxconns);
}
}
}
private void changeMaxConns(int newMaxconns) {
ArrayBlockingQueue<Connection> newQueue = new ArrayBlockingQueue<>(newMaxconns);
ArrayBlockingQueue<Connection> oldQueue = this.queue;
Semaphore oldSemaphore = this.canNewSemaphore;
this.queue = newQueue;
this.maxConns = newMaxconns;
this.canNewSemaphore = new Semaphore(this.maxConns);
Connection c;
while ((c = oldQueue.poll()) != null) {
try {
if (c.getClientInfo() != null) {
c.getClientInfo().put("version", "-1");
}
} catch (SQLException e) {
}
offerConnection(c, oldSemaphore);
}
}
public Connection pollConnection() {
Connection conn = queue.poll();
if (conn == null) {