From b7a91036f97d5aabcd70376578a053f597d665c4 Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Sun, 26 Jun 2022 10:03:02 +0100 Subject: [PATCH] Cleanup Transactions. (#275) - use hibernate 6 methods - Don't use session.getTransaction() - regex to validate channelId for saving channels --- src/main/java/me/kavin/piped/Main.java | 4 +- .../me/kavin/piped/utils/ResponseHelper.java | 107 +++++++++--------- 2 files changed, 55 insertions(+), 56 deletions(-) diff --git a/src/main/java/me/kavin/piped/Main.java b/src/main/java/me/kavin/piped/Main.java index fc42324..79bd6fd 100644 --- a/src/main/java/me/kavin/piped/Main.java +++ b/src/main/java/me/kavin/piped/Main.java @@ -83,9 +83,7 @@ public class Main { public void run() { try (Session s = DatabaseSessionFactory.createSession()) { - Transaction tr = s.getTransaction(); - - tr.begin(); + Transaction tr = s.beginTransaction(); Query query = s.createQuery("delete from Video where uploaded < :time").setParameter("time", System.currentTimeMillis() - TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION)); diff --git a/src/main/java/me/kavin/piped/utils/ResponseHelper.java b/src/main/java/me/kavin/piped/utils/ResponseHelper.java index af82291..dccc246 100644 --- a/src/main/java/me/kavin/piped/utils/ResponseHelper.java +++ b/src/main/java/me/kavin/piped/utils/ResponseHelper.java @@ -210,10 +210,9 @@ public class ResponseHelper { || !channel.getUploaderAvatar().equals(info.getAvatarUrl())) { channel.setVerified(info.isVerified()); channel.setUploaderAvatar(info.getAvatarUrl()); - if (!s.getTransaction().isActive()) - s.getTransaction().begin(); - s.update(channel); - s.getTransaction().commit(); + var tr = s.beginTransaction(); + s.merge(channel); + tr.commit(); } for (StreamInfoItem item : info.getRelatedItems()) { long time = item.getUploadDate() != null @@ -665,9 +664,9 @@ public class ResponseHelper { User newuser = new User(user, argon2PasswordEncoder.encode(pass), Set.of()); - s.save(newuser); - s.getTransaction().begin(); - s.getTransaction().commit(); + var tr = s.beginTransaction(); + s.persist(newuser); + tr.commit(); return mapper.writeValueAsBytes(new LoginResponse(newuser.getSessionId())); @@ -728,12 +727,11 @@ public class ResponseHelper { return mapper.writeValueAsBytes(new IncorrectCredentialsResponse()); try { - s.delete(user); + var tr = s.beginTransaction(); + s.remove(user); + tr.commit(); - s.getTransaction().begin(); - s.getTransaction().commit(); - - Multithreading.runAsync(() -> pruneUnusedPlaylistVideos()); + Multithreading.runAsync(ResponseHelper::pruneUnusedPlaylistVideos); } catch (Exception e) { return mapper.writeValueAsBytes(new ErrorResponse(ExceptionUtils.getStackTrace(e), e.getMessage())); } @@ -753,9 +751,10 @@ public class ResponseHelper { if (!user.getSubscribed().contains(channelId)) { user.getSubscribed().add(channelId); - s.update(user); - s.getTransaction().begin(); - s.getTransaction().commit(); + + var tr = s.beginTransaction(); + s.merge(user); + tr.commit(); Multithreading.runAsync(() -> { try (Session s2 = DatabaseSessionFactory.createSession()) { @@ -783,10 +782,10 @@ public class ResponseHelper { if (user != null) { try (Session s = DatabaseSessionFactory.createSession()) { - s.getTransaction().begin(); + var tr = s.beginTransaction(); s.createNativeQuery("delete from users_subscribed where subscriber = :id and channel = :channel") .setParameter("id", user.getId()).setParameter("channel", channelId).executeUpdate(); - s.getTransaction().commit(); + tr.commit(); return mapper.writeValueAsBytes(new AcceptedResponse()); } @@ -823,7 +822,6 @@ public class ResponseHelper { // Get all videos from subscribed channels, with channel info CriteriaQuery