From b914364c49b5b89bcf4ac3019c620fda15833857 Mon Sep 17 00:00:00 2001 From: pixtaded Date: Fri, 10 Jan 2025 23:47:19 +0300 Subject: [PATCH] Auto-close DB and fix server closing algorithm --- src/main/java/net/pixtaded/crab/server/CrabServer.java | 4 ++-- src/main/java/net/pixtaded/crab/server/Database.java | 8 +++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/pixtaded/crab/server/CrabServer.java b/src/main/java/net/pixtaded/crab/server/CrabServer.java index 4874014..ea6eab1 100644 --- a/src/main/java/net/pixtaded/crab/server/CrabServer.java +++ b/src/main/java/net/pixtaded/crab/server/CrabServer.java @@ -60,9 +60,9 @@ public class CrabServer implements Crab { } } - public void stop() { + public synchronized void stop() { + isStopped = true; try { - isStopped = true; if (socket != null) socket.close(); if (serverSocket != null) serverSocket.close(); } catch (IOException e) { diff --git a/src/main/java/net/pixtaded/crab/server/Database.java b/src/main/java/net/pixtaded/crab/server/Database.java index d328f9b..61986d5 100644 --- a/src/main/java/net/pixtaded/crab/server/Database.java +++ b/src/main/java/net/pixtaded/crab/server/Database.java @@ -6,7 +6,7 @@ import java.sql.*; import java.util.Date; import java.util.Locale; -public class Database { +public class Database implements AutoCloseable { private Connection connection; private String logs = ""; @@ -68,4 +68,10 @@ public class Database { else return logs.getBytes().length; } + @Override + public void close() throws SQLException { + if (connection != null && !connection.isClosed()) { + connection.close(); + } + } }