Auto-close DB and fix server closing algorithm

This commit is contained in:
pixtaded 2025-01-10 23:47:19 +03:00
parent 3f63e2262f
commit b914364c49
2 changed files with 9 additions and 3 deletions

View File

@ -60,9 +60,9 @@ public class CrabServer implements Crab {
} }
} }
public void stop() { public synchronized void stop() {
try {
isStopped = true; isStopped = true;
try {
if (socket != null) socket.close(); if (socket != null) socket.close();
if (serverSocket != null) serverSocket.close(); if (serverSocket != null) serverSocket.close();
} catch (IOException e) { } catch (IOException e) {

View File

@ -6,7 +6,7 @@ import java.sql.*;
import java.util.Date; import java.util.Date;
import java.util.Locale; import java.util.Locale;
public class Database { public class Database implements AutoCloseable {
private Connection connection; private Connection connection;
private String logs = ""; private String logs = "";
@ -68,4 +68,10 @@ public class Database {
else return logs.getBytes().length; else return logs.getBytes().length;
} }
@Override
public void close() throws SQLException {
if (connection != null && !connection.isClosed()) {
connection.close();
}
}
} }