Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Using Transaction Template

Leroy Elendt July 8, 2012

Hello folks,

I'm currently trying to access Permissions from a page in a Job-Plugin which is, as far as I found out not possible the direct way. As I tried this:

page.getContentPermissionSet(contPerm);

It said, it is'nt possible due to LazyInitialisation :(

So I read, that its possible to do this with a TransactionCallback/Tempalte - so I tried the example Codes on the page but this does need casts and stuff and is not working exactly that way how its suggested.

I try to use it this way:

DefaultTransactionDefinition definition = new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRED);
        TransactionTemplate transactionTemplate = new TransactionTemplate((PlatformTransactionManager) ContainerManager.getComponent("transactionManager"), definition);

With this imports:

import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.transaction.PlatformTransactionManager;

It gives me the Error:

[INFO] [talledLocalContainer] # ERROR # java.lang.ClassCastException: org.springframework.orm.hibernate.HibernateTransactionManager cannot be cast to org.springframework.transaction.PlatformTransactionManager

And if I use it this way:

TransactionTemplate transactionTemplate = (TransactionTemplate) ContainerManager.getComponent("transactionManager");

with this imports

import com.atlassian.sal.api.transaction.TransactionTemplate;

Im getting this Error.

[INFO] [talledLocalContainer] # ERROR # java.lang.ClassCastException: org.springframework.orm.hibernate.HibernateTransactionManager cannot be cast to com.atlassian.sal.api.transaction.TransactionTemplate

I dont see where I go wrong. I looked through some other code and looked here at answers and tried to do it like others but it didnt seem to work either :(

3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Answer accepted
Joe Clark
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 16, 2012

Alternatively, you can use the SAL TransactionTemplate wrapper - see https://answers.atlassian.com/questions/16810/managing-a-session-manually

This problem of trying to access the Spring TransactionTemplate in a Confluence plugin exists because Confluence and the Plugin Framework use different versions of Spring.

Leroy Elendt July 17, 2012

I actually read that post before but I think I made some mistakes so this did'nt worked for me :(

2 votes
Leroy Elendt July 9, 2012

Ok,

since I needed the TransactionTemplate and there was no way to get it with the help of the Container Manager, I used the way of using a Static accessor (Link).

I just inserted the TransactionTemplate into it and accessed it from my PluginJob Class like this:

TransactionTemplate transactionTemplate = StaticAccessor.getTransactionTemplate();

and made my transaction like the way it is suggested:

Object result = transactionTemplate.execute(new TransactionCallback()
		{
		    @Override
		    public Object doInTransaction()
		    {
		        // ... execute transactional code ...
		    	ContentPermissionSet cps = pageManager.getPage(pageId).getContentPermissionSet(ContentPermission.VIEW_PERMISSION);
		    	
		    	for(ContentPermission cp: cps){
		    		if(cp.isUserPermission()){
		    			contentPermissionManager.removeContentPermission(cp);
		    		}
		    	}
		    	
		        return null;
		    }
		});

David at David Simpson Apps
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
March 20, 2014

@Leroy: What does your StaticAccessor class look like?

TakashiI June 10, 2015

Anyway, you have to get TransactionTemplate object. I try it by constructor (or setter) injection.

0 votes
Leroy Elendt July 8, 2012

I think I found a solution - I'm gonna post it later :)

TAGS
AUG Leaders

Atlassian Community Events