Hi,
I'm using spring framework jcrTemplate to control my JCR code and properly
manage the transactions.
I have a requirement in some of my services i need to work on two sessions
in the same transaction because some action need to be performed as a user
with higher privileges. I thought may session impersonation can help me out
here. but sadly the new impersonated session is not controlled by the
transaction manager..
I set up a test case to clarify this, check it out:
@Before
public void setUp(){
Repository repository = new TransientRepository(
System.getProperty("user.dir")+ File.separator + "target" +
File.separator + "sample.xml",
System.getProperty("user.dir")+ File.separator + "target");
sessionFactory = new JcrSessionFactory();
sessionFactory.setRepository(repository);
sessionFactory.setWorkspaceName("default");
sessionFactory.setCredentials(new
SimpleCredentials("admin","admin".toCharArray()));
tx = new LocalTransactionManager(sessionFactory);
jcrTemplate = new JcrTemplate();
jcrTemplate.setSessionFactory(sessionFactory);
jcrTemplate.setAllowCreate(false);
}
@Test
public void testImpersonationTx(){
try {
new TransactionTemplate(tx).execute(new
TransactionCallbackWithoutResult(){
@Override
protected void
doInTransactionWithoutResult(TransactionStatus status) {
jcrTemplate.execute(new JcrCallbackWithoutResult(){
@Override
protected void doInJcrWithoutResult(Session
session) throws IOException, RepositoryException {
session.getRootNode().addNode("test1");
session.save();
Session userSession = session.impersonate(new
SimpleCredentials("admin", "".toCharArray()));
userSession.getRootNode().addNode("test2");
userSession.save();
throw new RuntimeException("Forcing transaction
rollback");
}
});
}
});
} catch (RuntimeException e) {
Assert.assertEquals("Forcing transaction rollback",
e.getMessage());
}
new TransactionTemplate(tx).execute(new
TransactionCallbackWithoutResult(){
@Override
protected void doInTransactionWithoutResult(TransactionStatus
status) {
jcrTemplate.execute(new JcrCallbackWithoutResult(){
@Override
protected void doInJcrWithoutResult(Session session)
throws IOException, RepositoryException {
//Test1
Assert.assertFalse( "test1 does exists",
session.getRootNode().hasNode("test1")); //This succeds
//Test2
Assert.assertFalse( "test2 does exists",
session.getRootNode().hasNode("test2")); //This Fails
}
});
}
});
}
as you can see Test2 fails which means it is not rolled back.. any idea how
to fix this or another approach for solving this issue.
--
*BR,
Yusuf
* <http://www.gso.org.sa>