Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2014-2015 Philip Helger (www.helger.com) philip[at]helger[dot]com Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package com.helger.db.jpa;
 
 
 
Abstract request singleton to handle a single javax.persistence.EntityManager.
Note: this class does NOT implement IHasEntityManager by purpose, as this class should not be used as a direct callback parameter, because than only the object of this particular request is used.

Author(s):
Philip Helger
 
 public abstract class AbstractPerRequestEntityManager extends AbstractRequestSingleton
 {
   private static final Logger s_aLogger = LoggerFactory.getLogger (AbstractPerRequestEntityManager.class);
 
   private volatile EntityManager m_aEntityManager;
   private boolean m_bDestroyed = false;
 
   {}

  
Create a new javax.persistence.EntityManager when required.

Returns:
The created javax.persistence.EntityManager. Never null.
 
   @Nonnull
   protected abstract EntityManager createEntityManager ();

  

Returns:
The javax.persistence.EntityManager to be used in this request. If it is the first request to an javax.persistence.EntityManager in this request is created via createEntityManager(). Never null.
 
   @Nonnull
   {
     EntityManager ret;
     .readLock ().lock ();
     try
     {
       if ()
         throw new IllegalStateException ("This object was already destroyed and should not be re-used!");
       ret = ;
     }
     finally
     {
       .readLock ().unlock ();
     }
 
     if (ret == null)
     {
       // No EntityManager present for this request
       .writeLock ().lock ();
       try
       {
         // Try again in write lock
         ret = ;
         if (ret == null)
         {
           ret = createEntityManager ();
           if (ret == null)
             throw new IllegalStateException ("Failed to create EntityManager!");
            = ret;
 
           if (.isDebugEnabled ())
             .debug ("EntityManager created");
         }
       }
      finally
      {
        .writeLock ().unlock ();
      }
    }
    return ret;
  }
  protected void onDestroy (@Nonnull final IScope aScopeInDestruction)
  {
    .writeLock ().lock ();
    try
    {
      // Close EntityManager, if present
      final EntityManager aEM = ;
      if (aEM != null)
      {
        aEM.close ();
         = null;
        if (.isDebugEnabled ())
          .debug ("EntityManager destroyed");
      }
       = true;
    }
    finally
    {
      .writeLock ().unlock ();
    }
  }
New to GrepCode? Check out our FAQ X