Hi!
I have now faced this issue for a couple of days now, and I can't seem to be able to fix it. I have tried to do the following code:
public boolean convertMailToPageProgrammatically(Message m, String parentPageTitle, String title, String spaceKey) {
try {
log.warn("convertMailToPage for message " + m.getSubject());
SpaceManager spaceManager = ComponentLocator.getComponent(SpaceManager.class);
PageManager pageManager = ComponentLocator.getComponent(PageManager.class);
ConfluenceIndexer confluenceIndexer = ComponentLocator.getComponent(ConfluenceIndexer.class);
Space spaceToUse = spaceManager.getSpace(spaceKey);
log.warn("Page: " + pageManager.getPage("SpaceKey", "New Page 1337").getTitle());
Page page = new Page();
Page parentPage = pageManager.getPage(spaceKey, parentPageTitle);
page.setSpace(spaceToUse);
page.setTitle(title);
page.setBodyAsString(getTextFromMessage(m));
page.setVersion(1);
page.setParentPage(parentPage);
log.warn("now we are here");
pageManager.saveContentEntity(page, null);
parentPage.addChild(page);
confluenceIndexer.reIndex(page);
return true;
} catch (MessagingException | IOException e) {
log.error("ERROR! " + e.getMessage());
return false;
}
}This code has worked in a lot of my other plugins. However, the difference now is that I'm trying to run this through a Confluence Scheduled Job, which itself seems to run just fine. You can see the code below:
package net.teliacompany.diva.confluence.purgingbackend.job;
import javax.mail.MessagingException;
import com.atlassian.plugin.spring.scanner.annotation.component.Scanned;
import com.atlassian.scheduler.JobRunner;
import com.atlassian.scheduler.JobRunnerRequest;
import com.atlassian.scheduler.JobRunnerResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import net.teliacompany.diva.confluence.purgingbackend.mail.MailUtilities;
import net.teliacompany.diva.confluence.purgingbackend.utilities.PARUtilities;
@Component
@Scanned
public class PurgingAndRetentionJobv2 implements JobRunner {
private static final Logger log = LoggerFactory.getLogger(PurgingAndRetentionJobv2.class);
private final PARUtilities parUti = new PARUtilities();
private final MailUtilities mailUti = new MailUtilities();
@Override
public JobRunnerResponse runJob(JobRunnerRequest request) {
try {
int noOfMails = mailUti.checkIfThereAnyNewAvailableMails();
if (noOfMails > 0) {
log.warn("There was " + noOfMails + " mails to process");
return JobRunnerResponse.success("There was " + parUti.processMails() + " new mail, job complete!");
} else {
return JobRunnerResponse.success("There was no new mail, job complete!");
}
} catch (MessagingException ex) {
return JobRunnerResponse.failed("Job Failed!");
}
}
}
However, as soon as I try to execute, it runs fine (it can get pages just fine when using the PageManager). But when it it arrives at saving the actual page, I get the following error:
2019-10-29 09:52:30,696 WARN [Caesium-1-4] [confluence.impl.hibernate.ConfluenceHibernateTransactionManager] doRollback Performing rollback. Transactions:
->[com.atlassian.confluence.pages.DefaultPageManager.saveContentEntity]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT (Session #247582940)
2019-10-29 09:52:30,697 ERROR [Caesium-1-4] [confluence.purgingbackend.utilities.PARUtilities] processMails ERROR!
2019-10-29 09:52:30,698 ERROR [Caesium-1-4] [confluence.purgingbackend.utilities.PARUtilities] processMails A different object with the same identifier value was already associated with the session : [com.atlassian.confluence.spaces.Space#21594119]; nested exception is org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session : [com.atlassian.confluence.spaces.Space#21594119]
2019-10-29 09:52:30,698 ERROR [Caesium-1-4] [confluence.purgingbackend.utilities.PARUtilities] processMails ERROR:___ org.springframework.orm.hibernate5.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:265)
I have tried using both Atlassian Spring Scanner version 1 and 2 to no avail, I always end up with the same error no matter what I do. What am I doing wrong?