JBoss Community

Re: Session and thread safety

created by Sebastian Calbaza in jBPM - View the full discussion

Here is my thread safe ProcessPersistenceContextManager if someone needs it.

It needs to be set on the Environment param (env.set(EnvironmentName.PERSISTENCE_CONTEXT_MANAGER, persistenceContextManager);) before you  pass it to JPAKnowledgeService.newStatefulKnowledgeSession(kbase,null, env)

 

-----------------------------------------

public class MyJpaProcessPersistenceContextManager implements

        ProcessPersistenceContextManager, PersistenceContextManager {

 

    private EntityManagerFactory emf;

 

    private ThreadLocal<EntityManager> appScopedEntityManager=new ThreadLocal<EntityManager>();

    protected ThreadLocal<EntityManager> cmdScopedEntityManager=new ThreadLocal<EntityManager>();

 

    public ProcessPersistenceContext getProcessPersistenceContext() {

        return new JpaProcessPersistenceContext(cmdScopedEntityManager.get());

    }

 

    public MyJpaProcessPersistenceContextManager(Environment env) {

        this.emf = (EntityManagerFactory) env.get(EnvironmentName.ENTITY_MANAGER_FACTORY);

    }

 

    public PersistenceContext getApplicationScopedPersistenceContext() {

        if (this.appScopedEntityManager.get() == null) {

                this.appScopedEntityManager.set(this.emf.createEntityManager());

        }

        return new JpaPersistenceContext(appScopedEntityManager.get());

    }

 

    public PersistenceContext getCommandScopedPersistenceContext() {

        return new JpaPersistenceContext(this.cmdScopedEntityManager.get());

    }

 

    public void beginCommandScopedEntityManager() {

        if (cmdScopedEntityManager.get() == null ||

                (this.cmdScopedEntityManager.get() != null && !this.cmdScopedEntityManager.get().isOpen())) {

            this.cmdScopedEntityManager.set( this.emf.createEntityManager());

        }

        cmdScopedEntityManager.get().joinTransaction();

        appScopedEntityManager.get().joinTransaction();

    }

 

    public void endCommandScopedEntityManager() {

        if (this.cmdScopedEntityManager.get()!=null){

            this.cmdScopedEntityManager.get().flush();

            this.cmdScopedEntityManager.get().close();

        }

    }

 

    public ThreadLocal<EntityManager> getCmdScopedEntityManager() {

        return cmdScopedEntityManager;

    }

 

    public ThreadLocal<EntityManager> getAppScopedEntityManager() {

        return appScopedEntityManager;

    }

 

    public void dispose() {

            if (this.appScopedEntityManager.get() != null && this.appScopedEntityManager.get().isOpen()) {

                this.appScopedEntityManager.get().flush();

                this.appScopedEntityManager.get().close();

            }

            this.appScopedEntityManager.set(null);

            if (this.cmdScopedEntityManager.get() != null && this.cmdScopedEntityManager.get().isOpen()) {

                this.cmdScopedEntityManager.get().flush();

                this.cmdScopedEntityManager.get().close();

            }

            this.cmdScopedEntityManager.set(null);

    }

}

Reply to this message by going to Community

Start a new discussion in jBPM at Community