Skip to content

Commit e97a6dc

Browse files
committed
Fixes
1 parent f3911db commit e97a6dc

2 files changed

Lines changed: 21 additions & 7 deletions

File tree

asql-core/src/main/java/me/zort/sqllib/SQLDatabaseConnection.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package me.zort.sqllib;
22

33
import lombok.AllArgsConstructor;
4-
import lombok.Data;
54
import lombok.Getter;
65
import me.zort.sqllib.api.Query;
76
import me.zort.sqllib.api.SQLConnection;
@@ -376,7 +375,10 @@ protected void logSqlError(Exception e) {
376375

377376
@SuppressWarnings("all")
378377
protected void notifyHandlers(int code) {
379-
errorCount++;
378+
if (code >= 100) {
379+
// Code is higher than 99, so it's an error
380+
errorCount++;
381+
}
380382
this.codeHandlers.forEach(handler -> runCatching(() -> handler.onNotified(code)));
381383
}
382384

@@ -398,9 +400,11 @@ protected static class DefsVals {
398400
}
399401

400402
public static final class Code {
401-
public static final int QUERY_FATAL = 0;
402403
public static final int CONNECTED = 1;
403404
public static final int CLOSED = 2;
404405
public static final int CLEARING = 3;
406+
407+
// Error codes start on 100
408+
public static final int QUERY_FATAL = 100;
405409
}
406410
}

asql-core/src/main/java/me/zort/sqllib/pool/SQLConnectionPool.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,28 @@ private PooledSQLDatabaseConnection establishObject() throws SQLException {
130130
if (error != null) throw error;
131131

132132
if (polled instanceof SQLDatabaseConnectionImpl)
133-
((SQLDatabaseConnectionImpl) polled).addCodeHandler(code -> handleConnectionError(polled));
133+
((SQLDatabaseConnectionImpl) polled).addCodeHandler(code -> handleConnectionCode(polled, code));
134134

135135
return polled;
136136
}
137137

138-
synchronized void handleConnectionError(PooledSQLDatabaseConnection polled) {
139-
errorCount++;
138+
synchronized void handleConnectionCode(PooledSQLDatabaseConnection polled, int code) {
139+
if (code == SQLDatabaseConnection.Code.CONNECTED) {
140+
return;
141+
}
142+
143+
if (code >= 100) {
144+
errorCount++;
145+
}
140146
// Remove the connection from the pool and disconnect
141147
// on fatal errors.
142148
freeConnections.remove(polled);
143149
usedConnections.remove(polled);
144-
polled.disconnect();
150+
if (code != SQLDatabaseConnection.Code.CLOSED) {
151+
// I prevent stack overflow by closing only connections
152+
// that are not already closed.
153+
polled.disconnect();
154+
}
145155
}
146156

147157
void releaseObject(PooledSQLDatabaseConnection connection) {

0 commit comments

Comments
 (0)