# HG changeset patch # User Calvin Yeh # Date 1498017362 -7200 # Node ID f4242db6206bb27b422250570477e7c07b7d1c92 # Parent 910cfd8521ddff1ee07ce3b849b1493012cb0092 Refactoring : replace getCurrentSession with openSession for nested transaction exception diff -r 910cfd8521dd -r f4242db6206b src/main/java/de/mpiwg/gazetteer/utils/DBService.java --- a/src/main/java/de/mpiwg/gazetteer/utils/DBService.java Fri May 19 20:12:34 2017 +0200 +++ b/src/main/java/de/mpiwg/gazetteer/utils/DBService.java Wed Jun 21 05:56:02 2017 +0200 @@ -15,6 +15,7 @@ import org.apache.log4j.Logger; import org.hibernate.Query; import org.hibernate.Session; +import org.hibernate.Transaction; import de.mpiwg.gazetteer.bo.DBEntry; import de.mpiwg.gazetteer.bo.LGBranch; @@ -854,18 +855,34 @@ logger.info("Deleting Branch by branchId=" + branchId); int modifiedFiles; - Session session = HibernateUtil.getSessionFactory().getCurrentSession(); - session.getTransaction().begin(); + Session session = HibernateUtil.getSessionFactory().openSession(); + Transaction tx = null; + + try{ + + tx = session.beginTransaction(); + + Query query = session.createQuery("delete LGBranch where id = :id"); + query.setLong("id", branchId); + modifiedFiles = query.executeUpdate(); - Query query = session.createQuery("delete LGBranch where id = :id"); - query.setLong("id", branchId); - modifiedFiles = query.executeUpdate(); + Query query0 = session.createQuery("delete LGFile where branchId = :branchId"); + query0.setLong("branchId", branchId); + modifiedFiles += query0.executeUpdate(); + + tx.commit(); + + }catch (Exception e) { - Query query0 = session.createQuery("delete LGFile where branchId = :branchId"); - query0.setLong("branchId", branchId); - modifiedFiles += query0.executeUpdate(); + if (tx!=null) tx.rollback(); + + e.printStackTrace(); - session.getTransaction().commit(); + throw e; + + }finally { + session.close(); + } return modifiedFiles; } @@ -873,14 +890,27 @@ protected static int deleteFullTextSearchFileFromDB(Long fileId){ int modifiedFiles = 0; - Session session = HibernateUtil.getSessionFactory().getCurrentSession(); - session.getTransaction().begin(); + Session session = HibernateUtil.getSessionFactory().openSession(); + Transaction tx = null; + + try{ + + tx = session.beginTransaction(); + + Query query0 = session.createQuery("delete LGFullTextSearchFile where id = :fileId"); + query0.setLong("fileId", fileId); + modifiedFiles = query0.executeUpdate(); - Query query0 = session.createQuery("delete LGFullTextSearchFile where id = :fileId"); - query0.setLong("fileId", fileId); - modifiedFiles = query0.executeUpdate(); + tx.commit(); + }catch (Exception e) { + if (tx!=null) tx.rollback(); - session.getTransaction().commit(); + e.printStackTrace(); + + throw e; + }finally { + session.close(); + } return modifiedFiles; } @@ -888,15 +918,25 @@ protected static int deleteFileFromDB(Long fileId){ int modifiedFiles; - Session session = HibernateUtil.getSessionFactory().getCurrentSession(); - session.getTransaction().begin(); + Session session = HibernateUtil.getSessionFactory().openSession(); + Transaction tx = null; + try{ + tx = session.beginTransaction(); + Query query0 = session.createQuery("delete LGFile where id = :fileId"); + query0.setLong("fileId", fileId); + modifiedFiles = query0.executeUpdate(); - Query query0 = session.createQuery("delete LGFile where id = :fileId"); - query0.setLong("fileId", fileId); - modifiedFiles = query0.executeUpdate(); + tx.commit(); + }catch (Exception e) { + if (tx!=null) tx.rollback(); - session.getTransaction().commit(); + e.printStackTrace(); + + throw e; + }finally { + session.close(); + } return modifiedFiles; @@ -1038,12 +1078,23 @@ protected static void saveDBEntry(DBEntry entry, Date date){ - Session session = HibernateUtil.getSessionFactory().getCurrentSession(); - session.getTransaction().begin(); + Session session = HibernateUtil.getSessionFactory().openSession(); + Transaction tx = null; + try{ + tx = session.beginTransaction(); + + saveDBEntry0(session, entry, date); - saveDBEntry0(session, entry, date); + tx.commit(); + }catch (Exception e) { + if (tx!=null) tx.rollback(); - session.getTransaction().commit(); + e.printStackTrace(); + + throw e; + }finally { + session.close(); + } } public static void saveDBEntry0(Session session, DBEntry entry, Date date){ @@ -1146,35 +1197,6 @@ } - // remove it - /* - public static LGFullTextSearchFile getExistFullTextSearchFile(Long userId, String fileName) { - //logger.info("getExistFullTextSearchFile: (userId,fileName)=" + userId + ","+fileName); - List list = new ArrayList(); - - Session session = HibernateUtil.getSessionFactory().getCurrentSession(); - session.getTransaction().begin(); - Query query = session.createQuery("from LGFullTextSearchFile where userId = :userId and fileName = :fileName"); - query.setLong("userId", userId); - query.setString("fileName", fileName); - - list = query.list(); - session.getTransaction().commit(); - - if (list.size() != 0) { - //logger.info("existing record."); - return list.get(0); - } else { - //logger.info("new record."); - return null; - } - - } - */ - - - - /* --- topic --- */ protected static List getAllLGTopicFromDB(){ List list = null; @@ -1203,31 +1225,42 @@ } protected static int deleteTopicFromDB(Long topicId){ + logger.info("Deleting topic by topicId=" + topicId); int modifiedTopic; - Session session = HibernateUtil.getSessionFactory().getCurrentSession(); - session.getTransaction().begin(); + Session session = HibernateUtil.getSessionFactory().openSession(); - // delete record in Topic table - Query query = session.createQuery("delete LGTopic where id = :id"); - query.setLong("id", topicId); - modifiedTopic = query.executeUpdate(); + Transaction tx = null; + try{ + tx = session.beginTransaction(); + // delete record in Topic table + Query query = session.createQuery("delete LGTopic where id = :id"); + query.setLong("id", topicId); + modifiedTopic = query.executeUpdate(); - // delete records in TopicSectionRelation table - Query query0 = session.createQuery("delete LGTopicSectionRelation where topicId = :topicId"); - query0.setLong("topicId", topicId); - modifiedTopic += query0.executeUpdate(); + // delete records in TopicSectionRelation table + Query query0 = session.createQuery("delete LGTopicSectionRelation where topicId = :topicId"); + query0.setLong("topicId", topicId); + modifiedTopic += query0.executeUpdate(); + + // delete records in TopicTagRelation table + Query query1 = session.createQuery("delete LGTopicTagRelation where topicId = :topicId"); + query1.setLong("topicId", topicId); + modifiedTopic += query1.executeUpdate(); - // delete records in TopicTagRelation table - Query query1 = session.createQuery("delete LGTopicTagRelation where topicId = :topicId"); - query1.setLong("topicId", topicId); - modifiedTopic += query1.executeUpdate(); + tx.commit(); + }catch (Exception e) { + if (tx!=null) tx.rollback(); + e.printStackTrace(); - session.getTransaction().commit(); + throw e; + }finally { + session.close(); + } return modifiedTopic; } @@ -1263,15 +1296,26 @@ protected static int deleteTopicSectionRelationFromDB(Long relationId){ int modifiedRelation; - Session session = HibernateUtil.getSessionFactory().getCurrentSession(); - session.getTransaction().begin(); + Session session = HibernateUtil.getSessionFactory().openSession(); + Transaction tx = null; + try{ + tx = session.beginTransaction(); - Query query0 = session.createQuery("delete LGTopicSectionRelation where id = :relationId"); - query0.setLong("relationId", relationId); - modifiedRelation = query0.executeUpdate(); + Query query0 = session.createQuery("delete LGTopicSectionRelation where id = :relationId"); + query0.setLong("relationId", relationId); + modifiedRelation = query0.executeUpdate(); - session.getTransaction().commit(); + tx.commit(); + }catch (Exception e) { + if (tx!=null) tx.rollback(); + + e.printStackTrace(); + + throw e; + }finally { + session.close(); + } return modifiedRelation; diff -r 910cfd8521dd -r f4242db6206b src/main/java/de/mpiwg/gazetteer/utils/DataProvider.java --- a/src/main/java/de/mpiwg/gazetteer/utils/DataProvider.java Fri May 19 20:12:34 2017 +0200 +++ b/src/main/java/de/mpiwg/gazetteer/utils/DataProvider.java Wed Jun 21 05:56:02 2017 +0200 @@ -9,6 +9,7 @@ import org.apache.log4j.Logger; import org.hibernate.Session; +import org.hibernate.Transaction; import cl.maps.duplex.DuplexKey; import de.mpiwg.gazetteer.bo.LGBranch; @@ -24,39 +25,39 @@ import de.mpiwg.gazetteer.utils.exceptions.VersioningException; public class DataProvider extends AbstractDataProvider{ - + private static Logger logger = Logger.getLogger(DataProvider.class); private static DataProvider instance; - + public static DataProvider getInstance(){ if(instance == null) instance = new DataProvider(); return instance; } - + public static void resetInstance() { instance = null; } - + public DataProvider(){ logger.info("##### Starting DataProvider #####"); } - - + + /* --- branch --- */ - + public LGFile getFile(Long fileId){ return getFileMap().getValuesByOwnKey(fileId); } - + public List getAllFiles(Long branchId){ List list = getFileMap().getValuesByAKey(branchId); Collections.sort(list); Collections.reverse(list); return list; } - + public List getAllExistingTopicSectionRelation() { List list = new ArrayList(); for (LGTopicSectionRelation relation : getTopicSectionRelationMap().values()) { @@ -64,15 +65,15 @@ } return list; } - + public List getAllExistingBranches(){ List list = new ArrayList(); - for(LGBranch branch : getBranchMap().values()){ + for(LGBranch branch : getBranchMap().values()){ list.add(branch); } return list; } - + public List getBranches(Long userId){ List list = new ArrayList(); for(LGBranch branch : getBranchMap().values()){ @@ -82,49 +83,49 @@ } return list; } - - + + public LGBranch getBranch(Long branchId){ return getBranchMap().getValuesByOwnKey(branchId); } - + public void deleteFile(LGFile file) throws Exception{ - + LGBranch branch = getBranch(file.getBranchId()); - + if(branch == null){ throw new Exception("There is any Branch for " + file); } - + List files = getAllFiles(file.getBranchId()); - + if(files.size() == 1){ throw new Exception("This file could not be deleted, because it is part of a branch that has only one file. A Branch without files is a inconsistency."); } - + // if the file is the last version of a branch, we must replace the current file with the penultimate file. if(branch.getCurrentLastFileId().equals(file.getId())){ LGFile penultimateFile = getPenultimateFile(files); - + penultimateFile.setLastVersion(true); branch.setCurrentLastFileId(penultimateFile.getId()); - + this.updateBranch(branch); this.updateFile(penultimateFile); - + } - + //deleting file from DB and cache int modifiedFiles = DBService.deleteFileFromDB(file.getId()); getFileMap().remove(file.getKey()); - + logger.info(modifiedFiles + " items deleted by removing file " + file); } - + private LGFile getPenultimateFile(List files){ LGFile penultimateFile = null; LGFile lastFile = null; - + for(LGFile file : files){ if(lastFile == null){ lastFile = file; @@ -140,15 +141,15 @@ } } } - + return penultimateFile; } - + public void deleteBranch(LGBranch branch){ - + int modifiedFiles = DBService.deleteBranchFromDB(branch.getId()); List fileToDelete = getFileMap().getValuesByAKey(branch.getId()); - + for(LGFile file : new ArrayList(fileToDelete)){ getFileMap().remove(file.getKey()); } @@ -156,198 +157,234 @@ logger.info(modifiedFiles + " items deleted by removing branch " + branch.toString()); } - + public void updateFile(LGFile file) throws Exception{ if(!file.isPersistent()){ throw new Exception("Trying to update a file that it is not persistent!"); } - + Date date = new Date(); DBService.saveDBEntry(file, date); this.getFileMap().put(file.getKey(), file); } - + public void updateBranch(LGBranch branch) throws Exception{ if(!branch.isPersistent()){ throw new Exception("Trying to update a branch that it is not persistent!"); } - + Date date = new Date(); DBService.saveDBEntry(branch, date); this.getBranchMap().put(branch.getKey(), branch); } - + public LGFile saveFile(Long branchId, String text, Long userId, Long userPreviousFileId) throws Exception{ - - - + Date date = new Date(); - + LGBranch branch = getBranchMap().getValuesByOwnKey(branchId); - + if(!branch.hasContributor(userId)){ throw new NoAuthorizedException(userId, branchId); } - + if(!branch.getCurrentLastFileId().equals(userPreviousFileId)){ LGFile userPreviousFile = getFileMap().getValuesByOwnKey(userPreviousFileId); LGFile currentLastFile = getFileMap().getValuesByOwnKey(branch.getCurrentLastFileId()); - + throw new VersioningException(userPreviousFile, currentLastFile); } - + LGFile previousFile = getFileMap().getValuesByOwnKey(branch.getCurrentLastFileId()); - + LGFile file = new LGFile(); file.setBranchId(branchId); file.setVersion(previousFile.getVersion() + 1); file.setUserId(userId); file.setContent(text); - + //Saving into DB //################################## - Session session = HibernateUtil.getSessionFactory().getCurrentSession(); - session.getTransaction().begin(); - - - file.setBranchId(branch.getId()); - DBService.saveDBEntry0(session, file, date); - - previousFile.setLastVersion(false); - DBService.saveDBEntry0(session, previousFile, date); - - branch.setCurrentLastFileId(file.getId()); - DBService.saveDBEntry0(session, branch, date); - - //Saving physical file in the operating system - String fileName = FileManager.saveFile(branch, file, date, userId); - file.setFileName(fileName); - DBService.saveDBEntry0(session, file, date); - - session.getTransaction().commit(); - //################################## - - - //Saving into Cache - getBranchMap().put(branch.getKey(), branch); - getFileMap().put(file.getKey(), file); - getFileMap().put(previousFile.getKey(), previousFile); - + Session session = HibernateUtil.getSessionFactory().openSession(); + Transaction tx = null; + + try{ + + tx = session.beginTransaction(); + + file.setBranchId(branch.getId()); + DBService.saveDBEntry0(session, file, date); + + previousFile.setLastVersion(false); + DBService.saveDBEntry0(session, previousFile, date); + + branch.setCurrentLastFileId(file.getId()); + DBService.saveDBEntry0(session, branch, date); + + //Saving physical file in the operating system + String fileName = FileManager.saveFile(branch, file, date, userId); + file.setFileName(fileName); + + DBService.saveDBEntry0(session, file, date); + + tx.commit(); + + //################################## + + //Saving into Cache + getBranchMap().put(branch.getKey(), branch); + getFileMap().put(file.getKey(), file); + getFileMap().put(previousFile.getKey(), previousFile); + + }catch (Exception e) { + + if (tx!=null) tx.rollback(); + + e.printStackTrace(); + + throw e; + + }finally { + session.close(); + } + return file; } - + public Long saveNewFile(String text, String label, Long sectionId, Long userId) throws Exception{ - + Date date = new Date(); - - LGBranch branch = new LGBranch(); + + LGBranch branch = new LGBranch(); branch.setSectionId(sectionId); branch.setUserId(userId); branch.setLabel(label); branch.addContributor(userId); branch.loadTransientData(); - + LGFile file = new LGFile(); file.setUserId(userId); file.setContent(text); - + //Saving into DB //################################## - Session session = HibernateUtil.getSessionFactory().getCurrentSession(); - session.getTransaction().begin(); - - DBService.saveDBEntry0(session, branch, date); - file.setBranchId(branch.getId()); - - DBService.saveDBEntry0(session, file, date); - branch.setCurrentLastFileId(file.getId()); + Session session = HibernateUtil.getSessionFactory().openSession(); + Transaction tx = null; + try{ + tx = session.beginTransaction(); + + DBService.saveDBEntry0(session, branch, date); + file.setBranchId(branch.getId()); + + DBService.saveDBEntry0(session, file, date); + branch.setCurrentLastFileId(file.getId()); + + //Saving physical file in the operating system + String fileName = FileManager.saveFile(branch, file, date, userId); + file.setFileName(fileName); + DBService.saveDBEntry0(session, file, date); + + tx.commit(); - //Saving physical file in the operating system - String fileName = FileManager.saveFile(branch, file, date, userId); - file.setFileName(fileName); - DBService.saveDBEntry0(session, file, date); - - session.getTransaction().commit(); + //Saving into Cache + getBranchMap().put(branch.getKey(), branch); + getFileMap().put(file.getKey(), file); + + }catch (Exception e) { + if (tx!=null) tx.rollback(); + + e.printStackTrace(); + + throw e; + }finally { + session.close(); + } //################################## - - - //Saving into Cache - getBranchMap().put(branch.getKey(), branch); - getFileMap().put(file.getKey(), file); - + return branch.getId(); - + } /* --- end branch --- */ - - - + + + /* --- full text search --- */ - + public List getSearchFileList4User(Long userId) { - + List list = new ArrayList(); - + for(LGFullTextSearchFile searchFile : getFullTextSearchFileMap().values()){ if(searchFile.getUserId() == userId){ list.add(searchFile); } } - + return list; } - - - + + + public void deleteLGFullTextSearchFile(LGFullTextSearchFile file) throws IOException{ //deleting file from DB: the record in table FullTextSearchFile int modifiedFiles = DBService.deleteFullTextSearchFileFromDB(file.getId()); - - // delete the files from file system (html in ftsearch-data/... and csv in LGMap/datasets/) + + // delete the files from file system (html in ftsearch-data/... and csv in LGMap/datasets/) FileManager.deleteFullTextSearchFileCsv(file); FileManager.deleteFullTextSearchFileHtml(file); - + // delete file from cache this.getFullTextSearchFileMap().remove(file.getKey()); logger.debug("deleteLGFullTextSearchFile"); } - - public LGFullTextSearchFile saveLGFullTextSearchFile(List list, Long userId, LGFullTextSearchFile searchFile) throws Exception { + + public LGFullTextSearchFile saveLGFullTextSearchFile(List list, Long userId, LGFullTextSearchFile searchFile) throws Exception { // save as csv and html file in filesystem, and records in db `FullTextSearchFile // List list is the filteredList of searching result with isRemoved field - + Date date = new Date(); - + //Saving into DB //################################## - Session session = HibernateUtil.getSessionFactory().getCurrentSession(); - session.getTransaction().begin(); - - DBService.saveDBEntry0(session, searchFile, date); - - //Saving physical file in the operating system - FileManager.saveFullTextSearchFileAsCsv(searchFile, userId, list); // save csv file to LGMap's datasets folder - FileManager.saveFullTextSearchFileAsHtml(searchFile, userId, list); // save html file to LGMap's datasets folder - - session.getTransaction().commit(); + Session session = HibernateUtil.getSessionFactory().openSession(); + Transaction tx = null; + try{ + tx = session.beginTransaction(); + + DBService.saveDBEntry0(session, searchFile, date); + + //Saving physical file in the operating system + FileManager.saveFullTextSearchFileAsCsv(searchFile, userId, list); // save csv file to LGMap's datasets folder + FileManager.saveFullTextSearchFileAsHtml(searchFile, userId, list); // save html file to LGMap's datasets folder + + tx.commit(); + }catch (Exception e) { + if (tx!=null) tx.rollback(); + + e.printStackTrace(); + + throw e; + }finally { + session.close(); + } //################################## - + return searchFile; } public LGFullTextSearchFile getFullTextSearchFile(Long fileId) { LGFullTextSearchFile file = getFullTextSearchFileMap().getValuesByOwnKey(fileId); - + return file; } /* --- end full text search --- */ /* --- topic --- */ - + public List getTopics(Long userId){ List list = new ArrayList(); for(LGTopic topic : getTopicMap().values()){ @@ -356,33 +393,34 @@ list.add(topic); } */ - + // add topic into list anyway, without checking the contributor role. list.add(topic); } - + return list; } - + public LGTopic getTopic(Long topicId){ return getTopicMap().getValuesByOwnKey(topicId); } - - + + public void deleteTopic(LGTopic topic){ - + int modifiedRelation = DBService.deleteTopicFromDB(topic.getId()); getTopicMap().remove(topic.getKey()); - + logger.info("removing " + modifiedRelation + " records in TopicSectionRelation and TopicTagRelation table for the topic " + topic.toString()); - + this.setTopicSectionRelationMap(null); // clear topicSectionRelationMap cache - + } public void createTopic(String nameEn, String nameCh,String namePinyin, String description, Long userId) { + Date date = new Date(); - + LGTopic topic = new LGTopic(); topic.setNameEn(nameEn); topic.setNameCh(nameCh); @@ -390,80 +428,89 @@ topic.setDescription(description); topic.setUserId(userId); topic.setContributors("[" + userId.toString() + "]"); - + // auto-generating a tag for the topic: could be from nameEn replace space with '_' String tag = new String(); tag = nameEn.replace(' ', '_'); topic.setTag(tag); - + //Saving into DB //################################## // For Topic table - Session session = HibernateUtil.getSessionFactory().getCurrentSession(); - session.getTransaction().begin(); - - DBService.saveDBEntry0(session, topic, date); - - // For Taglist table - // create record, which is the topic tag, in taglist table, - // with the taglis.name=nameCh, the taglist.tag=tag, the taglist.color=the defualt topic tag color, which is rgb(255,0,174) - - LGTaglist taglist = new LGTaglist(); - taglist.setName(nameCh); - - // tag to lowercase only!! bcuz it will be used as topic tag in Extraction-interface, and it'll be a tag in HTML which is case-insensitive - taglist.setTag(tag.toLowerCase()); - - taglist.setColor("rgb(255, 0, 174)"); - - DBService.saveDBEntry0(session, taglist, date); - - - logger.debug("new topic tag id=" + taglist.getId()); - - - // For TopicTagRelation table - // link the relation between the new topic and its corresponding tag - LGTopicTagRelation relation = new LGTopicTagRelation(); - relation.setTagId(taglist.getId()); - relation.setTopicId(topic.getId()); - - DBService.saveDBEntry0(session, relation, date); - - - session.getTransaction().commit(); + Session session = HibernateUtil.getSessionFactory().openSession(); + Transaction tx = null; + try{ + tx = session.beginTransaction(); + + DBService.saveDBEntry0(session, topic, date); + + // For Taglist table + // create record, which is the topic tag, in taglist table, + // with the taglis.name=nameCh, the taglist.tag=tag, the taglist.color=the defualt topic tag color, which is rgb(255,0,174) + + LGTaglist taglist = new LGTaglist(); + taglist.setName(nameCh); + + // tag to lowercase only!! bcuz it will be used as topic tag in Extraction-interface, and it'll be a tag in HTML which is case-insensitive + taglist.setTag(tag.toLowerCase()); + + taglist.setColor("rgb(255, 0, 174)"); + + DBService.saveDBEntry0(session, taglist, date); + + logger.debug("new topic tag id=" + taglist.getId()); + + // For TopicTagRelation table + // link the relation between the new topic and its corresponding tag + LGTopicTagRelation relation = new LGTopicTagRelation(); + relation.setTagId(taglist.getId()); + relation.setTopicId(topic.getId()); + + DBService.saveDBEntry0(session, relation, date); + + tx.commit(); + + }catch (Exception e) { + if (tx!=null) tx.rollback(); + + e.printStackTrace(); + + throw e; + }finally { + session.close(); + }; //################################## - + } public void updateTopic(LGTopic topic) throws Exception { if(!topic.isPersistent()){ throw new Exception("Trying to update a topic that it is not persistent!"); } - + Date date = new Date(); DBService.saveDBEntry(topic, date); this.getTopicMap().put(topic.getKey(), topic); - + } - - - + + + public List getAllSectionsInTopic(Long topicId) throws SQLException{ - + List topicSectionRelationList = getTopicSectionRelationMap().getValuesByAKey(topicId); // get sections from topicSecionRelation list List list = new ArrayList(); for (LGTopicSectionRelation aRelation : topicSectionRelationList) { //logger.debug(aRelation.getInfo() ); - + DBSection section = DBService.getSectionWithContent(aRelation.getSectionId()); list.add(section); - + } - - + + Collections.sort(list); Collections.reverse(list); return list; @@ -475,32 +522,32 @@ public void deleteTopicSectionRelation(Long topicId, String bookId) throws Exception{ // delete record with topicId, bookId in topicSectionRelation // this means the sectionId = 0, there is no corresponding section in the book for this topic - + // TODO - + } public void deleteTopicSectionRelation(Long topicId, Long sectionId) throws Exception{ if (sectionId == 0) { throw new Exception("sectionId cannot be 0"); } - + DuplexKey key = new DuplexKey(topicId, sectionId); LGTopicSectionRelation topicSectionRelation = this.getTopicSectionRelationMap().get(key); LGTopic topic = getTopic(topicId); - + if(topic == null){ throw new Exception("There is no topic of " + topicSectionRelation); } - + //deleting topicSectionRelation from DB and cache int modifiedRelation = DBService.deleteTopicSectionRelationFromDB(topicSectionRelation.getId()); getTopicSectionRelationMap().remove(topicSectionRelation.getKey()); - + logger.info(modifiedRelation + " items deleted by removing topicSectionRelation " + topicSectionRelation.toString()); - + } public String updateTopicSectionRelation(Long sectionId, Long topicId) throws Exception { @@ -510,56 +557,62 @@ if (topicSectionRelation != null) { // the relation already existed // would this possibly need any modification to the existing record in the topicSectionRelation table? - + return "updated"; - + } else { createTopicSectionRelation(sectionId, topicId); return "added"; } - - - + + + } - - + + public void createTopicSectionRelation(Long sectionId, Long topicId) throws Exception { - + Date date = new Date(); - + LGTopicSectionRelation topicSectionRelation = new LGTopicSectionRelation(); topicSectionRelation.setTopicId(topicId); topicSectionRelation.setSectionId(sectionId); - + DBService.getInstance(); DBSection section = DBService.getSectionWithContent(sectionId); topicSectionRelation.setBookId(section.getBookId()); - + //Saving into DB //################################## // For Topic table - Session session = HibernateUtil.getSessionFactory().getCurrentSession(); - session.getTransaction().begin(); - - DBService.saveDBEntry0(session, topicSectionRelation, date); - - session.getTransaction().commit(); - //################################## - + Session session = HibernateUtil.getSessionFactory().openSession(); + Transaction tx = null; + try{ + tx = session.beginTransaction(); + DBService.saveDBEntry0(session, topicSectionRelation, date); + tx.commit(); + }catch (Exception e) { + if (tx!=null) tx.rollback(); + e.printStackTrace(); + throw e; + }finally { + session.close(); + } + // update cache this.loadTopicSectionRelations(); - + } public Integer getNumberOfSectionInTopic(Long topicId) { List topicSectionRelationList = getTopicSectionRelationMap().getValuesByAKey(topicId); - + return topicSectionRelationList.size(); } - - + + /* --- end topic --- */ - + }