From e6c0f7c0f4e19afa57044d9175eb34c1482bb29b Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Mon, 4 Jul 2022 18:44:16 +0100 Subject: [PATCH] Cleanup transaction code. (#306) --- src/main/java/me/kavin/piped/Main.java | 3 +- .../me/kavin/piped/utils/ResponseHelper.java | 70 ++++++++++--------- 2 files changed, 37 insertions(+), 36 deletions(-) diff --git a/src/main/java/me/kavin/piped/Main.java b/src/main/java/me/kavin/piped/Main.java index 0cb54fc..8ff5c67 100644 --- a/src/main/java/me/kavin/piped/Main.java +++ b/src/main/java/me/kavin/piped/Main.java @@ -10,7 +10,6 @@ import me.kavin.piped.utils.obj.db.User; import me.kavin.piped.utils.obj.db.Video; import org.hibernate.Session; import org.hibernate.StatelessSession; -import org.hibernate.Transaction; import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.localization.Localization; import org.schabi.newpipe.extractor.services.youtube.YoutubeThrottlingDecrypter; @@ -99,7 +98,7 @@ public class Main { var root = cd.from(Video.class); cd.where(cb.lessThan(root.get("uploaded"), System.currentTimeMillis() - TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION))); - Transaction tr = s.beginTransaction(); + var tr = s.beginTransaction(); var query = s.createMutationQuery(cd); diff --git a/src/main/java/me/kavin/piped/utils/ResponseHelper.java b/src/main/java/me/kavin/piped/utils/ResponseHelper.java index 2e7e556..4421052 100644 --- a/src/main/java/me/kavin/piped/utils/ResponseHelper.java +++ b/src/main/java/me/kavin/piped/utils/ResponseHelper.java @@ -1325,7 +1325,7 @@ public class ResponseHelper { pvQuery.where(cb.not(pvRoot.get("id").in(subQuery))); var tr = s.beginTransaction(); - s.createQuery(pvQuery).executeUpdate(); + s.createMutationQuery(pvQuery).executeUpdate(); tr.commit(); } } @@ -1425,42 +1425,44 @@ public class ResponseHelper { if (!channelId.matches("[A-Za-z\\d_-]+")) return null; - try (Session s = DatabaseSessionFactory.createSession()) { - final ChannelInfo info; + final ChannelInfo info; - try { - info = ChannelInfo.getInfo("https://youtube.com/channel/" + channelId); - } catch (IOException | ExtractionException e) { - ExceptionUtils.rethrow(e); - return null; - } - - var channel = new me.kavin.piped.utils.obj.db.Channel(channelId, info.getName(), - info.getAvatarUrl(), info.isVerified()); - s.persist(channel); - s.beginTransaction().commit(); - - Multithreading.runAsync(() -> { - try { - subscribePubSub(channelId); - } catch (IOException e) { - ExceptionHandler.handle(e); - } - }); - - Multithreading.runAsync(() -> { - for (StreamInfoItem item : info.getRelatedItems()) { - long time = item.getUploadDate() != null - ? item.getUploadDate().offsetDateTime().toInstant().toEpochMilli() - : System.currentTimeMillis(); - if ((System.currentTimeMillis() - time) < TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION)) - handleNewVideo(item.getUrl(), time, channel); - } - }); - - return channel; + try { + info = ChannelInfo.getInfo("https://youtube.com/channel/" + channelId); + } catch (IOException | ExtractionException e) { + ExceptionUtils.rethrow(e); + return null; } + + var channel = new me.kavin.piped.utils.obj.db.Channel(channelId, info.getName(), + info.getAvatarUrl(), info.isVerified()); + + try (Session s = DatabaseSessionFactory.createSession()) { + var tr = s.beginTransaction(); + s.persist(channel); + tr.commit(); + } + + Multithreading.runAsync(() -> { + try { + subscribePubSub(channelId); + } catch (IOException e) { + ExceptionHandler.handle(e); + } + }); + + Multithreading.runAsync(() -> { + for (StreamInfoItem item : info.getRelatedItems()) { + long time = item.getUploadDate() != null + ? item.getUploadDate().offsetDateTime().toInstant().toEpochMilli() + : System.currentTimeMillis(); + if ((System.currentTimeMillis() - time) < TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION)) + handleNewVideo(item.getUrl(), time, channel); + } + }); + + return channel; } public static void subscribePubSub(String channelId) throws IOException {