Create new Pages in LongRunningTask

Deleted user February 6, 2012

Hi,

I am trying to create new confluence pages within a long running task. I am using the following code in the runInternal method:

        PageManager pageManager = (PageManager)ContainerManager.getInstance().getContainerContext().getComponent("pageManager");
        SpaceManager spaceManager = (SpaceManager)ContainerManager.getInstance().getContainerContext().getComponent("spaceManager");
        	
        Page htmlPage = new Page();
		htmlPage.setTitle("TEST");
		        	
		Page rootPage = pageManager.getPage(983074);
		htmlPage.setParentPage(rootPage);
		        	
		Space space = spaceManager.getSpace("timp");
		htmlPage.setSpace(space);
		pageManager.saveContentEntity(htmlPage, null); 

At the saveContentEntity I get the following Exception:

a different object with the same identifier value was already associated with the session: 1081345, of class: com.atlassian.confluence.spaces.Space; nested exception is net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 1081345, of class: com.atlassian.confluence.spaces.Space

I have also found an example where a TransactionTemplate with a TransactionManager is used, buth doing this I have the problem getting a transaction manager. The following code is not working. There is no exception it just stops there executing and never gets back:

        PlatformTransactionManager tm = (PlatformTransactionManager) ContainerManager.getInstance().getContainerContext().getComponent("transactionManager"); 

Does anybody has an idea how to solve this problem?

Regards

Boris

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

3 votes
Answer accepted
Deleted user February 7, 2012

Ok, I figured it out.

A com.atlassian.sal.api.transaction.TransactionTemplate is needed to create new pages in a LongRunningTask.

In the PageAction where I start the LongRunningTask I inject a com.atlassian.sal.api.transaction.TransactionTemplate and pass it to the LongRunningTask.

To get the TransactionTemplate the following entry in the atlassian-plugin.xml is needed:

  	<component-import name="SAL Transaction Template" key="transactionTemplate">
    	<interface>com.atlassian.sal.api.transaction.TransactionTemplate</interface>
	</component-import>

The LongRunningTask class looks like this:

   protected void runInternal()
    {
        Integer status = 0;
        ExportTransactionCallback<Integer> tc
          = new ExportTransactionCallback<Integer>(status);
        transactionTemplate.execute(tc);
        progress.setStatus("Test complete");
        progress.setPercentage(100);
    }

   private class ExportTransactionCallback<T> implements TransactionCallback<T>
    {
      private T t;
      public ExportTransactionCallback(T t)
      {
        this.t = t;
      }
   
      public T doInTransaction()
      {
   
        try {
        	PageManager pageManager = (PageManager)ContainerManager.getInstance().getContainerContext().getComponent("pageManager");
        	SpaceManager spaceManager = (SpaceManager)ContainerManager.getInstance().getContainerContext().getComponent("spaceManager");
	
        	Page htmlPage = new Page();
        	htmlPage.setTitle("TEST");
        	
        	Page rootPage = pageManager.getPage(983074);
        	htmlPage.setParentPage(rootPage);
        	
	        Space space = spaceManager.getSpace("timp");
	        htmlPage.setSpace(space);
	        pageManager.saveContentEntity(htmlPage, null); 
        	
	        rootPage.addChild(htmlPage);
progress.setStatus("Test complete"); } catch (Exception e) { log.error("Error during Test", e); progress.setStatus("There was an error in the Test. Please check your log files. :" + e.getMessage()); progress.setCompletedSuccessfully(false); } return t; } }

Regards

Boris

Andrei October 29, 2018

And (almost) everything has to be done in the doInTransaction callback (e.g. retrieving the space or the parent page).

https://jira.atlassian.com/browse/CONFSERVER-9226

0 votes
Sandro Herrmann [Communardo]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 6, 2012

Are you sure that the TEST page isn't already existing in your timp space? Maybe thats the reason.

TAGS
AUG Leaders

Atlassian Community Events