It's all about the answers!

Ask a question

Atomic link manipulation on workItem


Martin Dam Pedersen (1352814) | asked Dec 14 '12, 3:15 a.m.
edited Dec 14 '12, 3:25 a.m.
I want to delete and create links on a workItem as an atomic operation. So how can I ensure that it's all or nothing ?
I don't want to be in a position where I have deleted some links and then for some reason I couldn't create the new ones. Or the other way around.

I really don't care if the operation takes place on the server or in a client as long as it is atomic.

Any help is appreciated. 

2 answers



permanent link
Martin Dam Pedersen (1352814) | answered Mar 12 '13, 5:52 a.m.
edited Mar 12 '13, 5:57 a.m.
 Hi 

I actually found a solution to this problem.  

First I created a service on the server which extends AbstractService. Instructions and example can be found here : Hello Jazz -- How to write a simple Jazz component

Second i used the TransactionService inside my service to run my critical code. By getting a instance of the TransactionService it is possible to call the runInTransaction method. It looks like this:
public String myAtomicOperation() throws TeamRepositoryException {
	ITransactionService transactionService = getService(ITransactionService.class);

	String runInTransactionResult = transactionService.runInTransaction(false, new ITransactionRunnable<String>() {
		
		@Override
		public String run() throws TeamRepositoryException {
			
		    // CRITICAL CODE GOES HERE
		    // Example: createLinks, deleteLinks.	
		    
                    return "The result";
		}
	});

	return runInTransactionResult;
}
So when I want to atomic modify links on a WorkItem I call myAtomicOperation on my service, and it encapsulates all the removal and creation of links inside the transaction.
If anything goes wrong half the way through my operations and a TeamRepositoryException is thrown, then everything is rolled back automatically.  

I hope this could be useful for others to.

If anyone knows of any drawbacks to this implementation, please let me know. 
 

Comments
Ralph Schoon commented Mar 12 '13, 6:06 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

Thanks for sharing Martin!


permanent link
Ralph Schoon (63.5k33646) | answered Dec 14 '12, 3:36 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
The API is as the API is. You could check that the new links could be created before deleting the old ones. However if for some reason e.g. you don't have tested if you have permission to modify one of the elements, or the server goes down, or some other weird scenario throws an exception, there is noting you can do.

See https://rsjazz.wordpress.com/ for examples about the link API.

Comments
Martin Dam Pedersen commented Dec 14 '12, 4:56 a.m.

 Hi Ralph


I have made an example where I create some links between a workItem and a changeSet within a deliver operation contributor. When working in that scope I can create links and if I add an exceptionInfo to the collector the links are gone when returning from the deliver operation. 

This tells me that the deliver operation runs within an atomic transaction  and therefore everything gets rolled back if something went wrong. Is this correct ?

Now my question of course is. Can we do the same thing within a service of our own ?   

 


Ralph Schoon commented Dec 14 '12, 5:31 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

Hi Martin,

that is above my level of knowledge. My experience is that in an advisor, if you prevent a save, the element is rolled back to the prior state on cancel.

I had a discussion with some developers two day ago where we found that you should not do manipulations in an advisor. See this post. I was not really aware of that also - I did not read the API documentation good enough I guess.

For work items, I think if you do your modifications e.g. in a workitem operation and exit with an exception or you and don't do the save of the workingcopy, you should end up with the previous state of the work item. However, I am not really sure if this is guarded by a transaction and is really atomic. I will pass this on to someone who could probably tell.

Your answer


Register or to post your answer.


Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.