Skip to content

Commit

Permalink
Merge pull request sakaiproject#1185 from ern/KNL-1386
Browse files Browse the repository at this point in the history
KNL-1386 MessageBundleService fix for Spring + Hibernate Thread issue
  • Loading branch information
jonespm committed Oct 19, 2015
2 parents e463ed9 + 3a2b4d7 commit d2c0020
Showing 1 changed file with 40 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
import org.hibernate.type.Type;
import org.springframework.beans.BeanUtils;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.SessionHolder;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import java.sql.SQLException;
import java.util.*;
Expand Down Expand Up @@ -70,7 +72,7 @@ public class MessageBundleServiceImpl extends HibernateDaoSupport implements Mes
/**
* Queue of method invocations to save or update message bundle data
*/
private List queue = Collections.synchronizedList(new ArrayList());
private List<SaveOrUpdateCall> queue = Collections.synchronizedList(new ArrayList<>());

public void init() {
timer.schedule(new SaveOrUpdateTask(), 0, scheduleDelay);
Expand Down Expand Up @@ -125,7 +127,7 @@ public int getSearchCount(String searchQuery, String module, String baseName, St
}
Integer count = null;
try {
Query query = getSession().createQuery(queryString.toString());
Query query = getSessionFactory().getCurrentSession().createQuery(queryString.toString());
query.setParameters(values.toArray(), (Type[]) types.toArray(new Type[types.size()]));
count= (Integer) query.uniqueResult();
} catch (HibernateException e) {
Expand Down Expand Up @@ -278,12 +280,8 @@ public void updateMessageBundleProperty(MessageBundleProperty mbp) {
if (mbp == null) return;
if (mbp.getDefaultValue() == null) {
mbp.setDefaultValue("");
}
try {
getHibernateTemplate().saveOrUpdate(mbp);
} catch (Exception e) {
logger.warn("Cound not save MessageBundleProperty " + mbp + ", " + e.getMessage(), e);
}
getHibernateTemplate().saveOrUpdate(mbp);
}

public void deleteMessageBundleProperty(MessageBundleProperty mbp) {
Expand Down Expand Up @@ -351,7 +349,7 @@ public List<MessageBundleProperty> getAllProperties(String locale, String module
return getHibernateTemplate().find("from MessageBundleProperty where moduleName = ?", module);
} else {
return getHibernateTemplate().find("from MessageBundleProperty where locale = ? and moduleName = ?",
new String[]{locale, module});
new Object[]{locale, module});
}
}

Expand Down Expand Up @@ -396,7 +394,7 @@ public int importProperties(List<MessageBundleProperty> properties) {
@SuppressWarnings("unchecked")
public List<String> getAllModuleNames() {
List<String> retValue = getHibernateTemplate().find("select distinct(moduleName) from MessageBundleProperty order by moduleName");
if (retValue == null) return new ArrayList();
if (retValue == null) return new ArrayList<>();
//force deep load
retValue.size();
return retValue;
Expand All @@ -405,7 +403,7 @@ public List<String> getAllModuleNames() {
@SuppressWarnings("unchecked")
public List<String> getAllBaseNames() {
List<String> retValue = getHibernateTemplate().find("select distinct(baseName) from MessageBundleProperty order by baseName");
if (retValue == null) return new ArrayList();
if (retValue == null) return new ArrayList<>();
//force deep load
retValue.size();
return retValue;
Expand All @@ -425,7 +423,7 @@ public void revert(MessageBundleProperty mbp) {
protected int executeCountQuery(String query) {
Integer count = null;
try {
count = (Integer) getSession().createQuery(query).uniqueResult();
count = (Integer) getSessionFactory().getCurrentSession().createQuery(query).uniqueResult();
} catch (HibernateException e) {
throw new RuntimeException(e.getMessage(),e);
}
Expand Down Expand Up @@ -454,7 +452,7 @@ public List<MessageBundleProperty> getModifiedProperties(int sortOrder, int sort
org.hibernate.Query query = null;
String queryString = "from MessageBundleProperty where value != null order by " + sortFieldName + " " + orderBy;
try {
query = getSession().createQuery(queryString);
query = getSessionFactory().getCurrentSession().createQuery(queryString);
query.setFirstResult(startingIndex);
query.setMaxResults(pageSize);
return query.list();
Expand Down Expand Up @@ -511,18 +509,36 @@ public SaveOrUpdateTask(){
/**
* step through queue and call the real saveOrUpdateInternal method to do the work
*/
public void run() {
List queueList = new ArrayList(queue);
for (Iterator i = queueList.iterator(); i.hasNext(); ) {
SaveOrUpdateCall call = (SaveOrUpdateCall) i.next();
try {
saveOrUpdateInternal(call.baseName, call.moduleName, call.bundleData, call.loc);
} catch (Throwable e) {
logger.error("problem saving bundle data:", e);
} finally {
queue.remove(call);
}
}
public void run() {
List<SaveOrUpdateCall> queueList = new ArrayList<>(queue);
Session session = null;
try {
// Since we are in a thread that doesn't have a hibernate session
// we need to manage it here
session = getSessionFactory().openSession();
TransactionSynchronizationManager.bindResource(getSessionFactory(), new SessionHolder(session));

for (SaveOrUpdateCall call : queueList) {
try {
session.beginTransaction();
saveOrUpdateInternal(call.baseName, call.moduleName, call.bundleData, call.loc);
} catch (Throwable e) {
logger.error("problem saving bundle data:", e);
session.getTransaction().rollback();
} finally {
if (!session.getTransaction().wasRolledBack()) {
session.flush();
session.getTransaction().commit();
}
queue.remove(call);
}
}
} finally {
if (session != null) {
session.close();
}
TransactionSynchronizationManager.unbindResource(getSessionFactory());
}
}
}
}
Expand Down

0 comments on commit d2c0020

Please sign in to comment.