Cleanup Transactions. (#275)

- use hibernate 6 methods
- Don't use session.getTransaction()
- regex to validate channelId for saving channels
This commit is contained in:
Kavin 2022-06-26 10:03:02 +01:00 committed by GitHub
parent e8e055a676
commit b7a91036f9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 56 deletions

View file

@ -83,9 +83,7 @@ public class Main {
public void run() { public void run() {
try (Session s = DatabaseSessionFactory.createSession()) { try (Session s = DatabaseSessionFactory.createSession()) {
Transaction tr = s.getTransaction(); Transaction tr = s.beginTransaction();
tr.begin();
Query<?> query = s.createQuery("delete from Video where uploaded < :time").setParameter("time", Query<?> query = s.createQuery("delete from Video where uploaded < :time").setParameter("time",
System.currentTimeMillis() - TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION)); System.currentTimeMillis() - TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION));

View file

@ -210,10 +210,9 @@ public class ResponseHelper {
|| !channel.getUploaderAvatar().equals(info.getAvatarUrl())) { || !channel.getUploaderAvatar().equals(info.getAvatarUrl())) {
channel.setVerified(info.isVerified()); channel.setVerified(info.isVerified());
channel.setUploaderAvatar(info.getAvatarUrl()); channel.setUploaderAvatar(info.getAvatarUrl());
if (!s.getTransaction().isActive()) var tr = s.beginTransaction();
s.getTransaction().begin(); s.merge(channel);
s.update(channel); tr.commit();
s.getTransaction().commit();
} }
for (StreamInfoItem item : info.getRelatedItems()) { for (StreamInfoItem item : info.getRelatedItems()) {
long time = item.getUploadDate() != null long time = item.getUploadDate() != null
@ -665,9 +664,9 @@ public class ResponseHelper {
User newuser = new User(user, argon2PasswordEncoder.encode(pass), Set.of()); User newuser = new User(user, argon2PasswordEncoder.encode(pass), Set.of());
s.save(newuser); var tr = s.beginTransaction();
s.getTransaction().begin(); s.persist(newuser);
s.getTransaction().commit(); tr.commit();
return mapper.writeValueAsBytes(new LoginResponse(newuser.getSessionId())); return mapper.writeValueAsBytes(new LoginResponse(newuser.getSessionId()));
@ -728,12 +727,11 @@ public class ResponseHelper {
return mapper.writeValueAsBytes(new IncorrectCredentialsResponse()); return mapper.writeValueAsBytes(new IncorrectCredentialsResponse());
try { try {
s.delete(user); var tr = s.beginTransaction();
s.remove(user);
tr.commit();
s.getTransaction().begin(); Multithreading.runAsync(ResponseHelper::pruneUnusedPlaylistVideos);
s.getTransaction().commit();
Multithreading.runAsync(() -> pruneUnusedPlaylistVideos());
} catch (Exception e) { } catch (Exception e) {
return mapper.writeValueAsBytes(new ErrorResponse(ExceptionUtils.getStackTrace(e), e.getMessage())); return mapper.writeValueAsBytes(new ErrorResponse(ExceptionUtils.getStackTrace(e), e.getMessage()));
} }
@ -753,9 +751,10 @@ public class ResponseHelper {
if (!user.getSubscribed().contains(channelId)) { if (!user.getSubscribed().contains(channelId)) {
user.getSubscribed().add(channelId); user.getSubscribed().add(channelId);
s.update(user);
s.getTransaction().begin(); var tr = s.beginTransaction();
s.getTransaction().commit(); s.merge(user);
tr.commit();
Multithreading.runAsync(() -> { Multithreading.runAsync(() -> {
try (Session s2 = DatabaseSessionFactory.createSession()) { try (Session s2 = DatabaseSessionFactory.createSession()) {
@ -783,10 +782,10 @@ public class ResponseHelper {
if (user != null) { if (user != null) {
try (Session s = DatabaseSessionFactory.createSession()) { try (Session s = DatabaseSessionFactory.createSession()) {
s.getTransaction().begin(); var tr = s.beginTransaction();
s.createNativeQuery("delete from users_subscribed where subscriber = :id and channel = :channel") s.createNativeQuery("delete from users_subscribed where subscriber = :id and channel = :channel")
.setParameter("id", user.getId()).setParameter("channel", channelId).executeUpdate(); .setParameter("id", user.getId()).setParameter("channel", channelId).executeUpdate();
s.getTransaction().commit(); tr.commit();
return mapper.writeValueAsBytes(new AcceptedResponse()); return mapper.writeValueAsBytes(new AcceptedResponse());
} }
@ -823,7 +822,6 @@ public class ResponseHelper {
// Get all videos from subscribed channels, with channel info // Get all videos from subscribed channels, with channel info
CriteriaQuery<Video> criteria = cb.createQuery(Video.class); CriteriaQuery<Video> criteria = cb.createQuery(Video.class);
criteria.distinct(true);
var root = criteria.from(Video.class); var root = criteria.from(Video.class);
var userRoot = criteria.from(User.class); var userRoot = criteria.from(User.class);
root.fetch("channel", JoinType.INNER); root.fetch("channel", JoinType.INNER);
@ -872,7 +870,6 @@ public class ResponseHelper {
// Get all videos from subscribed channels, with channel info // Get all videos from subscribed channels, with channel info
CriteriaQuery<Video> criteria = cb.createQuery(Video.class); CriteriaQuery<Video> criteria = cb.createQuery(Video.class);
criteria.distinct(true);
var root = criteria.from(Video.class); var root = criteria.from(Video.class);
var userRoot = criteria.from(User.class); var userRoot = criteria.from(User.class);
root.fetch("channel", JoinType.INNER); root.fetch("channel", JoinType.INNER);
@ -931,9 +928,9 @@ public class ResponseHelper {
} }
if (channelIds.length > 0) { if (channelIds.length > 0) {
sess.getTransaction().begin(); var tr = sess.beginTransaction();
sess.update(user); sess.merge(user);
sess.getTransaction().commit(); tr.commit();
} }
} }
}); });
@ -1005,9 +1002,10 @@ public class ResponseHelper {
try (Session s = DatabaseSessionFactory.createSession()) { try (Session s = DatabaseSessionFactory.createSession()) {
var playlist = new me.kavin.piped.utils.obj.db.Playlist(name, user, "https://i.ytimg.com/"); var playlist = new me.kavin.piped.utils.obj.db.Playlist(name, user, "https://i.ytimg.com/");
var tr = s.beginTransaction();
s.persist(playlist); s.persist(playlist);
s.getTransaction().begin(); tr.commit();
s.getTransaction().commit();
ObjectNode response = mapper.createObjectNode(); ObjectNode response = mapper.createObjectNode();
response.put("playlistId", String.valueOf(playlist.getPlaylistId())); response.put("playlistId", String.valueOf(playlist.getPlaylistId()));
@ -1037,10 +1035,9 @@ public class ResponseHelper {
return mapper.writeValueAsBytes(mapper.createObjectNode() return mapper.writeValueAsBytes(mapper.createObjectNode()
.put("error", "You do not own this playlist")); .put("error", "You do not own this playlist"));
s.delete(playlist); var tr = s.beginTransaction();
s.remove(playlist);
s.getTransaction().begin(); tr.commit();
s.getTransaction().commit();
Multithreading.runAsync(() -> pruneUnusedPlaylistVideos()); Multithreading.runAsync(() -> pruneUnusedPlaylistVideos());
} }
@ -1192,10 +1189,10 @@ public class ResponseHelper {
video = new PlaylistVideo(videoId, info.getName(), info.getThumbnailUrl(), info.getDuration(), channel); video = new PlaylistVideo(videoId, info.getName(), info.getThumbnailUrl(), info.getDuration(), channel);
var tr = s.beginTransaction();
s.persist(video); s.persist(video);
tr.commit();
if (!s.getTransaction().isActive())
s.getTransaction().begin();
} }
if (playlist.getVideos().isEmpty()) if (playlist.getVideos().isEmpty())
@ -1203,9 +1200,9 @@ public class ResponseHelper {
playlist.getVideos().add(video); playlist.getVideos().add(video);
if (!s.getTransaction().isActive()) var tr = s.beginTransaction();
s.getTransaction().begin(); s.merge(playlist);
s.getTransaction().commit(); tr.commit();
return mapper.writeValueAsBytes(new AcceptedResponse()); return mapper.writeValueAsBytes(new AcceptedResponse());
} }
@ -1239,11 +1236,9 @@ public class ResponseHelper {
playlist.getVideos().remove(index); playlist.getVideos().remove(index);
s.update(playlist); var tr = s.beginTransaction();
s.merge(playlist);
if (!s.getTransaction().isActive()) tr.commit();
s.getTransaction().begin();
s.getTransaction().commit();
Multithreading.runAsync(() -> pruneUnusedPlaylistVideos()); Multithreading.runAsync(() -> pruneUnusedPlaylistVideos());
@ -1282,9 +1277,9 @@ public class ResponseHelper {
pvQuery.where(cb.not(pvRoot.get("id").in(subQuery))); pvQuery.where(cb.not(pvRoot.get("id").in(subQuery)));
s.getTransaction().begin(); var tr = s.beginTransaction();
s.createQuery(pvQuery).executeUpdate(); s.createQuery(pvQuery).executeUpdate();
s.getTransaction().commit(); tr.commit();
} }
} }
@ -1305,11 +1300,10 @@ public class ResponseHelper {
video = new Video(info.getId(), info.getName(), info.getViewCount(), info.getDuration(), video = new Video(info.getId(), info.getName(), info.getViewCount(), info.getDuration(),
Math.max(infoTime, time), info.getThumbnailUrl(), channel); Math.max(infoTime, time), info.getThumbnailUrl(), channel);
s.save(video); var tr = s.beginTransaction();
s.persist(video);
tr.commit();
if (!s.getTransaction().isActive())
s.getTransaction().begin();
s.getTransaction().commit();
} else if (video != null) { } else if (video != null) {
updateVideo(info.getId(), info, time); updateVideo(info.getId(), info, time);
} }
@ -1369,13 +1363,17 @@ public class ResponseHelper {
} }
if (changed) { if (changed) {
s.update(video); var tr = s.beginTransaction();
if (!s.getTransaction().isActive()) s.getTransaction().begin(); s.merge(video);
s.getTransaction().commit(); tr.commit();
} }
} }
private static me.kavin.piped.utils.obj.db.Channel saveChannel(String channelId) { private static me.kavin.piped.utils.obj.db.Channel saveChannel(String channelId) {
if (!channelId.matches("[A-Za-z\\d_-]+"))
return null;
try (Session s = DatabaseSessionFactory.createSession()) { try (Session s = DatabaseSessionFactory.createSession()) {
final ChannelInfo info; final ChannelInfo info;
@ -1443,16 +1441,19 @@ public class ResponseHelper {
.build()).execute(); .build()).execute();
if (resp.code() == 202) { if (resp.code() == 202) {
if (pubsub == null)
var tr = s.beginTransaction();
if (pubsub == null) {
pubsub = new PubSub(channelId, System.currentTimeMillis()); pubsub = new PubSub(channelId, System.currentTimeMillis());
else s.persist(pubsub);
} else {
pubsub.setSubbedAt(System.currentTimeMillis()); pubsub.setSubbedAt(System.currentTimeMillis());
s.merge(pubsub);
}
s.saveOrUpdate(pubsub); tr.commit();
if (!s.getTransaction().isActive())
s.getTransaction().begin();
s.getTransaction().commit();
} else } else
System.out.println("Failed to subscribe: " + resp.code() + "\n" + Objects.requireNonNull(resp.body()).string()); System.out.println("Failed to subscribe: " + resp.code() + "\n" + Objects.requireNonNull(resp.body()).string());