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
Community moderators have prevented the ability to post new answers.
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
And (almost) everything has to be done in the doInTransaction callback (e.g. retrieving the space or the parent page).
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Are you sure that the TEST page isn't already existing in your timp space? Maybe thats the reason.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.