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 v
alid SecureContext was not provided in the RequestContext
     [java]     at net.sf.acegisecurity.intercept.AbstractSecurityInterceptor.cr
edentialsNotFound(AbstractSecurityInterceptor.java:477)
     [java]     at net.sf.acegisecurity.intercept.AbstractSecurityInterceptor.be
foreInvocation(AbstractSecurityInterceptor.java:355)
     [java]     at net.sf.acegisecurity.intercept.method.aopalliance.MethodSecur
ityInterceptor.invoke(MethodSecurityInterceptor.java:77)
     [java]     at org.springframework.aop.framework.ReflectiveMethodInvocation.
proceed(ReflectiveMethodInvocation.java:176)
     [java]     at org.alfresco.repo.security.permissions.impl.ExceptionTranslat
orMethodInterceptor.invoke(ExceptionTranslatorMethodInterceptor.java:49)
     [java]     at org.springframework.aop.framework.ReflectiveMethodInvocation.
proceed(ReflectiveMethodInvocation.java:176)
     [java]     at org.alfresco.repo.audit.AuditComponentImpl.auditImpl(AuditCom
ponentImpl.java:256)
     [java]     at org.alfresco.repo.audit.AuditComponentImpl.audit(AuditCompone
ntImpl.java:191)
     [java]     at org.alfresco.repo.audit.AuditMethodInterceptor.invoke(AuditMe
thodInterceptor.java:69)
     [java]     at org.springframework.aop.framework.ReflectiveMethodInvocation.
proceed(ReflectiveMethodInvocation.java:176)
     [java]     at org.springframework.transaction.interceptor.TransactionInterc
eptor.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(J
dkDynamicAopProxy.java:210)
     [java]     at $Proxy21.getCurrentUserName(Unknown Source)
     [java]     at org.alfresco.jcr.util.JCRProxyFactory$SessionConte xtInvocatio
nHandler.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.
Getting started

Tags


Find what you came for

We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.