|
|
@@ -27,7 +27,6 @@ public class Mysql implements Serializable {
|
|
|
protected transient Connection conn;
|
|
|
|
|
|
protected Mysql() {
|
|
|
- conn = getConnection();
|
|
|
}
|
|
|
|
|
|
public static Mysql getInstance() {
|
|
|
@@ -38,10 +37,12 @@ public class Mysql implements Serializable {
|
|
|
name = name.replace(" ", "-");
|
|
|
name = name.replace("\\.", "");
|
|
|
final String sql = "INSERT INTO Country (name) VALUES (?) ON DUPLICATE KEY UPDATE name = ?";
|
|
|
- try (PreparedStatement stat = conn.prepareStatement(sql)) {
|
|
|
+ try (PreparedStatement stat = getConnection().prepareStatement(sql)) {
|
|
|
stat.setString(1, name);
|
|
|
stat.setString(2, name);
|
|
|
stat.executeUpdate();
|
|
|
+ } finally {
|
|
|
+ closeConnection();
|
|
|
}
|
|
|
|
|
|
return getId("Country", name, -1, -1);
|
|
|
@@ -57,7 +58,7 @@ public class Mysql implements Serializable {
|
|
|
countryId = addCountry(country);
|
|
|
final String sql = "INSERT INTO League (name, sportId, countryId) VALUES (?, ?, ?) "
|
|
|
+ "ON DUPLICATE KEY UPDATE name = ?";
|
|
|
- try (PreparedStatement stat = conn.prepareStatement(sql)) {
|
|
|
+ try (PreparedStatement stat = getConnection().prepareStatement(sql)) {
|
|
|
stat.setString(1, leagueName);
|
|
|
stat.setInt(2, sportId);
|
|
|
stat.setInt(3, countryId);
|
|
|
@@ -67,6 +68,8 @@ public class Mysql implements Serializable {
|
|
|
}
|
|
|
} catch (SQLException e) {
|
|
|
e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ closeConnection();
|
|
|
}
|
|
|
return getId("League", leagueName, countryId, -1);
|
|
|
}
|
|
|
@@ -88,7 +91,7 @@ public class Mysql implements Serializable {
|
|
|
final ResultSet rs;
|
|
|
int gameId = -1;
|
|
|
final String date = result.getGameDate().format(DateTimeFormatter.ISO_DATE);
|
|
|
- try (PreparedStatement stat = conn.prepareStatement(selectSql)) {
|
|
|
+ try (PreparedStatement stat = getConnection().prepareStatement(selectSql)) {
|
|
|
stat.setInt(1, homeTeamId);
|
|
|
stat.setInt(2, awayTeamId);
|
|
|
stat.setString(3, date);
|
|
|
@@ -97,6 +100,8 @@ public class Mysql implements Serializable {
|
|
|
while (rs.next()) {
|
|
|
gameId = rs.getInt("id");
|
|
|
}
|
|
|
+ } finally {
|
|
|
+ closeConnection();
|
|
|
}
|
|
|
|
|
|
if (gameId != -1) {
|
|
|
@@ -104,7 +109,7 @@ public class Mysql implements Serializable {
|
|
|
+ " SET homeScore = ?, awayScore = ?, overtime = ?, odds1 = ?, oddsX = ?, odds2 = ? "
|
|
|
+ "WHERE homeTeamId = ? AND awayTeamId = ? AND DATE(gameDate) = ?";
|
|
|
|
|
|
- try (PreparedStatement stat = conn.prepareStatement(sql)) {
|
|
|
+ try (PreparedStatement stat = getConnection().prepareStatement(sql)) {
|
|
|
stat.setInt(1, result.getHomeScore());
|
|
|
stat.setInt(2, result.getAwayScore());
|
|
|
stat.setBoolean(3, result.isOvertime());
|
|
|
@@ -122,7 +127,7 @@ public class Mysql implements Serializable {
|
|
|
final String sql = "INSERT INTO " + result.getTableName()
|
|
|
+ " (homeTeamId, awayTeamId, homeScore, awayScore, overtime, odds1, oddsX, odds2, countryId, gameDate, season, leagueId) "
|
|
|
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE homeScore = ?, awayScore = ?, odds1 = ?, oddsX = ?, odds2 = ?";
|
|
|
- try (PreparedStatement stat = conn.prepareStatement(sql)) {
|
|
|
+ try (PreparedStatement stat = getConnection().prepareStatement(sql)) {
|
|
|
stat.setInt(1, homeTeamId);
|
|
|
stat.setInt(2, awayTeamId);
|
|
|
stat.setInt(3, result.getHomeScore());
|
|
|
@@ -146,6 +151,8 @@ public class Mysql implements Serializable {
|
|
|
}
|
|
|
} catch (SQLException e) {
|
|
|
e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ closeConnection();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -153,7 +160,7 @@ public class Mysql implements Serializable {
|
|
|
|
|
|
final String sql = "INSERT INTO SoccerResults (homeTeamId, awayTeamId, homeScore, awayScore, overtime, odds1, oddsX, odds2, countryId, gameDate, season, leagueId) "
|
|
|
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE homeScore = ?, awayScore = ?, odds1 = ?, oddsX = ?, odds2 = ?";
|
|
|
- try (PreparedStatement stat = conn.prepareStatement(sql)) {
|
|
|
+ try (PreparedStatement stat = getConnection().prepareStatement(sql)) {
|
|
|
|
|
|
for (ResultDTO resultDTO : resultsToInsert) {
|
|
|
|
|
|
@@ -187,6 +194,8 @@ public class Mysql implements Serializable {
|
|
|
stat.executeBatch();
|
|
|
} catch (SQLException e) {
|
|
|
e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ closeConnection();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -194,10 +203,12 @@ public class Mysql implements Serializable {
|
|
|
sport = sport.replace(" ", "-");
|
|
|
sport = sport.replace("\\.", "");
|
|
|
final String sql = "INSERT INTO Sport (name) VALUES (?) ON DUPLICATE KEY UPDATE name = ?";
|
|
|
- try (PreparedStatement stat = conn.prepareStatement(sql)) {
|
|
|
+ try (PreparedStatement stat = getConnection().prepareStatement(sql)) {
|
|
|
stat.setString(1, sport);
|
|
|
stat.setString(2, sport);
|
|
|
stat.executeUpdate();
|
|
|
+ } finally {
|
|
|
+ closeConnection();
|
|
|
}
|
|
|
return getId("Sport", sport, -1, -1);
|
|
|
}
|
|
|
@@ -205,7 +216,7 @@ public class Mysql implements Serializable {
|
|
|
public int getCountryId(String country) {
|
|
|
final String sql = "SELECT id from Country WHERE name = ?";
|
|
|
int id = -1;
|
|
|
- try (PreparedStatement stat = conn.prepareStatement(sql)) {
|
|
|
+ try (PreparedStatement stat = getConnection().prepareStatement(sql)) {
|
|
|
stat.setString(1, country.trim());
|
|
|
|
|
|
final ResultSet rs = stat.executeQuery();
|
|
|
@@ -214,6 +225,8 @@ public class Mysql implements Serializable {
|
|
|
}
|
|
|
} catch (SQLException e) {
|
|
|
e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ closeConnection();
|
|
|
}
|
|
|
return id;
|
|
|
}
|
|
|
@@ -221,7 +234,7 @@ public class Mysql implements Serializable {
|
|
|
public CurrentParsing getCurrentParsing() {
|
|
|
final CurrentParsing returnValue = new CurrentParsing();
|
|
|
final String sql = "SELECT * FROM parsing";
|
|
|
- try (PreparedStatement stat = conn.prepareStatement(sql)) {
|
|
|
+ try (PreparedStatement stat = getConnection().prepareStatement(sql)) {
|
|
|
final ResultSet rs = stat.executeQuery();
|
|
|
while (rs.next()) {
|
|
|
returnValue.setDone(rs.getBoolean("done"));
|
|
|
@@ -232,13 +245,19 @@ public class Mysql implements Serializable {
|
|
|
}
|
|
|
} catch (final SQLException e) {
|
|
|
e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ closeConnection();
|
|
|
}
|
|
|
return returnValue;
|
|
|
}
|
|
|
|
|
|
public Connection getDbConnection() {
|
|
|
- if (conn == null) {
|
|
|
- conn = getConnection();
|
|
|
+ try {
|
|
|
+ if (conn == null || conn.isClosed()) {
|
|
|
+ conn = getConnection();
|
|
|
+ }
|
|
|
+ } catch (SQLException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
}
|
|
|
return conn;
|
|
|
}
|
|
|
@@ -247,7 +266,7 @@ public class Mysql implements Serializable {
|
|
|
String returnValue = "";
|
|
|
|
|
|
final String sql = "SELECT parsedYear FROM League WHERE name = ? AND countryId = ?";
|
|
|
- try (PreparedStatement stat = conn.prepareStatement(sql)) {
|
|
|
+ try (PreparedStatement stat = getConnection().prepareStatement(sql)) {
|
|
|
stat.setString(1, leagueName);
|
|
|
stat.setInt(2, countryId);
|
|
|
|
|
|
@@ -258,6 +277,8 @@ public class Mysql implements Serializable {
|
|
|
}
|
|
|
} catch (final SQLException e) {
|
|
|
e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ closeConnection();
|
|
|
}
|
|
|
|
|
|
return returnValue;
|
|
|
@@ -278,6 +299,8 @@ public class Mysql implements Serializable {
|
|
|
}
|
|
|
} catch (SQLException e) {
|
|
|
e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ closeConnection();
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
@@ -286,7 +309,7 @@ public class Mysql implements Serializable {
|
|
|
public String getLastSeason(String leagueName, int countryId) {
|
|
|
String sql = "SELECT season FROM SoccerResults WHERE leagueId = (SELECT id FROM League WHERE name = ? AND countryId = ?) ORDER BY gameDate DESC limit 1";
|
|
|
String result = null;
|
|
|
- try (PreparedStatement stat = conn.prepareStatement(sql)) {
|
|
|
+ try (PreparedStatement stat = getConnection().prepareStatement(sql)) {
|
|
|
stat.setString(1, leagueName);
|
|
|
stat.setInt(2, countryId);
|
|
|
|
|
|
@@ -297,6 +320,8 @@ public class Mysql implements Serializable {
|
|
|
}
|
|
|
} catch (SQLException e) {
|
|
|
e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ closeConnection();
|
|
|
}
|
|
|
|
|
|
if (Strings.isNullOrEmpty(result)) {
|
|
|
@@ -308,7 +333,7 @@ public class Mysql implements Serializable {
|
|
|
public int getLeagueId(int sportId, int countryId, String leagueName) {
|
|
|
final String sql = "SELECT id FROM League WHERE name = ? AND countryId = ? AND sportId = ?";
|
|
|
int id = -1;
|
|
|
- try (PreparedStatement stat = conn.prepareStatement(sql)) {
|
|
|
+ try (PreparedStatement stat = getConnection().prepareStatement(sql)) {
|
|
|
stat.setString(1, leagueName.trim());
|
|
|
stat.setInt(2, countryId);
|
|
|
stat.setInt(3, sportId);
|
|
|
@@ -320,6 +345,8 @@ public class Mysql implements Serializable {
|
|
|
}
|
|
|
} catch (SQLException e) {
|
|
|
e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ closeConnection();
|
|
|
}
|
|
|
if (id == -1) {
|
|
|
String message = "Cound not find league " + leagueName + " countryId " + countryId + " sportId " + sportId;
|
|
|
@@ -336,7 +363,7 @@ public class Mysql implements Serializable {
|
|
|
teamId = getId("Team", teamName, countryId, leagueId);
|
|
|
if (teamId <= 0) {
|
|
|
final String insertSql = "INSERT INTO Team (name, sportId, countryId, leagueId) VALUES (? ,? ,? ,?)";
|
|
|
- try (PreparedStatement stat = conn.prepareStatement(insertSql, Statement.RETURN_GENERATED_KEYS)) {
|
|
|
+ try (PreparedStatement stat = getConnection().prepareStatement(insertSql, Statement.RETURN_GENERATED_KEYS)) {
|
|
|
stat.setString(1, teamName.trim());
|
|
|
stat.setInt(2, sportId);
|
|
|
stat.setInt(3, countryId);
|
|
|
@@ -348,6 +375,8 @@ public class Mysql implements Serializable {
|
|
|
teamId = generatedKeys.getInt(1);
|
|
|
} catch (SQLException e) {
|
|
|
e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ closeConnection();
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
@@ -360,7 +389,7 @@ public class Mysql implements Serializable {
|
|
|
public int getSportId(String sportName) {
|
|
|
final String sql = "SELECT id from Sport WHERE name = ?";
|
|
|
int id = 0;
|
|
|
- try (PreparedStatement stat = conn.prepareStatement(sql)) {
|
|
|
+ try (PreparedStatement stat = getConnection().prepareStatement(sql)) {
|
|
|
stat.setString(1, sportName.trim());
|
|
|
|
|
|
final ResultSet rs = stat.executeQuery();
|
|
|
@@ -369,14 +398,16 @@ public class Mysql implements Serializable {
|
|
|
}
|
|
|
} catch (final SQLException e) {
|
|
|
e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ closeConnection();
|
|
|
}
|
|
|
return id;
|
|
|
}
|
|
|
|
|
|
public void setParsingForLeague(int leagueId, int sportId, int countryId, LocalDateTime gameDate,
|
|
|
- int currentParsePage, String parsedYear) {
|
|
|
+ int currentParsePage, String parsedYear) {
|
|
|
final String sql = "UPDATE League SET parsedYear = ?, parsedPage = ?, lastParsedGameDate = ? WHERE sportId = ? AND countryId = ? AND id = ?";
|
|
|
- try (PreparedStatement stat = conn.prepareStatement(sql)) {
|
|
|
+ try (PreparedStatement stat = getConnection().prepareStatement(sql)) {
|
|
|
stat.setString(1, parsedYear);
|
|
|
stat.setInt(2, currentParsePage);
|
|
|
stat.setString(3, gameDate.toString());
|
|
|
@@ -387,24 +418,26 @@ public class Mysql implements Serializable {
|
|
|
stat.executeUpdate();
|
|
|
} catch (final SQLException e) {
|
|
|
e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ closeConnection();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
protected Connection getConnection() {
|
|
|
- if (conn == null) {
|
|
|
- try {
|
|
|
+ try {
|
|
|
+ if (conn == null || conn.isClosed()) {
|
|
|
Class.forName("com.mysql.cj.jdbc.Driver");
|
|
|
Properties prop = new Properties();
|
|
|
prop.put("user", USERNAME);
|
|
|
prop.put("password", PASSWORD);
|
|
|
prop.put("useSSL", "false");
|
|
|
conn = DriverManager.getConnection(URL + DATABASE + TIMEZONE_FIX, prop);
|
|
|
- } catch (final SQLException e) {
|
|
|
- throw new RuntimeException(e.getMessage(), e);
|
|
|
- } catch (ClassNotFoundException e) {
|
|
|
- System.out.println("ClassNotFoundException: " + e.getMessage());
|
|
|
- throw new RuntimeException(e);
|
|
|
}
|
|
|
+ } catch (final SQLException e) {
|
|
|
+ throw new RuntimeException(e.getMessage(), e);
|
|
|
+ } catch (ClassNotFoundException e) {
|
|
|
+ System.out.println("ClassNotFoundException: " + e.getMessage());
|
|
|
+ throw new RuntimeException(e);
|
|
|
}
|
|
|
return conn;
|
|
|
}
|
|
|
@@ -418,7 +451,7 @@ public class Mysql implements Serializable {
|
|
|
if (leagueId > -1) {
|
|
|
sql += " AND leagueId = ?";
|
|
|
}
|
|
|
- try (PreparedStatement stat = conn.prepareStatement(sql)) {
|
|
|
+ try (PreparedStatement stat = getConnection().prepareStatement(sql)) {
|
|
|
stat.setString(1, name.trim());
|
|
|
if (countryId > -1 && leagueId > -1) {
|
|
|
stat.setInt(2, countryId);
|
|
|
@@ -434,7 +467,19 @@ public class Mysql implements Serializable {
|
|
|
}
|
|
|
} catch (SQLException e) {
|
|
|
e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ closeConnection();
|
|
|
}
|
|
|
return id;
|
|
|
}
|
|
|
+
|
|
|
+ public void closeConnection() {
|
|
|
+ try {
|
|
|
+ if (!conn.isClosed()) {
|
|
|
+ conn.close();
|
|
|
+ }
|
|
|
+ } catch (SQLException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|