List of usage examples for org.hibernate Hibernate close
public static void close(Iterator iterator) throws HibernateException
From source file:de.innovationgate.webgate.api.jdbc.filehandling.CS5P4FileHandling.java
License:Open Source License
public InputStream getFileDerivateData(String id) throws WGAPIException { ContentFileDerivate cfd = (ContentFileDerivate) getParent().getSession().get(ContentFileDerivate.class, id); if (cfd == null) { return null; }//w ww . j a v a2 s . c om String hql = "select cfc from ContentFileContent as cfc where cfc.checksumSha512 = :checksum order by cfc.ordinalnr asc"; Query q = getParent().getSession().createQuery(hql); q.setParameter("checksum", cfd.getDerivateSha512()); @SuppressWarnings("rawtypes") Iterator it = q.iterate(); try { if (!it.hasNext()) { return null; } ContentFileContent cfc = (ContentFileContent) it.next(); String hqlQuery = "select cfp from ContentFileContentPart as cfp where cfp.fileContents = :contents order by cfp.partnr asc"; Query query = getParent().getSession().createQuery(hqlQuery); query.setParameter("contents", cfc); return createOptimizedInputStream(query); } finally { Hibernate.close(it); } }
From source file:de.innovationgate.webgate.api.jdbc.filehandling.CS5P4FileHandling.java
License:Open Source License
protected ContentFileContent fetchFileContents(String checksumSha512) throws WGAPIException { ContentFileContent fileContents = null; Query contentsQuery = getParent().getSession().createQuery( "from ContentFileContent as cfc where cfc.checksumSha512 = :checksum order by cfc.ordinalnr asc"); contentsQuery.setParameter("checksum", checksumSha512); Iterator<?> it = contentsQuery.iterate(); try {//from w ww. j a v a 2s.c o m if (it.hasNext()) { fileContents = (ContentFileContent) it.next(); } return fileContents; } finally { Hibernate.close(it); } }
From source file:de.innovationgate.webgate.api.jdbc.filehandling.CS5P4FileHandling.java
License:Open Source License
@Override public long dailyFileMaintenance(Logger log) throws WGAPIException { Session session = getParent().getSession(); try {//from w w w . ja v a 2s . c o m session.setDefaultReadOnly(false); long freedMemory = 0; int deletedDuplicates = 0; // Remove duplicate file contents String hql = "select cfc.checksumSha512 as checksum from ContentFileContent as cfc group by cfc.checksumSha512 having count(*) > 1"; @SuppressWarnings("unchecked") Iterator<String> duplicateChecksums = session.createQuery(hql).iterate(); while (duplicateChecksums.hasNext()) { String duplicaceChecksum = duplicateChecksums.next(); hql = "select cfc.id as id from ContentFileContent as cfc where cfc.checksumSha512 = :checksum order by cfc.ordinalnr asc"; Query q = session.createQuery(hql); q.setParameter("checksum", duplicaceChecksum); @SuppressWarnings("unchecked") Iterator<String> duplicateIds = q.iterate(); if (!duplicateIds.hasNext()) { // Just in case continue; } // Skip the first one. That is the one that will be kept and constantly retrieved duplicateIds.next(); // Delete the other duplicates while (duplicateIds.hasNext()) { String id = duplicateIds.next(); ContentFileContent cfc = (ContentFileContent) session.get(ContentFileContent.class, id); if (cfc != null) { deletedDuplicates++; freedMemory += cfc.getSize(); session.setReadOnly(cfc, false); // Delete data entities via HQL to prevent loading them hql = "delete ContentFileContentPart cfp where cfp.fileContents=:cfc"; Query deleteQ = session.createQuery(hql); deleteQ.setParameter("cfc", cfc); deleteQ.executeUpdate(); session.delete(cfc); getParent().commitHibernateTransaction(); } } Hibernate.close(duplicateIds); } Hibernate.close(duplicateChecksums); // Remove unused file contents long deletedUnusedFiles = 0; hql = "select cfc.id as id from ContentFileContent as cfc where cfc.checksumSha512 not in (select distinct cfm.checksumSha512 from ContentFileMeta as cfm where cfm.checksumSha512 is not null) and cfc.checksumSha512 not in (select distinct cfd.derivateSha512 from ContentFileDerivate as cfd)"; @SuppressWarnings("unchecked") Iterator<String> obsoleteIds = session.createQuery(hql).iterate(); while (obsoleteIds.hasNext()) { String id = obsoleteIds.next(); ContentFileContent cfc = (ContentFileContent) session.get(ContentFileContent.class, id); if (cfc != null) { deletedUnusedFiles++; freedMemory += cfc.getSize(); session.setReadOnly(cfc, false); // Delete data entities via HQL to prevent loading them hql = "delete ContentFileContentPart cfp where cfp.fileContents=:cfc"; Query deleteQ = session.createQuery(hql); deleteQ.setParameter("cfc", cfc); deleteQ.executeUpdate(); session.delete(cfc); //log.info("Deleted file contents " + cfc.getChecksumSha512() + " ordinal nr " + cfc.getOrdinalnr()); getParent().commitHibernateTransaction(); } } Hibernate.close(obsoleteIds); // Remove unused derivates of old CS5P4 style. Corresponding derivate data is deleted in the next run. hql = "select cfd.id as id from ContentFileDerivate as cfd where cfd.parentSha512 not in (select distinct cfc.checksumSha512 from ContentFileContent as cfc)"; @SuppressWarnings("unchecked") Iterator<String> obsoleteDerivateIds = session.createQuery(hql).iterate(); while (obsoleteDerivateIds.hasNext()) { String id = obsoleteDerivateIds.next(); ContentFileDerivate cfd = (ContentFileDerivate) session.get(ContentFileDerivate.class, id); if (cfd != null) { session.setReadOnly(cfd, false); session.delete(cfd); getParent().commitHibernateTransaction(); } } Hibernate.close(obsoleteDerivateIds); String freedMemoryText; if (deletedDuplicates > 0 || deletedUnusedFiles > 0) { if (freedMemory > 1024 * 1024) { freedMemoryText = WGUtils.DECIMALFORMAT_STANDARD.format(freedMemory / 1024 / 1024) + " MB of file storage"; } else { freedMemoryText = WGUtils.DECIMALFORMAT_STANDARD.format(freedMemory) + " Bytes of file storage"; } log.info("Maintenance on content store of app/plugin '" + getParent().getDb().getDbReference() + "': Deleted " + WGUtils.DECIMALFORMAT_STANDARD.format(deletedDuplicates) + " duplicates and " + WGUtils.DECIMALFORMAT_STANDARD.format(deletedUnusedFiles) + " unused file contents freeing " + freedMemoryText); } return freedMemory; } catch (Throwable e) { try { session.getTransaction().rollback(); } catch (Exception e2) { } throw new WGBackendException("Exception running daily maintenance", e); } finally { session.setDefaultReadOnly(true); } }
From source file:de.innovationgate.webgate.api.jdbc.filehandling.CS5P5FileHandling.java
License:Open Source License
@Override public long dailyFileMaintenance(Logger log) throws WGAPIException { Session session = getParent().getSession(); try {/*from ww w . ja v a 2 s. c om*/ session.setDefaultReadOnly(false); long freedMemory = 0; int deletedDuplicates = 0; // Remove duplicate file contents String hql = "select cfc.checksumSha512 as checksum from ContentFileContent as cfc group by cfc.checksumSha512 having count(*) > 1"; @SuppressWarnings("unchecked") Iterator<String> duplicateChecksums = session.createQuery(hql).iterate(); while (duplicateChecksums.hasNext()) { String duplicaceChecksum = duplicateChecksums.next(); hql = "select cfc.id as id from ContentFileContent as cfc where cfc.checksumSha512 = :checksum order by cfc.ordinalnr asc"; Query q = session.createQuery(hql); q.setParameter("checksum", duplicaceChecksum); @SuppressWarnings("unchecked") Iterator<String> duplicateIds = q.iterate(); if (!duplicateIds.hasNext()) { // Just in case continue; } // Skip the first one. That is the one that will be kept and // constantly retrieved duplicateIds.next(); // Delete the other duplicates while (duplicateIds.hasNext()) { String id = duplicateIds.next(); ContentFileContent cfc = (ContentFileContent) session.get(ContentFileContent.class, id); if (cfc != null) { deletedDuplicates++; freedMemory += cfc.getSize(); session.setReadOnly(cfc, false); // Delete data entities via HQL to prevent loading them hql = "delete ContentFileContentPart cfp where cfp.fileContents=:cfc"; Query deleteQ = session.createQuery(hql); deleteQ.setParameter("cfc", cfc); deleteQ.executeUpdate(); session.delete(cfc); getParent().commitHibernateTransaction(); } } Hibernate.close(duplicateIds); } Hibernate.close(duplicateChecksums); // Remove unused file contents, not used on attachments, derivates // or binary extension data long deletedUnusedFiles = 0; hql = "select cfc.id as id from ContentFileContent as cfc where cfc.checksumSha512 not in (select distinct cfm.checksumSha512 from ContentFileMeta as cfm where cfm.checksumSha512 is not null) and cfc.checksumSha512 not in (select distinct cfd.derivateSha512 from ContentFileDerivate as cfd) and cfc.checksumSha512 not in (select distinct ext.binarySha512 from ExtensionData as ext where ext.type=7)"; @SuppressWarnings("unchecked") Iterator<String> obsoleteIds = session.createQuery(hql).iterate(); while (obsoleteIds.hasNext()) { String id = obsoleteIds.next(); ContentFileContent cfc = (ContentFileContent) session.get(ContentFileContent.class, id); if (cfc != null) { deletedUnusedFiles++; freedMemory += cfc.getSize(); session.setReadOnly(cfc, false); // Delete data entities via HQL to prevent loading them hql = "delete ContentFileContentPart cfp where cfp.fileContents=:cfc"; Query deleteQ = session.createQuery(hql); deleteQ.setParameter("cfc", cfc); deleteQ.executeUpdate(); session.delete(cfc); // log.info("Deleted file contents " + // cfc.getChecksumSha512() + " ordinal nr " + // cfc.getOrdinalnr()); getParent().commitHibernateTransaction(); } } Hibernate.close(obsoleteIds); // Remove unused derivates of old CS5P4 style. Corresponding // derivate data is deleted in the next run. hql = "select cfd.id as id from ContentFileDerivate as cfd where cfd.parentMeta is null and cfd.parentSha512 not in (select distinct cfc.checksumSha512 from ContentFileContent as cfc)"; @SuppressWarnings("unchecked") Iterator<String> obsoleteDerivateIds = session.createQuery(hql).iterate(); while (obsoleteDerivateIds.hasNext()) { String id = obsoleteDerivateIds.next(); ContentFileDerivate cfd = (ContentFileDerivate) session.get(ContentFileDerivate.class, id); if (cfd != null) { session.setReadOnly(cfd, false); session.delete(cfd); getParent().commitHibernateTransaction(); } } Hibernate.close(obsoleteDerivateIds); String freedMemoryText; if (deletedDuplicates > 0 || deletedUnusedFiles > 0) { if (freedMemory > 1024 * 1024) { freedMemoryText = WGUtils.DECIMALFORMAT_STANDARD.format(freedMemory / 1024 / 1024) + " MB of file storage"; } else { freedMemoryText = WGUtils.DECIMALFORMAT_STANDARD.format(freedMemory) + " Bytes of file storage"; } log.info("Maintenance on content store of app/plugin '" + getParent().getDb().getDbReference() + "': Deleted " + WGUtils.DECIMALFORMAT_STANDARD.format(deletedDuplicates) + " duplicates and " + WGUtils.DECIMALFORMAT_STANDARD.format(deletedUnusedFiles) + " unused file contents freeing " + freedMemoryText); } return freedMemory; } catch (Throwable e) { try { session.getTransaction().rollback(); } catch (Exception e2) { } throw new WGBackendException("Exception running daily maintenance", e); } finally { session.setDefaultReadOnly(true); } }
From source file:de.innovationgate.webgate.api.jdbc.HibernateQueryInputStream.java
License:Open Source License
@Override public void close() throws IOException { if (_part != null) { _session.evict(_part);/*from w ww . j a v a2s. co m*/ } Hibernate.close(_data); super.close(); }
From source file:de.innovationgate.webgate.api.jdbc.WGDatabaseImpl.java
License:Open Source License
private Long upgradeFileStorage(Logger log) throws WGAPIException { _ugradeFileStorageRunning = true;/* ww w . j a v a2 s . com*/ try { if (_csVersion.getVersion() < 5.0 || _csVersion.getPatchLevel() < 4) { log.error("This task needs a content store of version 5.0 patch level 4 or higher"); return 0L; } CS5P4FileHandling fileHandling = ((CS5P4FileHandling) _fileHandling); long freedMemory = 0; while (true) { String metaHql = "from ContentFileMeta as meta where meta.checksumSha512 is null"; Iterator oldMetas = getSession().createQuery(metaHql).iterate(); try { int counter = 0; if (!oldMetas.hasNext()) { break; } while (oldMetas.hasNext() && counter < 100) { ContentFileMeta meta = (ContentFileMeta) oldMetas.next(); getSession().setReadOnly(meta, false); LockRequest lockRequest = getSession() .buildLockRequest(new LockOptions(LockMode.PESSIMISTIC_WRITE)); lockRequest.lock(meta); try { // Just-for-sure check if this is really not yet migrated getSession().refresh(meta); if (meta.getChecksumSha512() != null) { rollbackHibernateTransaction(false); continue; } log.info("Database: " + getDb().getDbReference() + ": Upgrading storage of file '" + meta.getName() + "' from document '" + meta.getParentcontent().getTitle() + "' (" + meta.getParentcontent().getStructentry().getKey() + "." + meta.getParentcontent().getLanguage().getName() + "." + meta.getParentcontent().getVersion() + ")"); // Select file parts String hqlQuery = "select cfp from ContentFilePart as cfp where cfp.meta=:metaEntity order by cfp.partnr asc"; Query query = getSession().createQuery(hqlQuery); query.setParameter("metaEntity", meta); // Migrate file parts to filecontents parts InputStream in = new HibernateQueryInputStream(fileHandling.getParent().getSession(), query, 0, isOptimizedFileHandlingDisableQueryPaging()); try { fileHandling.storeFileContents(meta, new CS5P4ContentFileDescriptor(), in); } finally { in.close(); } // Delete file parts Query deletionQuery = getSession() .createQuery("delete ContentFilePart cfp where cfp.meta = :meta"); deletionQuery.setEntity("meta", meta); deletionQuery.executeUpdate(); // Commit so we can read the file afterwards commitHibernateTransaction(); /* // Annotate the file WGDocumentImpl doc = createDocumentImpl(meta.getParentcontent()); TemporaryFile tempFile = new TemporaryFile(meta.getName(), doc.getFileData(meta.getName()), WGFactory.getTempDir()); try { WGFileMetaData md = new WGFileMetaData(new WGDocument.FakeMetaDataContext(), meta.getName(), meta.getSize(), meta.getCreated(), meta.getLastmodified(), meta.getChecksum(), meta.getChecksumSha512(), fileHandling.loadMdExtensionData(doc, meta)); _db.annotateMetadata(tempFile.getFile(), md, null); fileHandling.storeMdExtensionData(doc, md, meta); if (isSaveIsolationActive()) { getSession().update(meta); // This will not be able to store binary extension data, which however cannot be present before upgrading the file storage } } finally { tempFile.delete(); } commitHibernateTransaction(); */ } catch (Throwable e) { log.error("Exception upgrading file", e); rollbackHibernateTransaction(false); } counter++; } log.info("Clearing session cache"); refresh(); log.info("Running file storage maintenance to remove duplicate file data"); freedMemory += dailyMaintenance(log); } finally { Hibernate.close(oldMetas); } } log.info("Database: " + getDb().getDbReference() + ": Upgrading file storage freed " + WGUtils.DECIMALFORMAT_STANDARD.format(freedMemory / 1024 / 1024) + " MB of file storage memory."); return freedMemory; } finally { _ugradeFileStorageRunning = false; } }
From source file:gr.interamerican.bo2.impl.open.hibernate.AbstractHibernateQuery.java
License:Open Source License
/** * Closes the {@link HibernateIterator} of the result set. *///ww w. j a v a 2s . c o m @SuppressWarnings("nls") private void closeIterator() { if (iterator instanceof HibernateIterator) { Debug.debug(logger, "Closing hibernate iterator. " + this.getClass().getName()); Hibernate.close(iterator); } }
From source file:nl.strohalm.cyclos.utils.DataIteratorHelper.java
License:Open Source License
private static void close(final Iterator<?> iterator, final boolean remove) { if (iterator instanceof Closeable) { // Is an index reader. Invoke the close() method final Closeable closeable = (Closeable) iterator; try {/*from ww w .j av a2 s. c om*/ closeable.close(); } catch (final Exception e) { // Silently ignore } } else { // Close the iterator with Hibernate try { Hibernate.close(iterator); } catch (final Exception e) { // Silently ignore } } // Remove the iterator from the thread-bound set if (remove) { final Map<Iterator<?>, Boolean> iterators = OPEN_ITERATORS.get(); if (iterators != null) { iterators.remove(iterator); } } }
From source file:org.apache.ode.daohib.bpel.CorrelatorDaoImpl.java
License:Apache License
@SuppressWarnings("unchecked") public MessageExchangeDAO dequeueMessage(CorrelationKeySet keySet) { entering("CorrelatorDaoImpl.dequeueMessage"); MessageExchangeDAO mex = null;//w w w . ja va2 s .co m String hdr = "dequeueMessage(" + keySet + "): "; __log.debug(hdr); List<CorrelationKeySet> subSets = keySet.findSubSets(); Query qry = getSession().createFilter(_hobj.getMessageCorrelations(), generateUnmatchedQuery(subSets)); for (int i = 0; i < subSets.size(); i++) { qry.setString("s" + i, subSets.get(i).toCanonicalString()); } // We really should consider the possibility of multiple messages matching a criteria. // When the message is handled, its not too convenient to attempt to determine if the // received message conflicts with one already received. Iterator mcors; try { mcors = qry.setLockMode("this", LockMode.UPGRADE).iterate(); } catch (LockAcquisitionException e) { throw new Scheduler.JobProcessorException(e, true); } try { if (!mcors.hasNext()) { if (__log.isDebugEnabled()) __log.debug(hdr + "did not find a MESSAGE entry."); } else { HCorrelatorMessage mcor = (HCorrelatorMessage) mcors.next(); if (__log.isDebugEnabled()) __log.debug(hdr + "found MESSAGE entry " + mcor.getMessageExchange()); mex = new MessageExchangeDaoImpl(_sm, mcor.getMessageExchange()); } } finally { Hibernate.close(mcors); } return mex; }
From source file:org.apache.ode.daohib.bpel.ProcessDaoImpl.java
License:Apache License
@SuppressWarnings("unchecked") public CorrelatorDAO getCorrelator(String corrId) { entering("ProcessDaoImpl.getCorrelator"); Iterator results;//from w w w .j av a 2 s. co m Query q = getSession().createFilter(_process.getCorrelators(), QRY_CORRELATOR); results = q.setString(0, corrId).iterate(); if (!results.hasNext()) { String msg = "no such correlator: corrId = " + corrId; throw new IllegalArgumentException(msg); } try { return new CorrelatorDaoImpl(_sm, (HCorrelator) results.next()); } finally { Hibernate.close(results); } }