Skip to content
This repository has been archived by the owner on Sep 12, 2018. It is now read-only.

Commit

Permalink
Suppressed OptimisticLockException
Browse files Browse the repository at this point in the history
Suppressed the exception for updating ProjectVisition, cause it is not that critical operation. After this commit, it logs about it instead of throwing the exception.
  • Loading branch information
Keesun Baik committed Nov 5, 2014
1 parent b5adeeb commit cfc791f
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions app/models/RecentlyVisitedProjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/
package models;

import play.Logger;
import play.db.ebean.Model;
import play.db.ebean.Transactional;

Expand Down Expand Up @@ -48,6 +49,15 @@ public class RecentlyVisitedProjects extends Model {
@JoinColumn(name = "recently_visited_projects_id")
public List<ProjectVisitation> visitedProjects;

/**
* Add new ProjectVisitation to existing RecentlyVisitedProjects or new one.
*
* Updating the existing ProjectVisitation can be failed by OptimisticLockException.
*
* @param user
* @param project
* @return
*/
@Transactional
public static RecentlyVisitedProjects addNewVisitation(User user, Project project) {
RecentlyVisitedProjects existingOne = find.where().eq("user", user).findUnique();
Expand All @@ -68,8 +78,7 @@ public static RecentlyVisitedProjects addNewVisitation(User user, Project projec
private void add(Project project) {
ProjectVisitation existingPV = ProjectVisitation.findBy(this, project);
if(existingPV != null) {
existingPV.visited = new Date();
existingPV.update();
updateExistingPv(existingPV);
} else {
ProjectVisitation newPV = new ProjectVisitation();
newPV.recentlyVisitedProjects = this;
Expand All @@ -79,6 +88,15 @@ private void add(Project project) {
}
}

private void updateExistingPv(ProjectVisitation existingPV) {
try {
existingPV.visited = new Date();
existingPV.update();
} catch (OptimisticLockException e) {
Logger.warn("OptimisticLockException occurred when updating ProjectVisitation: " + existingPV.id);
}
}

public List<ProjectVisitation> findRecentlyVisitedProjects(int size) {
return ProjectVisitation.findRecentlyVisitedProjects(this, size);
}
Expand Down

0 comments on commit cfc791f

Please sign in to comment.