List of usage examples for org.hibernate Session clear
void clear();
From source file:com.krawler.spring.importFunctionality.ImportUtil.java
License:Open Source License
/** * @param filename//from ww w.j a va2 s . c o m * @param sheetNo * @param startindex * @param importDao * @return * @throws ServiceException */ public static void dumpXLSFileData(String filename, int sheetNo, int startindex, ImportDAO importDao, HibernateTransactionManager txnManager) throws ServiceException { boolean commitedEx = false; DefaultTransactionDefinition def = new DefaultTransactionDefinition(); def.setName("import_Tx"); def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); TransactionStatus status = txnManager.getTransaction(def); Session session = txnManager.getSessionFactory().getCurrentSession(); try { String destinationDirectory = storageHandlerImpl.GetDocStorePath() + "xlsfiles"; POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(destinationDirectory + "/" + filename)); HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFSheet sheet = wb.getSheetAt(sheetNo); //DateFormat sdf = new SimpleDateFormat(df_full); int maxRow = sheet.getLastRowNum(); int maxCol = 0; String tableName = importDao.getTableName(filename); int flushCounter = 0; for (int i = startindex; i <= maxRow; i++) { HSSFRow row = sheet.getRow(i); if (row == null) { continue; } if (i == startindex) { maxCol = row.getLastCellNum(); //Column Count } ArrayList<String> dataArray = new ArrayList<String>(); JSONObject dataObj = new JSONObject(); for (int j = 0; j < maxCol; j++) { HSSFCell cell = row.getCell(j); String val = null; if (cell == null) { dataArray.add(val); continue; } String colHeader = new CellReference(i, j).getCellRefParts()[2]; switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_NUMERIC: if (HSSFDateUtil.isCellDateFormatted(cell)) { val = Long.toString(cell.getDateCellValue().getTime()); } else { val = dfmt.format(cell.getNumericCellValue()); } break; case HSSFCell.CELL_TYPE_STRING: val = ImportUtil.cleanHTML(cell.getRichStringCellValue().getString()); break; } dataObj.put(colHeader, val); dataArray.add(val); //Collect row data } //Insert Query if (dataObj.length() > 0) { // Empty row check (if lenght==0 then all columns are empty) importDao.dumpFileRow(tableName, dataArray.toArray()); if (flushCounter % 30 == 0) { session.flush(); session.clear(); } flushCounter++; } } try { txnManager.commit(status); } catch (Exception ex) { commitedEx = true; throw ex; } } catch (IOException ex) { throw ServiceException.FAILURE("dumpXLSFileData: " + ex.getMessage(), ex); } catch (Exception ex) { if (!commitedEx) { //if exception occurs during commit then dont call rollback txnManager.rollback(status); } throw ServiceException.FAILURE("dumpXLSFileData: " + ex.getMessage(), ex); } }
From source file:com.krawler.spring.importFunctionality.ImportUtil.java
License:Open Source License
/** * @param filename//w w w . ja v a 2 s . c om * @param sheetNo * @param startindex * @param importDao * @return * @throws ServiceException */ public static void dumpXLSXFileData(String filename, int sheetNo, int startindex, ImportDAO importDao, HibernateTransactionManager txnManager) throws ServiceException { boolean commitedEx = false; DefaultTransactionDefinition def = new DefaultTransactionDefinition(); def.setName("import_Tx"); def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); TransactionStatus status = txnManager.getTransaction(def); Session session = txnManager.getSessionFactory().getCurrentSession(); try { String destinationDirectory = storageHandlerImpl.GetDocStorePath() + "xlsfiles"; FileInputStream fs = new FileInputStream(destinationDirectory + "/" + filename); XSSFWorkbook wb = new XSSFWorkbook(fs); XSSFSheet sheet = wb.getSheetAt(sheetNo); //DateFormat sdf = new SimpleDateFormat(df_full); int maxRow = sheet.getLastRowNum(); int maxCol = 0; String tableName = importDao.getTableName(filename); int flushCounter = 0; for (int i = startindex; i <= maxRow; i++) { XSSFRow row = sheet.getRow(i); if (row == null) { continue; } if (i == startindex) { maxCol = row.getLastCellNum(); //Column Count } ArrayList<String> dataArray = new ArrayList<String>(); JSONObject dataObj = new JSONObject(); for (int j = 0; j < maxCol; j++) { XSSFCell cell = row.getCell(j); String val = null; if (cell == null) { dataArray.add(val); continue; } String colHeader = new CellReference(i, j).getCellRefParts()[2]; switch (cell.getCellType()) { case XSSFCell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { val = Long.toString(cell.getDateCellValue().getTime()); } else { val = dfmt.format(cell.getNumericCellValue()); } break; case XSSFCell.CELL_TYPE_STRING: val = ImportUtil.cleanHTML(cell.getRichStringCellValue().getString()); break; } dataObj.put(colHeader, val); dataArray.add(val); //Collect row data } //Insert Query if (dataObj.length() > 0) { // Empty row check (if lenght==0 then all columns are empty) importDao.dumpFileRow(tableName, dataArray.toArray()); if (flushCounter % 30 == 0) { session.flush(); session.clear(); } flushCounter++; } } try { txnManager.commit(status); } catch (Exception ex) { commitedEx = true; throw ex; } } catch (IOException ex) { throw ServiceException.FAILURE("dumpXLSXFileData: " + ex.getMessage(), ex); } catch (Exception ex) { if (!commitedEx) { //if exception occurs during commit then dont call rollback txnManager.rollback(status); } throw ServiceException.FAILURE("dumpXLSXFileData: " + ex.getMessage(), ex); } }
From source file:com.leixl.easyframework.lucene.impl.LuceneEMovieDaoImpl.java
License:Open Source License
public Integer index(IndexWriter writer, Date startDate, Date endDate, Integer startId, Integer max) throws CorruptIndexException, IOException { Finder f = Finder.create("select bean from EMovie bean"); f.append(" where 1=1"); if (startId != null) { f.append(" and bean.id > :startId"); f.setParam("startId", startId); }/*w w w . j a va 2 s. c o m*/ if (startDate != null) { f.append(" and bean.createDate >= :startDate"); f.setParam("startDate", startDate); } if (endDate != null) { f.append(" and bean.createDate <= :endDate"); f.setParam("endDate", endDate); } f.append(" order by bean.id asc"); if (max != null) { f.setMaxResults(max); } Session session = getSession(); ScrollableResults movies = f.createQuery(getSession()).setCacheMode(CacheMode.IGNORE) .scroll(ScrollMode.FORWARD_ONLY); int count = 0; EMovie movie = null; while (movies.next()) { movie = (EMovie) movies.get(0); writer.addDocument(LuceneContent.createDocument(movie)); if (++count % 20 == 0) { session.clear(); } } if (count < max || movie == null) { // ???????? return null; } else { // ?ID return movie.getId(); } }
From source file:com.liusoft.dlog4j.dao.FCKUploadFileDAO.java
License:Open Source License
/** * []/* www. ja v a 2 s. c om*/ * * @param siteid * @param obj_id * @return * @throws IOException */ public static boolean deleteFileById(String[] ids, UserBean bean) { if (ids != null && ((bean.getPopedom() == bean.POPEDOM_ALL || bean.getPopedom() == bean.POPEDOM_SUPER || bean.getPopedom() == bean.POPEDOM_OPERATE)) && (bean.getRole() == (bean.ROLE_ADMINISTRATOR))) { Session ssn = getSession(); beginTransaction(); List list = new ArrayList<FckUploadFileBean>(); for (int i = 0; i < ids.length; i++) { FckUploadFileBean fufb = (FckUploadFileBean) namedUniqueResult("GET_UPLOAD_FILE", Integer.parseInt(ids[i])); list.add(fufb); try { FCK_UploadManager.getUploadHandler().remove(fufb); } catch (Exception e) { e.printStackTrace(); rollback(); return false; } ssn.delete(fufb); ssn.flush(); ssn.clear(); } commit(); for (int i = 0; i < list.size(); i++) { try { FCK_UploadManager.getUploadHandler().remove(((FckUploadFileBean) list.get(i))); } catch (Exception e) { e.printStackTrace(); } } return true; } return false; }
From source file:com.liusoft.dlog4j.dao.MessageDAO.java
License:Open Source License
/** * /* w w w . j a v a 2 s . co m*/ * * @param club * @param only_admin * @param content * @param sender */ public static void SendMsgs(UserBean sender, List receivers, String content) { try { Date newDate = new Date(); // Session ssn = getSession(); for (int i = 0; i < receivers.size(); i++) { String nickName = " "; UserBean receiver = (UserBean) receivers.get(i); // FriendBean friendBane = UserDAO.getFriend(receiver.getId(), sender.getId());// (,) nickName = ((UserBean) getBean(UserBean.class, receiver.getId())).getNickname(); // if (!UserDAO.isUserInBlackList(receiver.getId(), sender.getId())) { beginTransaction(); // bean MessageBean msg = new MessageBean(); msg.setContent(content); msg.setSendTime(newDate); msg.setFromUser(sender); msg.setStatus(MessageBean.STATUS_NEW); msg.setToUser(receiver); ssn.save(msg); // Bean SendMessageBean sendMessageBean = new SendMessageBean(); sendMessageBean.setMsgid(msg.getId()); sendMessageBean.setReceiverid(receiver.getId());// sendMessageBean.setReceivername(nickName);// sendMessageBean.setSendtime(newDate); sendMessageBean.setUserid(sender.getId());// sendMessageBean.setStatus(0);// ssn.save(sendMessageBean); // Bean ReceiverMessageBean receiverMessageBean = new ReceiverMessageBean(); receiverMessageBean.setMsgid(msg.getId()); receiverMessageBean.setSendid(sender.getId()); receiverMessageBean.setSendname(sender.getNickname()); receiverMessageBean.setUserid(receiver.getId()); receiverMessageBean.setReadtime(newDate); if (friendBane != null) receiverMessageBean.setStatus(0);// else receiverMessageBean.setStatus(3);// ssn.save(receiverMessageBean); if (i % 20 == 0) { ssn.flush(); ssn.clear(); } commit(); } } } catch (HibernateException e) { e.printStackTrace(); rollback(); } }
From source file:com.lzw.work.cms.manager.PreCarRegisterManager.java
public void getSeq() { final String sql = "EXEC Sequences 'ycscgs',''"; Session session = null; String code = ""; try {//from w ww .ja va 2s .c o m session = this.getHibernateTemplate().getSessionFactory().openSession(); ProcedureCall pc = session.createStoredProcedureCall("Sequences"); pc.registerParameter("Table_Name", String.class, ParameterMode.IN).bindValue("ycscgs"); pc.registerParameter("Seq_Type", String.class, ParameterMode.IN).bindValue(""); pc.registerParameter("MaxId", String.class, ParameterMode.OUT); // System.out.println(pc.getOutputs().getCurrent()); code = (String) pc.getOutputs().getOutputParameterValue("MaxId"); System.out.println(code); } catch (Exception e) { e.printStackTrace(); } finally { if (session != null) { session.clear(); session.close(); } } // List<Object> list = this.getBaseList(sql, null, null); // if(list==null||list.size()==0){ // return; // } // String code =(String) ((Map)list.get(0)).get("MaxDicId"); code = code.trim(); String prm = "??|"; String path = System.getProperty("temp2code"); create2Code(path, prm + code, code); Map map = new HashMap(); map.put("data", code); map.put("state", "200"); JSONObject jo = JSONObject.fromObject(map); pw.print(jo); }
From source file:com.lzw.work.cms.services.AnjianDBMannager.java
public List getUserListBy(String stationCode) { Session session = this.getHibernateTemplate().getSessionFactory().openSession(); String sql = "select * from t_user where username<>'admin' and [check]=1 and StationCode=:StationCode"; try {//from w w w. j a v a 2 s. co m Query query = session.createSQLQuery(sql); query.setParameter("StationCode", stationCode); query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP); return query.list(); } finally { session.clear(); session.close(); } }
From source file:com.lzw.work.cms.services.AnjianDBMannager.java
public List getStationList() { Session session = this.getHibernateTemplate().getSessionFactory().openSession(); String sql = "select * from tm_StationInfo"; try {/*from w w w.ja v a 2 s.c o m*/ Query query = session.createSQLQuery(sql); query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP); return query.list(); } finally { session.clear(); session.close(); } }
From source file:com.lzw.work.cms.services.AnjianDBMannager.java
public Integer userLogin(String userName, String password) { String sql = "select count(*) from t_user where userName = :userName and password = :password"; Session session = this.getHibernateTemplate().getSessionFactory().openSession(); try {// w w w. ja v a 2 s .com Query query = session.createSQLQuery(sql); query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP); Long c = (Long) query.uniqueResult(); return c.intValue(); } finally { session.clear(); session.close(); } }
From source file:com.lzw.work.cms.services.AnjianDBMannager.java
public List getTemplets() { String sql = "from DefaultItemTempletConfig"; Session session = this.getHibernateTemplate().getSessionFactory().openSession(); try {// w w w. jav a 2 s . c o m Query query = session.createQuery(sql); return query.list(); } finally { session.clear(); session.close(); } }