Cleanup transaction code. (#306)

This commit is contained in:
Kavin 2022-07-04 18:44:16 +01:00 committed by GitHub
parent 7c087d82b3
commit e6c0f7c0f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 36 deletions

View file

@ -10,7 +10,6 @@ import me.kavin.piped.utils.obj.db.User;
import me.kavin.piped.utils.obj.db.Video; import me.kavin.piped.utils.obj.db.Video;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.StatelessSession; import org.hibernate.StatelessSession;
import org.hibernate.Transaction;
import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.localization.Localization; import org.schabi.newpipe.extractor.localization.Localization;
import org.schabi.newpipe.extractor.services.youtube.YoutubeThrottlingDecrypter; import org.schabi.newpipe.extractor.services.youtube.YoutubeThrottlingDecrypter;
@ -99,7 +98,7 @@ public class Main {
var root = cd.from(Video.class); var root = cd.from(Video.class);
cd.where(cb.lessThan(root.get("uploaded"), System.currentTimeMillis() - TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION))); 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); var query = s.createMutationQuery(cd);

View file

@ -1325,7 +1325,7 @@ public class ResponseHelper {
pvQuery.where(cb.not(pvRoot.get("id").in(subQuery))); pvQuery.where(cb.not(pvRoot.get("id").in(subQuery)));
var tr = s.beginTransaction(); var tr = s.beginTransaction();
s.createQuery(pvQuery).executeUpdate(); s.createMutationQuery(pvQuery).executeUpdate();
tr.commit(); tr.commit();
} }
} }
@ -1425,7 +1425,6 @@ public class ResponseHelper {
if (!channelId.matches("[A-Za-z\\d_-]+")) if (!channelId.matches("[A-Za-z\\d_-]+"))
return null; return null;
try (Session s = DatabaseSessionFactory.createSession()) {
final ChannelInfo info; final ChannelInfo info;
@ -1438,8 +1437,12 @@ public class ResponseHelper {
var channel = new me.kavin.piped.utils.obj.db.Channel(channelId, info.getName(), var channel = new me.kavin.piped.utils.obj.db.Channel(channelId, info.getName(),
info.getAvatarUrl(), info.isVerified()); info.getAvatarUrl(), info.isVerified());
try (Session s = DatabaseSessionFactory.createSession()) {
var tr = s.beginTransaction();
s.persist(channel); s.persist(channel);
s.beginTransaction().commit(); tr.commit();
}
Multithreading.runAsync(() -> { Multithreading.runAsync(() -> {
try { try {
@ -1461,7 +1464,6 @@ public class ResponseHelper {
return channel; return channel;
} }
}
public static void subscribePubSub(String channelId) throws IOException { public static void subscribePubSub(String channelId) throws IOException {