Commit b578659b3b83e20f09a4b84256251365608dd613

Authored by Andrew Shvayka
Committed by GitHub
2 parents 6af34cf0 9c633eee

Merge pull request #4697 from ShvaykaD/feature/cleanup-call-deduplication

Added try with resource to auto-close the connections, statements and result sets in clean up calls
... ... @@ -40,6 +40,7 @@ import org.thingsboard.server.dao.model.sql.EdgeInfoEntity;
40 40 import org.thingsboard.server.dao.relation.RelationDao;
41 41 import org.thingsboard.server.dao.sql.JpaAbstractSearchTextDao;
42 42
  43 +import java.sql.Connection;
43 44 import java.sql.PreparedStatement;
44 45 import java.sql.ResultSet;
45 46 import java.sql.SQLException;
... ... @@ -199,16 +200,17 @@ public class JpaEdgeDao extends JpaAbstractSearchTextDao<EdgeEntity, Edge> imple
199 200
200 201 @Override
201 202 public void cleanupEvents(long ttl) {
202   - try {
203   - log.info("Going to cleanup old edge events using ttl: {}s", ttl);
204   - PreparedStatement stmt = dataSource.getConnection().prepareStatement("call cleanup_edge_events_by_ttl(?,?)");
  203 + log.info("Going to cleanup old edge events using ttl: {}s", ttl);
  204 + try (Connection connection = dataSource.getConnection();
  205 + PreparedStatement stmt = connection.prepareStatement("call cleanup_edge_events_by_ttl(?,?)")) {
205 206 stmt.setLong(1, ttl);
206 207 stmt.setLong(2, 0);
207 208 stmt.execute();
208 209 printWarnings(stmt);
209   - ResultSet resultSet = stmt.getResultSet();
210   - resultSet.next();
211   - log.info("Total edge events removed by TTL: [{}]", resultSet.getLong(1));
  210 + try (ResultSet resultSet = stmt.getResultSet()) {
  211 + resultSet.next();
  212 + log.info("Total edge events removed by TTL: [{}]", resultSet.getLong(1));
  213 + }
212 214 } catch (SQLException e) {
213 215 log.error("SQLException occurred during edge events TTL task execution ", e);
214 216 }
... ...
... ... @@ -39,6 +39,7 @@ import org.thingsboard.server.dao.event.EventDao;
39 39 import org.thingsboard.server.dao.model.sql.EventEntity;
40 40 import org.thingsboard.server.dao.sql.JpaAbstractDao;
41 41
  42 +import java.sql.Connection;
42 43 import java.sql.PreparedStatement;
43 44 import java.sql.ResultSet;
44 45 import java.sql.SQLException;
... ... @@ -260,17 +261,18 @@ public class JpaBaseEventDao extends JpaAbstractDao<EventEntity, Event> implemen
260 261
261 262 @Override
262 263 public void cleanupEvents(long otherEventsTtl, long debugEventsTtl) {
263   - try {
264   - log.info("Going to cleanup old events using debug events ttl: {}s and other events ttl: {}s", debugEventsTtl, otherEventsTtl);
265   - PreparedStatement stmt = dataSource.getConnection().prepareStatement("call cleanup_events_by_ttl(?,?,?)");
  264 + log.info("Going to cleanup old events using debug events ttl: {}s and other events ttl: {}s", debugEventsTtl, otherEventsTtl);
  265 + try (Connection connection = dataSource.getConnection();
  266 + PreparedStatement stmt = connection.prepareStatement("call cleanup_events_by_ttl(?,?,?)")) {
266 267 stmt.setLong(1, otherEventsTtl);
267 268 stmt.setLong(2, debugEventsTtl);
268 269 stmt.setLong(3, 0);
269 270 stmt.execute();
270 271 printWarnings(stmt);
271   - ResultSet resultSet = stmt.getResultSet();
272   - resultSet.next();
273   - log.info("Total events removed by TTL: [{}]", resultSet.getLong(1));
  272 + try (ResultSet resultSet = stmt.getResultSet()){
  273 + resultSet.next();
  274 + log.info("Total events removed by TTL: [{}]", resultSet.getLong(1));
  275 + }
274 276 } catch (SQLException e) {
275 277 log.error("SQLException occurred during events TTL task execution ", e);
276 278 }
... ...
... ... @@ -29,6 +29,7 @@ import org.thingsboard.server.dao.model.ModelConstants;
29 29 import org.thingsboard.server.dao.sql.ScheduledLogExecutorComponent;
30 30
31 31 import javax.annotation.Nullable;
  32 +import java.sql.Connection;
32 33 import java.sql.PreparedStatement;
33 34 import java.sql.ResultSet;
34 35 import java.sql.SQLException;
... ... @@ -67,17 +68,18 @@ public abstract class AbstractSqlTimeseriesDao extends BaseAbstractSqlTimeseries
67 68 private long systemTtl;
68 69
69 70 public void cleanup(long systemTtl) {
70   - try {
71   - log.info("Going to cleanup old timeseries data using ttl: {}s", systemTtl);
72   - PreparedStatement stmt = dataSource.getConnection().prepareStatement("call cleanup_timeseries_by_ttl(?,?,?)");
  71 + log.info("Going to cleanup old timeseries data using ttl: {}s", systemTtl);
  72 + try (Connection connection = dataSource.getConnection();
  73 + PreparedStatement stmt = connection.prepareStatement("call cleanup_timeseries_by_ttl(?,?,?)")) {
73 74 stmt.setObject(1, ModelConstants.NULL_UUID);
74 75 stmt.setLong(2, systemTtl);
75 76 stmt.setLong(3, 0);
76 77 stmt.execute();
77 78 printWarnings(stmt);
78   - ResultSet resultSet = stmt.getResultSet();
79   - resultSet.next();
80   - log.info("Total telemetry removed stats by TTL for entities: [{}]", resultSet.getLong(1));
  79 + try (ResultSet resultSet = stmt.getResultSet()) {
  80 + resultSet.next();
  81 + log.info("Total telemetry removed stats by TTL for entities: [{}]", resultSet.getLong(1));
  82 + }
81 83 } catch (SQLException e) {
82 84 log.error("SQLException occurred during timeseries TTL task execution ", e);
83 85 }
... ...
... ... @@ -35,6 +35,7 @@ import org.thingsboard.server.dao.timeseries.SqlTsPartitionDate;
35 35 import org.thingsboard.server.dao.util.PsqlDao;
36 36 import org.thingsboard.server.dao.util.SqlTsDao;
37 37
  38 +import java.sql.Connection;
38 39 import java.sql.PreparedStatement;
39 40 import java.sql.ResultSet;
40 41 import java.sql.SQLException;
... ... @@ -104,17 +105,18 @@ public class JpaPsqlTimeseriesDao extends AbstractChunkedAggregationTimeseriesDa
104 105 }
105 106
106 107 private void cleanupPartitions(long systemTtl) {
107   - try {
108   - log.info("Going to cleanup old timeseries data partitions using partition type: {} and ttl: {}s", partitioning, systemTtl);
109   - PreparedStatement stmt = dataSource.getConnection().prepareStatement("call drop_partitions_by_max_ttl(?,?,?)");
  108 + log.info("Going to cleanup old timeseries data partitions using partition type: {} and ttl: {}s", partitioning, systemTtl);
  109 + try (Connection connection = dataSource.getConnection();
  110 + PreparedStatement stmt = connection.prepareStatement("call drop_partitions_by_max_ttl(?,?,?)")) {
110 111 stmt.setString(1, partitioning);
111 112 stmt.setLong(2, systemTtl);
112 113 stmt.setLong(3, 0);
113 114 stmt.execute();
114 115 printWarnings(stmt);
115   - ResultSet resultSet = stmt.getResultSet();
116   - resultSet.next();
117   - log.info("Total partitions removed by TTL: [{}]", resultSet.getLong(1));
  116 + try (ResultSet resultSet = stmt.getResultSet()) {
  117 + resultSet.next();
  118 + log.info("Total partitions removed by TTL: [{}]", resultSet.getLong(1));
  119 + }
118 120 } catch (SQLException e) {
119 121 log.error("SQLException occurred during TTL task execution ", e);
120 122 }
... ...