diff src/main/java/de/mpiwg/gazetteer/utils/DBService.java @ 88:f4242db6206b

Refactoring : replace getCurrentSession with openSession for nested transaction exception
author Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
date Wed, 21 Jun 2017 05:56:02 +0200
parents 110be241ff54
children 090035f79373
line wrap: on
line diff
--- 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<LGFullTextSearchFile> list = new ArrayList<LGFullTextSearchFile>();
-
-		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<LGTopic> getAllLGTopicFromDB(){
 		List<LGTopic> 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;