Skip to content

Commit 4a6a6f5

Browse files
committed
fix bug
1 parent d7b3cc0 commit 4a6a6f5

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

  • src/main/java/dev/noah/perplayerkit/storage/sql

src/main/java/dev/noah/perplayerkit/storage/sql/SQLite.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
public class SQLite implements SQLDatabase {
2626

27-
2827
private final String databasePath;
2928
private Connection connection;
3029

@@ -33,7 +32,11 @@ public SQLite(String databasePath) {
3332
}
3433

3534
public boolean isConnected() {
36-
return (connection != null);
35+
try {
36+
return connection != null && !connection.isClosed() && connection.isValid(1);
37+
} catch (SQLException e) {
38+
return false;
39+
}
3740
}
3841

3942
public void connect() throws ClassNotFoundException, SQLException {
@@ -43,17 +46,20 @@ public void connect() throws ClassNotFoundException, SQLException {
4346
}
4447
}
4548

46-
public void disconnect() {
47-
if (isConnected()) {
48-
try {
49-
connection.close();
50-
} catch (SQLException e) {
51-
e.printStackTrace();
52-
}
49+
public void disconnect() throws SQLException {
50+
if (connection != null && !connection.isClosed()) {
51+
connection.close();
5352
}
5453
}
5554

56-
public Connection getConnection() {
55+
public Connection getConnection() throws SQLException {
56+
if (!isConnected()) {
57+
try {
58+
connect();
59+
} catch (ClassNotFoundException e) {
60+
throw new SQLException("Failed to load SQLite driver", e);
61+
}
62+
}
5763
return connection;
5864
}
5965
}

0 commit comments

Comments
 (0)