Skip to content
Snippets Groups Projects
Commit 56db6a7a authored by srosse's avatar srosse
Browse files

OO-634: hardened publisher update to the cost of a query

parent 463c97a5
No related branches found
No related tags found
No related merge requests found
......@@ -594,42 +594,45 @@ public class NotificationsManagerImpl extends NotificationsManager implements Us
*/
@Override
public Publisher getPublisher(SubscriptionContext subsContext) {
return getPublisher(subsContext.getResName(), subsContext.getResId(), subsContext.getSubidentifier(), false);
}
private Publisher getPublisherForUpdate(SubscriptionContext subsContext) {
return getPublisher(subsContext.getResName(), subsContext.getResId(), subsContext.getSubidentifier(), true);
}
@Override
public List<Publisher> getAllPublisher() {
String q = "select pub from org.olat.notifications.PublisherImpl pub";
return DBFactory.getInstance().getCurrentEntityManager().createQuery(q, Publisher.class)
.getResultList();
}
/**
* return the publisher for the given composite primary key ores +
* subidentifier.
*/
private Publisher getPublisher(String resName, Long resId, String subidentifier, boolean forUpdate) {
StringBuilder q = new StringBuilder();
q.append("select pub from ").append(PublisherImpl.class.getName()).append(" pub ")
.append(" where pub.resName=:resName and pub.resId = :resId and pub.subidentifier = :subidentifier");
.append(" where pub.resName=:resName and pub.resId = :resId");
if(StringHelper.containsNonWhitespace(subsContext.getSubidentifier())) {
q.append(" and pub.subidentifier=:subidentifier");
} else {
q.append(" and (pub.subidentifier='' or pub.subidentifier is null)");
}
TypedQuery<Publisher> query = DBFactory.getInstance().getCurrentEntityManager()
.createQuery(q.toString(), Publisher.class)
.setParameter("resName", resName)
.setParameter("resId", resId.longValue())
.setParameter("subidentifier", subidentifier);
if(forUpdate) {
query.setLockMode(LockModeType.PESSIMISTIC_WRITE);
.setParameter("resName", subsContext.getResName())
.setParameter("resId", subsContext.getResId());
if(StringHelper.containsNonWhitespace(subsContext.getSubidentifier())) {
query.setParameter("subidentifier", subsContext.getSubidentifier());
}
List<Publisher> res = query.getResultList();
if (res.isEmpty()) return null;
if (res.size() != 1) throw new AssertException("only one subscriber per person and publisher!!");
return res.get(0);
}
private Publisher getPublisherForUpdate(SubscriptionContext subsContext) {
Publisher pub = getPublisher(subsContext);
if(pub != null && pub.getKey() != null) {
//prevent optimistic lock issue
DBFactory.getInstance().getCurrentEntityManager().detach(pub);
pub = DBFactory.getInstance().getCurrentEntityManager().find(PublisherImpl.class, pub.getKey(), LockModeType.PESSIMISTIC_WRITE);
}
return pub;
}
@Override
public List<Publisher> getAllPublisher() {
String q = "select pub from org.olat.notifications.PublisherImpl pub";
return DBFactory.getInstance().getCurrentEntityManager().createQuery(q, Publisher.class)
.getResultList();
}
/**
* @param resName
......@@ -838,7 +841,7 @@ public class NotificationsManagerImpl extends NotificationsManager implements Us
// to make sure: ignore if no subscriptionContext
if (subscriptionContext == null) return;
Publisher toUpdate = getPublisher(subscriptionContext.getResName(), subscriptionContext.getResId(), subscriptionContext.getSubidentifier(), true);
Publisher toUpdate = getPublisherForUpdate(subscriptionContext);
if(toUpdate == null) {
return;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment