Skip to content

Commit 6ddfefb

Browse files
committed
API changes
1 parent 1287a9c commit 6ddfefb

6 files changed

Lines changed: 84 additions & 54 deletions

File tree

api/src/main/java/me/zort/sqllib/api/SQLConnection.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.jetbrains.annotations.Nullable;
44

5+
import java.io.Closeable;
56
import java.sql.Connection;
67

78
/**
@@ -11,7 +12,8 @@
1112
*
1213
* @author ZorTik
1314
*/
14-
public interface SQLConnection {
15+
@SuppressWarnings("closeable")
16+
public interface SQLConnection extends Closeable {
1517

1618
/**
1719
* Tries to connect to remote SQL server.
@@ -24,7 +26,9 @@ public interface SQLConnection {
2426
/**
2527
* Tries to disconnect from remote SQL server.
2628
*/
29+
@Deprecated
2730
void disconnect();
31+
void close();
2832

2933
/**
3034
* Returns current running connection with

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,11 @@ public void disconnect() {
182182
}
183183
}
184184

185+
@Override
186+
public void close() {
187+
disconnect();
188+
}
189+
185190
protected void logSqlError(Exception e) {
186191
if(isLogSqlErrors()) {
187192
e.printStackTrace();

core/src/main/java/me/zort/sqllib/SQLDatabaseConnectionImpl.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import me.zort.sqllib.internal.query.part.SetStatement;
2626
import me.zort.sqllib.mapping.DefaultResultAdapter;
2727
import me.zort.sqllib.mapping.DefaultStatementMappingFactory;
28+
import me.zort.sqllib.pool.PooledSQLDatabaseConnection;
2829
import me.zort.sqllib.util.Pair;
2930
import me.zort.sqllib.util.Validator;
3031
import org.jetbrains.annotations.ApiStatus;
@@ -50,7 +51,7 @@
5051
* @author ZorTik
5152
*/
5253
@SuppressWarnings("unused")
53-
public class SQLDatabaseConnectionImpl extends SQLDatabaseConnection {
54+
public class SQLDatabaseConnectionImpl extends PooledSQLDatabaseConnection {
5455

5556
// --***-- Default Constants --***--
5657

@@ -431,19 +432,14 @@ private boolean reconnect() {
431432

432433
public UpsertQuery save(Object obj) {
433434
Pair<String[], UnknownValueWrapper[]> data = buildDefsVals(obj);
434-
435-
if(data == null) {
436-
return null;
437-
}
435+
if(data == null) return null;
438436

439437
String[] defs = data.getFirst();
440438
UnknownValueWrapper[] vals = data.getSecond();
441-
442439
UpsertQuery upsert = upsert().into(null, defs);
443440
for(UnknownValueWrapper wrapper : vals) {
444441
upsert.appendVal(wrapper.getObject());
445442
}
446-
447443
SetStatement<InsertQuery> setStmt = upsert.onDuplicateKey();
448444
for(int i = 0; i < defs.length; i++) {
449445
setStmt.and(defs[i], vals[i].getObject());
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package me.zort.sqllib.pool;
2+
3+
import lombok.Getter;
4+
import me.zort.sqllib.SQLDatabaseConnection;
5+
import me.zort.sqllib.internal.factory.SQLConnectionFactory;
6+
import org.jetbrains.annotations.Nullable;
7+
8+
public abstract class PooledSQLDatabaseConnection extends SQLDatabaseConnection {
9+
10+
private SQLConnectionPool assignedPool = null;
11+
@Getter(onMethod_ = {@Nullable})
12+
private long lastUsed = System.currentTimeMillis();
13+
14+
public PooledSQLDatabaseConnection(SQLConnectionFactory connectionFactory) {
15+
super(connectionFactory);
16+
}
17+
18+
protected void setAssignedPool(SQLConnectionPool pool) {
19+
assignedPool = pool;
20+
}
21+
22+
protected void setLastUsed(long lastUsed) {
23+
this.lastUsed = lastUsed;
24+
}
25+
26+
@Override
27+
public void close() {
28+
lastUsed = System.currentTimeMillis();
29+
if (assignedPool != null) {
30+
assignedPool.releaseObject(this);
31+
} else {
32+
super.close();
33+
}
34+
}
35+
}

core/src/main/java/me/zort/sqllib/SQLConnectionPool.java renamed to core/src/main/java/me/zort/sqllib/pool/SQLConnectionPool.java

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
package me.zort.sqllib;
1+
package me.zort.sqllib.pool;
22

3-
import lombok.AccessLevel;
43
import lombok.Data;
54
import lombok.RequiredArgsConstructor;
5+
import me.zort.sqllib.SQLConnectionBuilder;
6+
import me.zort.sqllib.SQLDatabaseConnection;
7+
import me.zort.sqllib.SQLDatabaseConnectionImpl;
68
import org.jetbrains.annotations.NotNull;
79

810
import java.sql.SQLException;
@@ -39,9 +41,10 @@ public static final class Options {
3941
private final boolean blockWhenExhausted;
4042

4143
// --***-- Pooled connection caches --***--
42-
private final Queue<SQLPooledConnection> freeConnections = new ConcurrentLinkedQueue<>();
43-
private final List<SQLPooledConnection> usedConnections = new CopyOnWriteArrayList<>();
44+
private final Queue<PooledSQLDatabaseConnection> freeConnections = new ConcurrentLinkedQueue<>();
45+
private final List<PooledSQLDatabaseConnection> usedConnections = new CopyOnWriteArrayList<>();
4446

47+
@SuppressWarnings("unused")
4548
public SQLConnectionPool(@NotNull SQLConnectionBuilder from) {
4649
this(from, new Options());
4750
}
@@ -68,9 +71,9 @@ public SQLConnectionPool(@NotNull SQLConnectionBuilder from, @NotNull Options po
6871
* @throws SQLException Connection error.
6972
*/
7073
@NotNull
71-
public Resource getResource() throws SQLException {
72-
freeConnections.removeIf(SQLPooledConnection::expired);
73-
SQLPooledConnection polled = freeConnections.poll();
74+
public SQLDatabaseConnection getResource() throws SQLException {
75+
freeConnections.removeIf(this::expired);
76+
PooledSQLDatabaseConnection polled = freeConnections.poll();
7477
if (polled == null && size() < maxConnections) {
7578
polled = establishObject();
7679
} else if(polled == null) {
@@ -90,29 +93,40 @@ public Resource getResource() throws SQLException {
9093
}
9194
}
9295
usedConnections.add(polled);
93-
return new Resource(this, polled);
96+
return polled;
9497
}
9598

96-
private SQLPooledConnection establishObject() throws SQLException {
97-
SQLPooledConnection polled = new SQLPooledConnection(builder.build());
98-
polled.connection.connect();
99+
private PooledSQLDatabaseConnection establishObject() throws SQLException {
100+
SQLDatabaseConnection polled_ = builder.build();
101+
if (!(polled_ instanceof PooledSQLDatabaseConnection)) {
102+
throw new SQLException("Builder does not produce a pooled connection.");
103+
}
104+
PooledSQLDatabaseConnection polled = (PooledSQLDatabaseConnection) polled_;
105+
polled.setAssignedPool(this);
106+
polled.connect();
99107

100-
SQLException error = polled.connection.getLastError();
108+
SQLException error = polled.getLastError();
101109
if (error != null) throw error;
102110

103-
if (polled.connection instanceof SQLDatabaseConnectionImpl) {
104-
((SQLDatabaseConnectionImpl) polled.connection).addErrorHandler(code -> {
111+
if (polled instanceof SQLDatabaseConnectionImpl) {
112+
((SQLDatabaseConnectionImpl) polled).addErrorHandler(code -> {
105113
// Remove the connection from the pool and disconnect
106114
// on fatal errors.
107115
freeConnections.remove(polled);
108116
usedConnections.remove(polled);
109-
polled.connection.disconnect();
117+
polled.disconnect();
110118
});
111119
}
112120

113121
return polled;
114122
}
115123

124+
void releaseObject(PooledSQLDatabaseConnection connection) {
125+
connection.setLastUsed(System.currentTimeMillis());
126+
freeConnections.add(connection);
127+
usedConnections.remove(connection);
128+
}
129+
116130
public int size() {
117131
return usedConnections.size() + freeConnections.size();
118132
}
@@ -122,38 +136,14 @@ public int size() {
122136
* clears the caches.
123137
*/
124138
public void close() {
125-
usedConnections.forEach(c -> c.connection.disconnect());
126-
freeConnections.forEach(c -> c.connection.disconnect());
139+
usedConnections.forEach(SQLDatabaseConnection::disconnect);
140+
freeConnections.forEach(SQLDatabaseConnection::disconnect);
127141
usedConnections.clear();
128142
freeConnections.clear();
129143
}
130144

131-
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
132-
private static final class SQLPooledConnection {
133-
private final SQLDatabaseConnection connection;
134-
private long lastUsed = System.currentTimeMillis();
135-
136-
public boolean expired() {
137-
return System.currentTimeMillis() - lastUsed > 1000 * 60 * 5;
138-
}
139-
}
140-
141-
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
142-
public static final class Resource implements AutoCloseable {
143-
144-
private final SQLConnectionPool pool;
145-
private final SQLPooledConnection connection;
146-
147-
public SQLDatabaseConnection getConnection() {
148-
return connection.connection;
149-
}
150-
151-
@Override
152-
public void close() {
153-
connection.lastUsed = System.currentTimeMillis();
154-
pool.freeConnections.add(connection);
155-
pool.usedConnections.remove(connection);
156-
}
145+
private boolean expired(PooledSQLDatabaseConnection connection) {
146+
return System.currentTimeMillis() - connection.getLastUsed() > 1000 * 60 * 5;
157147
}
158148

159149
}

src/test/java/me/zort/sqllib/test/TestCase1.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import lombok.AllArgsConstructor;
44
import lombok.extern.log4j.Log4j2;
55
import me.zort.sqllib.SQLConnectionBuilder;
6-
import me.zort.sqllib.SQLConnectionPool;
6+
import me.zort.sqllib.SQLDatabaseConnectionImpl;
7+
import me.zort.sqllib.pool.SQLConnectionPool;
78
import me.zort.sqllib.SQLDatabaseConnection;
89
import me.zort.sqllib.SQLDatabaseOptions;
910
import me.zort.sqllib.api.data.QueryResult;
@@ -158,9 +159,8 @@ public void test6_Pool() {
158159
options.setBorrowObjectTimeout(5000L);
159160
options.setBlockWhenExhausted(false);
160161
SQLConnectionPool pool = new SQLConnectionPool(builder, options);
161-
try (SQLConnectionPool.Resource resource = pool.getResource()) {
162+
try (SQLDatabaseConnection connection = pool.getResource()) {
162163
System.out.println("Got connection from pool");
163-
SQLDatabaseConnection connection = resource.getConnection();
164164
assertTrue(connection.save(TABLE_NAME, user1).isSuccessful());
165165
} catch(SQLException e) {
166166
throw new RuntimeException(e);

0 commit comments

Comments
 (0)