1919
2020public class SQLHandler {
2121
22- Connection connection ;
22+ private Connection connection ;
2323 Logger logger = SimpleHomes .getInstance ().getLogger ();
2424
2525 private SQLHandler () {
@@ -35,23 +35,22 @@ public static SQLHandler getInstance() {
3535 }
3636
3737 public void init () {
38- try {
39- connection = sqlOrSqlLite ();
40- try (Statement statement = connection .createStatement ()) {
41- statement .execute ("""
42- CREATE TABLE IF NOT EXISTS homes (
43- player_uuid_and_name VARCHAR (255) PRIMARY KEY,
44- player_uuid VARCHAR(36),
45- home_name VARCHAR(255),
46- world_uuid VARCHAR(255),
47- world_name VARCHAR(255),
48- location_x DOUBLE,
49- location_y DOUBLE,
50- location_z DOUBLE,
51- yaw FLOAT,
52- pitch FLOAT
53- );""" );
54- }
38+
39+ try (Statement statement = getConnection ().createStatement ()) {
40+ statement .execute ("""
41+ CREATE TABLE IF NOT EXISTS homes (
42+ player_uuid_and_name VARCHAR (255) PRIMARY KEY,
43+ player_uuid VARCHAR(36),
44+ home_name VARCHAR(255),
45+ world_uuid VARCHAR(255),
46+ world_name VARCHAR(255),
47+ location_x DOUBLE,
48+ location_y DOUBLE,
49+ location_z DOUBLE,
50+ yaw FLOAT,
51+ pitch FLOAT
52+ );""" );
53+
5554 } catch (SQLException e ) {
5655 logger .severe ("Failed to connect to SQLite database" );
5756 logger .severe ("Error: " + e .getMessage ());
@@ -65,7 +64,7 @@ public List<Home> getHomes(UUID uuid) {
6564 }
6665 List <Home > homes = new ArrayList <>();
6766 String query = "SELECT * FROM homes WHERE player_uuid = ?" ;
68- try (PreparedStatement statement = connection .prepareStatement (query )) {
67+ try (PreparedStatement statement = getConnection () .prepareStatement (query )) {
6968 statement .setString (1 , uuid .toString ());
7069 try (ResultSet resultSet = statement .executeQuery ()) {
7170 while (resultSet .next ()) {
@@ -101,13 +100,13 @@ public List<Home> getHomes(UUID uuid) {
101100 public boolean deleteHome (UUID uuid , String homeName ) {
102101 // Prepare the SQL statement to check if the home exists
103102 String checkIfExistsQuery = "SELECT * FROM homes WHERE player_uuid = ? AND home_name = ?" ;
104- try (PreparedStatement homeExists = connection .prepareStatement (checkIfExistsQuery )) {
103+ try (PreparedStatement homeExists = getConnection () .prepareStatement (checkIfExistsQuery )) {
105104 homeExists .setString (1 , uuid .toString ());
106105 homeExists .setString (2 , homeName );
107106 try (ResultSet resultSet = homeExists .executeQuery ()) {
108107 if (resultSet .next ()) { // Home exists
109108 String deleteQuery = "DELETE FROM homes WHERE player_uuid = ? AND home_name = ?" ;
110- try (PreparedStatement deleteStatement = connection .prepareStatement (deleteQuery )) {
109+ try (PreparedStatement deleteStatement = getConnection () .prepareStatement (deleteQuery )) {
111110 deleteStatement .setString (1 , uuid .toString ());
112111 deleteStatement .setString (2 , homeName );
113112 deleteStatement .executeUpdate ();
@@ -127,9 +126,9 @@ public boolean deleteHome(UUID uuid, String homeName) {
127126
128127 public boolean setHome (UUID uuid , Location location , String homeName ) {
129128 String insertQuery = "REPLACE INTO homes " +
130- "(player_uuid_and_name, player_uuid, home_name, world_uuid, location_x, location_y, location_z, yaw, pitch) " +
131- "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)" ;
132- try (PreparedStatement insertStatement = connection .prepareStatement (insertQuery )) {
129+ "(player_uuid_and_name, player_uuid, home_name, world_uuid, location_x, location_y, location_z, yaw, pitch) " +
130+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)" ;
131+ try (PreparedStatement insertStatement = getConnection () .prepareStatement (insertQuery )) {
133132 insertStatement .setString (1 , uuid + homeName );
134133 insertStatement .setString (2 , uuid .toString ());
135134 insertStatement .setString (3 , homeName );
@@ -153,7 +152,7 @@ public boolean setHome(UUID uuid, Location location, String homeName) {
153152 private void updateCache (UUID uuid ) {
154153 List <Home > homes = new ArrayList <>();
155154 String query = "SELECT * FROM homes WHERE player_uuid = ?" ;
156- try (PreparedStatement statement = connection .prepareStatement (query )) {
155+ try (PreparedStatement statement = getConnection () .prepareStatement (query )) {
157156 statement .setString (1 , uuid .toString ());
158157 try (ResultSet resultSet = statement .executeQuery ()) {
159158 while (resultSet .next ()) {
@@ -183,6 +182,13 @@ public void removePlayerFromCache(UUID uuid) {
183182 cachedHomes .remove (uuid );
184183 }
185184
185+ private Connection getConnection () throws SQLException {
186+ if (connection == null || connection .isClosed () || !connection .isValid (2 )) {
187+ connection = sqlOrSqlLite ();
188+ }
189+ return connection ;
190+ }
191+
186192 private Connection sqlOrSqlLite () throws SQLException {
187193 if (ConfigHandler .getInstance ().isUsingMysql ()) {
188194 return DriverManager .getConnection ("jdbc:mysql://" + ConfigHandler .getInstance ().getIp () + "/" + ConfigHandler .getInstance ().getName (), ConfigHandler .getInstance ().getUsername (), ConfigHandler .getInstance ().getPassword ());
0 commit comments