Uploaded image for project: 'XWiki Platform'
  1. XWiki Platform
  2. XWIKI-6763

loadAttachmentContent should not set the attachment content object before loading is effective

    XMLWordPrintable

Details

    • Bug
    • Resolution: Duplicate
    • Major
    • None
    • 3.1
    • Storage
    • None
    • Unknown

    Description

      In http://svn.xwiki.org/svnroot/xwiki/platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/store/XWikiHibernateAttachmentStore.java

        public void loadAttachmentContent(XWikiAttachment attachment, XWikiContext context, boolean bTransaction)
              throws XWikiException
          {
              try {
                  if (bTransaction) {
                      checkHibernate(context);
                      bTransaction = beginTransaction(false, context);
                  }
                  Session session = getSession(context);
      
                  String db = context.getDatabase();
                  String attachdb = (attachment.getDoc() == null) ? null : attachment.getDoc().getDatabase();
                  try {
                      if (attachdb != null) {
                          context.setDatabase(attachdb);
                      }
      
      
                      // Hibernate calls setContent which causes isContentDirty to be true. This is not what we want.
                      content.setContentDirty(false);
      
                  } finally {
                      context.setDatabase(db);
                  }
      
                  if (bTransaction) {
                      endTransaction(context, false, false);
                  }
              } catch (Exception e) {
                  Object[] args = {attachment.getFilename(), attachment.getDoc().getFullName()};
                  throw new XWikiException(XWikiException.MODULE_XWIKI_STORE,
                      XWikiException.ERROR_XWIKI_STORE_HIBERNATE_LOADING_ATTACHMENT,
                      "Exception while loading attachment {0} of document {1}", e, args);
              } finally {
                  try {
                      if (bTransaction) {
                          endTransaction(context, false, false);
                      }
                  } catch (Exception e) {
                  }
              }
          }
      

      The following 3 lines

                      XWikiAttachmentContent content = new XWikiAttachmentContent(attachment);
                      attachment.setAttachment_content(content);
                      session.load(content, new Long(content.getId()));
      

      Show that if session.load fails, then still the attachment content is set in the attachment object, thus leading to a corrupt cache of the attachment content.

      The lines should be inverted or failure should be caught to unset the attachment content.

      This issue was experience on a production server (attachment being corrupt although the data correct in the DB. Flush cache fixed the issue)

      Attachments

        Issue Links

          Activity

            People

              marta Marta Girdea
              ludovic Ludovic Dubost
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: