mirror of
https://github.com/Athou/commafeed.git
synced 2026-03-21 21:37:29 +00:00
run post login activities in a new transaction to avoid database locks
This commit is contained in:
@@ -6,8 +6,10 @@ import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import org.apache.commons.lang3.time.DateUtils;
|
||||
import org.hibernate.SessionFactory;
|
||||
|
||||
import com.commafeed.CommaFeedConfiguration;
|
||||
import com.commafeed.backend.dao.UnitOfWork;
|
||||
import com.commafeed.backend.dao.UserDAO;
|
||||
import com.commafeed.backend.model.User;
|
||||
import com.commafeed.backend.service.FeedSubscriptionService;
|
||||
@@ -20,6 +22,7 @@ public class PostLoginActivities {
|
||||
|
||||
private final UserDAO userDAO;
|
||||
private final FeedSubscriptionService feedSubscriptionService;
|
||||
private final SessionFactory sessionFactory;
|
||||
private final CommaFeedConfiguration config;
|
||||
|
||||
public void executeFor(User user) {
|
||||
@@ -27,19 +30,26 @@ public class PostLoginActivities {
|
||||
Date now = new Date();
|
||||
|
||||
boolean saveUser = false;
|
||||
|
||||
// only update lastLogin field every hour in order to not
|
||||
// invalidate the cache every time someone logs in
|
||||
if (lastLogin == null || lastLogin.before(DateUtils.addHours(now, -1))) {
|
||||
user.setLastLogin(now);
|
||||
saveUser = true;
|
||||
}
|
||||
if (config.getApplicationSettings().getHeavyLoad() && user.shouldRefreshFeedsAt(now)) {
|
||||
|
||||
if (Boolean.TRUE.equals(config.getApplicationSettings().getHeavyLoad()) && user.shouldRefreshFeedsAt(now)) {
|
||||
feedSubscriptionService.refreshAll(user);
|
||||
user.setLastFullRefresh(now);
|
||||
saveUser = true;
|
||||
}
|
||||
|
||||
if (saveUser) {
|
||||
userDAO.saveOrUpdate(user);
|
||||
// Post login activites are susceptible to run for any webservice call.
|
||||
// We update the user in a new transaction to update the user immediately.
|
||||
// If we didn't and the webservice call takes time, subsequent webservice calls would have to wait for the first call to
|
||||
// finish even if they didn't use the same database tables, because they updated the user too.
|
||||
UnitOfWork.run(sessionFactory, () -> userDAO.saveOrUpdate(user));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user