cancel
Showing results for 
Search instead for 
Did you mean: 

Alfresco Sessions in threads

rrino
Champ in-the-making
Champ in-the-making
Hi i'm new in alfresco and i'm trying to do some tests with uploading files into repository.
Everything was easy until i tried to run several threads with session for each one.
I got Exception  :
Exception in thread "main" javax.jcr.RepositoryException: Only one active session per thread.‍
I've tried it with JackRabbit and there is everything ok - can i do the same with Alfresco?
4 REPLIES 4

mrogers
Star Contributor
Star Contributor
Several threads with a session for each should be O.K.   

What you can't do is have several sessions on a single thread since theres a bunch of stuff stored in a thread local slot, including the transaction identifier.

rrino
Champ in-the-making
Champ in-the-making
Thanks for Your reply.

Now I'm doing somethig else wrong:/

In main thread i make instance of MyRepository (repository = new MyRepository())in my case:

import java.io.IOException;import javax.jcr.Repository;import javax.jcr.RepositoryException;import javax.jcr.Session;import javax.jcr.SimpleCredentials;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MyRepository {  private static final String PASSWORD = "admin";  private static final String LOGIN = "admin";    private Repository repository;    MyRepository(){    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:alfresco/application-context.xml");    repository = (Repository) context.getBean("JCR.Repository");  }    public Session getSession() throws IOException, RepositoryException {    System.out.println("Login… Getting new session… ");    return repository.login(new SimpleCredentials(LOGIN, PASSWORD.toCharArray()));  }}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

And here goes two scenerio:

when I in thread Main make

Session session = repository.getSession();

and giva session as a argument to class constructor A (it invokes constructor in B with the session as argument too) everything works ok.

In second case when i give repository as argument of class C extends Thread constructor and in this class i make
Session session = repository.getSession();‍
 

and now give as an class A constructor argument, and when it invokes C with session and run e.g. method session.getRootNode I get exception:

     [java] net.sf.acegisecurity.AuthenticationCredentialsNotFoundException: A valid SecureContext was not provided in the RequestContext     [java]     at net.sf.acegisecurity.intercept.AbstractSecurityInterceptor.credentialsNotFound(AbstractSecurityInterceptor.java:477)     [java]     at net.sf.acegisecurity.intercept.AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:355)     [java]     at net.sf.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor.invoke(MethodSecurityInterceptor.java:77)     [java]     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:176)     [java]     at org.alfresco.repo.security.permissions.impl.ExceptionTranslatorMethodInterceptor.invoke(ExceptionTranslatorMethodInterceptor.java:49)     [java]     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:176)     [java]     at org.alfresco.repo.audit.AuditComponentImpl.auditImpl(AuditComponentImpl.java:256)     [java]     at org.alfresco.repo.audit.AuditComponentImpl.audit(AuditComponentImpl.java:191)     [java]     at org.alfresco.repo.audit.AuditMethodInterceptor.invoke(AuditMethodInterceptor.java:69)     [java]     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:176)     [java]     at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)     [java] MultiAccessTest stop…5     [java]     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:176)     [java]     at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:210)     [java]     at $Proxy21.getCurrentUserName(Unknown Source)     [java]     at org.alfresco.jcr.util.JCRProxyFactory$SessionConte xtInvocationHandler.invoke(JCRProxyFactory.java:124)     [java]     at $Proxy84.getRootNode(Unknown Source)     [java]     at pl.swmind.jcr.test.UploadTest.execute(Unknown Source)     [java]     at pl.swmind.jcr.test.SingleTest.start(Unknown Source)     [java]     at pl.swmind.jcr.test.MultiAccessTest.run(Unknown Source)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Probably somewhere there is my stupid mistake but now i don't see it…
What is a problem here?

rrino
Champ in-the-making
Champ in-the-making
my stupid mistake of course - in class that extend thread i've tried to get session not in method run like i should but in constructor. I've not noticed it and been looking for mistake in other places:/

mrogers
Star Contributor
Star Contributor
Glad to hear you have fixed it.