Refresh JCR session
authorMathieu Baudier <mbaudier@argeo.org>
Mon, 16 May 2022 08:45:06 +0000 (10:45 +0200)
committerMathieu Baudier <mbaudier@argeo.org>
Mon, 16 May 2022 08:45:06 +0000 (10:45 +0200)
jcr/org.argeo.cms.jcr/src/org/argeo/cms/jcr/acr/JcrSessionAdapter.java

index 896f30a9e0248c1c8896ee7f1eac712e489af64d..f02b8d213d6573c04f1538ac7505c6ea1377af43 100644 (file)
@@ -10,6 +10,7 @@ import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.security.auth.Subject;
 
+import org.argeo.jcr.JcrException;
 import org.argeo.jcr.JcrUtils;
 
 /** Manages JCR {@link Session} in an ACR context. */
@@ -21,6 +22,8 @@ class JcrSessionAdapter {
 
        private boolean closed = false;
 
+       private Thread lastRetrievingThread = null;
+
        public JcrSessionAdapter(Repository repository, Subject subject) {
                this.repository = repository;
                this.subject = subject;
@@ -42,6 +45,8 @@ class JcrSessionAdapter {
                        throw new IllegalStateException("JCR session adapter is closed.");
 
                Thread currentThread = Thread.currentThread();
+               if (lastRetrievingThread == null)
+                       lastRetrievingThread = currentThread;
 
                Map<String, Session> threadSession = threadSessions.get(currentThread);
                if (threadSession == null) {
@@ -61,6 +66,15 @@ class JcrSessionAdapter {
                        });
                        threadSession.put(workspace, session);
                }
+
+               if (lastRetrievingThread != currentThread) {
+                       try {
+                               session.refresh(true);
+                       } catch (RepositoryException e) {
+                               throw new JcrException("Cannot refresh JCR session " + session, e);
+                       }
+               }
+               lastRetrievingThread = currentThread;
                return session;
        }