Mercurial > hg > LGServices
comparison 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 |
comparison
equal
deleted
inserted
replaced
87:910cfd8521dd | 88:f4242db6206b |
---|---|
13 import java.util.Map; | 13 import java.util.Map; |
14 | 14 |
15 import org.apache.log4j.Logger; | 15 import org.apache.log4j.Logger; |
16 import org.hibernate.Query; | 16 import org.hibernate.Query; |
17 import org.hibernate.Session; | 17 import org.hibernate.Session; |
18 import org.hibernate.Transaction; | |
18 | 19 |
19 import de.mpiwg.gazetteer.bo.DBEntry; | 20 import de.mpiwg.gazetteer.bo.DBEntry; |
20 import de.mpiwg.gazetteer.bo.LGBranch; | 21 import de.mpiwg.gazetteer.bo.LGBranch; |
21 import de.mpiwg.gazetteer.bo.LGFile; | 22 import de.mpiwg.gazetteer.bo.LGFile; |
22 import de.mpiwg.gazetteer.bo.LGFullTextSearchFile; | 23 import de.mpiwg.gazetteer.bo.LGFullTextSearchFile; |
852 */ | 853 */ |
853 protected static int deleteBranchFromDB(Long branchId){ | 854 protected static int deleteBranchFromDB(Long branchId){ |
854 logger.info("Deleting Branch by branchId=" + branchId); | 855 logger.info("Deleting Branch by branchId=" + branchId); |
855 | 856 |
856 int modifiedFiles; | 857 int modifiedFiles; |
857 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); | 858 Session session = HibernateUtil.getSessionFactory().openSession(); |
858 session.getTransaction().begin(); | 859 Transaction tx = null; |
859 | 860 |
860 Query query = session.createQuery("delete LGBranch where id = :id"); | 861 try{ |
861 query.setLong("id", branchId); | 862 |
862 modifiedFiles = query.executeUpdate(); | 863 tx = session.beginTransaction(); |
863 | 864 |
864 Query query0 = session.createQuery("delete LGFile where branchId = :branchId"); | 865 Query query = session.createQuery("delete LGBranch where id = :id"); |
865 query0.setLong("branchId", branchId); | 866 query.setLong("id", branchId); |
866 modifiedFiles += query0.executeUpdate(); | 867 modifiedFiles = query.executeUpdate(); |
867 | 868 |
868 session.getTransaction().commit(); | 869 Query query0 = session.createQuery("delete LGFile where branchId = :branchId"); |
870 query0.setLong("branchId", branchId); | |
871 modifiedFiles += query0.executeUpdate(); | |
872 | |
873 tx.commit(); | |
874 | |
875 }catch (Exception e) { | |
876 | |
877 if (tx!=null) tx.rollback(); | |
878 | |
879 e.printStackTrace(); | |
880 | |
881 throw e; | |
882 | |
883 }finally { | |
884 session.close(); | |
885 } | |
869 | 886 |
870 return modifiedFiles; | 887 return modifiedFiles; |
871 } | 888 } |
872 | 889 |
873 protected static int deleteFullTextSearchFileFromDB(Long fileId){ | 890 protected static int deleteFullTextSearchFileFromDB(Long fileId){ |
874 int modifiedFiles = 0; | 891 int modifiedFiles = 0; |
875 | 892 |
876 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); | 893 Session session = HibernateUtil.getSessionFactory().openSession(); |
877 session.getTransaction().begin(); | 894 Transaction tx = null; |
878 | 895 |
879 Query query0 = session.createQuery("delete LGFullTextSearchFile where id = :fileId"); | 896 try{ |
880 query0.setLong("fileId", fileId); | 897 |
881 modifiedFiles = query0.executeUpdate(); | 898 tx = session.beginTransaction(); |
882 | 899 |
883 session.getTransaction().commit(); | 900 Query query0 = session.createQuery("delete LGFullTextSearchFile where id = :fileId"); |
901 query0.setLong("fileId", fileId); | |
902 modifiedFiles = query0.executeUpdate(); | |
903 | |
904 tx.commit(); | |
905 }catch (Exception e) { | |
906 if (tx!=null) tx.rollback(); | |
907 | |
908 e.printStackTrace(); | |
909 | |
910 throw e; | |
911 }finally { | |
912 session.close(); | |
913 } | |
884 | 914 |
885 return modifiedFiles; | 915 return modifiedFiles; |
886 } | 916 } |
887 | 917 |
888 protected static int deleteFileFromDB(Long fileId){ | 918 protected static int deleteFileFromDB(Long fileId){ |
889 | 919 |
890 int modifiedFiles; | 920 int modifiedFiles; |
891 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); | 921 Session session = HibernateUtil.getSessionFactory().openSession(); |
892 session.getTransaction().begin(); | 922 Transaction tx = null; |
893 | 923 try{ |
894 | 924 tx = session.beginTransaction(); |
895 Query query0 = session.createQuery("delete LGFile where id = :fileId"); | 925 |
896 query0.setLong("fileId", fileId); | 926 Query query0 = session.createQuery("delete LGFile where id = :fileId"); |
897 modifiedFiles = query0.executeUpdate(); | 927 query0.setLong("fileId", fileId); |
898 | 928 modifiedFiles = query0.executeUpdate(); |
899 session.getTransaction().commit(); | 929 |
930 tx.commit(); | |
931 }catch (Exception e) { | |
932 if (tx!=null) tx.rollback(); | |
933 | |
934 e.printStackTrace(); | |
935 | |
936 throw e; | |
937 }finally { | |
938 session.close(); | |
939 } | |
900 | 940 |
901 return modifiedFiles; | 941 return modifiedFiles; |
902 | 942 |
903 } | 943 } |
904 | 944 |
1036 return list; | 1076 return list; |
1037 } | 1077 } |
1038 | 1078 |
1039 protected static void saveDBEntry(DBEntry entry, Date date){ | 1079 protected static void saveDBEntry(DBEntry entry, Date date){ |
1040 | 1080 |
1041 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); | 1081 Session session = HibernateUtil.getSessionFactory().openSession(); |
1042 session.getTransaction().begin(); | 1082 Transaction tx = null; |
1043 | 1083 try{ |
1044 saveDBEntry0(session, entry, date); | 1084 tx = session.beginTransaction(); |
1045 | 1085 |
1046 session.getTransaction().commit(); | 1086 saveDBEntry0(session, entry, date); |
1087 | |
1088 tx.commit(); | |
1089 }catch (Exception e) { | |
1090 if (tx!=null) tx.rollback(); | |
1091 | |
1092 e.printStackTrace(); | |
1093 | |
1094 throw e; | |
1095 }finally { | |
1096 session.close(); | |
1097 } | |
1047 } | 1098 } |
1048 | 1099 |
1049 public static void saveDBEntry0(Session session, DBEntry entry, Date date){ | 1100 public static void saveDBEntry0(Session session, DBEntry entry, Date date){ |
1050 entry.setLastChangeDate(date); | 1101 entry.setLastChangeDate(date); |
1051 if (entry.isPersistent()) { | 1102 if (entry.isPersistent()) { |
1144 | 1195 |
1145 | 1196 |
1146 } | 1197 } |
1147 | 1198 |
1148 | 1199 |
1149 // remove it | |
1150 /* | |
1151 public static LGFullTextSearchFile getExistFullTextSearchFile(Long userId, String fileName) { | |
1152 //logger.info("getExistFullTextSearchFile: (userId,fileName)=" + userId + ","+fileName); | |
1153 List<LGFullTextSearchFile> list = new ArrayList<LGFullTextSearchFile>(); | |
1154 | |
1155 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); | |
1156 session.getTransaction().begin(); | |
1157 Query query = session.createQuery("from LGFullTextSearchFile where userId = :userId and fileName = :fileName"); | |
1158 query.setLong("userId", userId); | |
1159 query.setString("fileName", fileName); | |
1160 | |
1161 list = query.list(); | |
1162 session.getTransaction().commit(); | |
1163 | |
1164 if (list.size() != 0) { | |
1165 //logger.info("existing record."); | |
1166 return list.get(0); | |
1167 } else { | |
1168 //logger.info("new record."); | |
1169 return null; | |
1170 } | |
1171 | |
1172 } | |
1173 */ | |
1174 | |
1175 | |
1176 | |
1177 | |
1178 /* --- topic --- */ | 1200 /* --- topic --- */ |
1179 protected static List<LGTopic> getAllLGTopicFromDB(){ | 1201 protected static List<LGTopic> getAllLGTopicFromDB(){ |
1180 List<LGTopic> list = null; | 1202 List<LGTopic> list = null; |
1181 | 1203 |
1182 Session session = HibernateUtil.getSessionFactory().openSession(); | 1204 Session session = HibernateUtil.getSessionFactory().openSession(); |
1201 | 1223 |
1202 return list; | 1224 return list; |
1203 } | 1225 } |
1204 | 1226 |
1205 protected static int deleteTopicFromDB(Long topicId){ | 1227 protected static int deleteTopicFromDB(Long topicId){ |
1228 | |
1206 logger.info("Deleting topic by topicId=" + topicId); | 1229 logger.info("Deleting topic by topicId=" + topicId); |
1207 | 1230 |
1208 int modifiedTopic; | 1231 int modifiedTopic; |
1209 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); | 1232 |
1210 | 1233 Session session = HibernateUtil.getSessionFactory().openSession(); |
1211 session.getTransaction().begin(); | 1234 |
1212 | 1235 Transaction tx = null; |
1213 // delete record in Topic table | 1236 try{ |
1214 Query query = session.createQuery("delete LGTopic where id = :id"); | 1237 tx = session.beginTransaction(); |
1215 query.setLong("id", topicId); | 1238 // delete record in Topic table |
1216 modifiedTopic = query.executeUpdate(); | 1239 Query query = session.createQuery("delete LGTopic where id = :id"); |
1217 | 1240 query.setLong("id", topicId); |
1218 | 1241 modifiedTopic = query.executeUpdate(); |
1219 // delete records in TopicSectionRelation table | 1242 |
1220 Query query0 = session.createQuery("delete LGTopicSectionRelation where topicId = :topicId"); | 1243 |
1221 query0.setLong("topicId", topicId); | 1244 // delete records in TopicSectionRelation table |
1222 modifiedTopic += query0.executeUpdate(); | 1245 Query query0 = session.createQuery("delete LGTopicSectionRelation where topicId = :topicId"); |
1223 | 1246 query0.setLong("topicId", topicId); |
1224 // delete records in TopicTagRelation table | 1247 modifiedTopic += query0.executeUpdate(); |
1225 Query query1 = session.createQuery("delete LGTopicTagRelation where topicId = :topicId"); | 1248 |
1226 query1.setLong("topicId", topicId); | 1249 // delete records in TopicTagRelation table |
1227 modifiedTopic += query1.executeUpdate(); | 1250 Query query1 = session.createQuery("delete LGTopicTagRelation where topicId = :topicId"); |
1228 | 1251 query1.setLong("topicId", topicId); |
1229 | 1252 modifiedTopic += query1.executeUpdate(); |
1230 session.getTransaction().commit(); | 1253 |
1254 tx.commit(); | |
1255 }catch (Exception e) { | |
1256 if (tx!=null) tx.rollback(); | |
1257 | |
1258 e.printStackTrace(); | |
1259 | |
1260 throw e; | |
1261 }finally { | |
1262 session.close(); | |
1263 } | |
1231 | 1264 |
1232 return modifiedTopic; | 1265 return modifiedTopic; |
1233 } | 1266 } |
1234 | 1267 |
1235 | 1268 |
1261 } | 1294 } |
1262 | 1295 |
1263 protected static int deleteTopicSectionRelationFromDB(Long relationId){ | 1296 protected static int deleteTopicSectionRelationFromDB(Long relationId){ |
1264 | 1297 |
1265 int modifiedRelation; | 1298 int modifiedRelation; |
1266 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); | 1299 Session session = HibernateUtil.getSessionFactory().openSession(); |
1267 session.getTransaction().begin(); | 1300 Transaction tx = null; |
1268 | 1301 try{ |
1269 | 1302 tx = session.beginTransaction(); |
1270 Query query0 = session.createQuery("delete LGTopicSectionRelation where id = :relationId"); | 1303 |
1271 query0.setLong("relationId", relationId); | 1304 |
1272 modifiedRelation = query0.executeUpdate(); | 1305 Query query0 = session.createQuery("delete LGTopicSectionRelation where id = :relationId"); |
1273 | 1306 query0.setLong("relationId", relationId); |
1274 session.getTransaction().commit(); | 1307 modifiedRelation = query0.executeUpdate(); |
1308 | |
1309 tx.commit(); | |
1310 }catch (Exception e) { | |
1311 if (tx!=null) tx.rollback(); | |
1312 | |
1313 e.printStackTrace(); | |
1314 | |
1315 throw e; | |
1316 }finally { | |
1317 session.close(); | |
1318 } | |
1275 | 1319 |
1276 return modifiedRelation; | 1320 return modifiedRelation; |
1277 | 1321 |
1278 } | 1322 } |
1279 | 1323 |