Search in sources :

Example 1 with HibernateOptimisticLockingFailureException

use of org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException in project uPortal by Jasig.

the class PortletEntityRegistryImpl method deletePortletEntity.

/** Delete a portlet entity, removes it from the request, session and persistent stores */
protected void deletePortletEntity(HttpServletRequest request, IPortletEntity portletEntity, boolean cacheOnly) {
    final IPortletEntityId portletEntityId = portletEntity.getPortletEntityId();
    //Remove from request cache
    final PortletEntityCache<IPortletEntity> portletEntityMap = this.getPortletEntityMap(request);
    portletEntityMap.removeEntity(portletEntityId);
    //Remove from session cache
    final PortletEntityCache<PortletEntityData> portletEntityDataMap = this.getPortletEntityDataMap(request);
    portletEntityDataMap.removeEntity(portletEntityId);
    if (!cacheOnly && portletEntity instanceof PersistentPortletEntityWrapper) {
        final IPortletEntity persistentEntity = ((PersistentPortletEntityWrapper) portletEntity).getPersistentEntity();
        try {
            this.portletEntityDao.deletePortletEntity(persistentEntity);
        } catch (HibernateOptimisticLockingFailureException e) {
            this.logger.warn("This persistent portlet has already been deleted: " + persistentEntity + ", trying to find and delete by layout node and user.");
            final IPortletEntity existingPersistentEntity = this.portletEntityDao.getPortletEntity(persistentEntity.getLayoutNodeId(), persistentEntity.getUserId());
            if (existingPersistentEntity != null) {
                this.portletEntityDao.deletePortletEntity(existingPersistentEntity);
            }
        }
    }
}
Also used : IPortletEntity(org.apereo.portal.portlet.om.IPortletEntity) HibernateOptimisticLockingFailureException(org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException) IPortletEntityId(org.apereo.portal.portlet.om.IPortletEntityId)

Example 2 with HibernateOptimisticLockingFailureException

use of org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException in project uPortal by Jasig.

the class PortletEntityRegistryImpl method storePortletEntity.

/* (non-Javadoc)
     * @see org.apereo.portal.portlet.registry.IPortletEntityRegistry#storePortletEntity(org.apereo.portal.portlet.om.IPortletEntity)
     */
@Override
public void storePortletEntity(HttpServletRequest request, final IPortletEntity portletEntity) {
    Validate.notNull(portletEntity, "portletEntity can not be null");
    final IUserInstance userInstance = this.userInstanceManager.getUserInstance(request);
    final IPerson person = userInstance.getPerson();
    if (person.isGuest()) {
        //Never persist things for the guest user, just rely on in-memory storage
        return;
    }
    final IPortletEntityId wrapperPortletEntityId = portletEntity.getPortletEntityId();
    final Lock portletEntityLock = this.getPortletEntityLock(request, wrapperPortletEntityId);
    portletEntityLock.lock();
    try {
        final boolean shouldBePersisted = this.shouldBePersisted(portletEntity);
        if (portletEntity instanceof PersistentPortletEntityWrapper) {
            //Unwrap the persistent entity
            final IPortletEntity persistentEntity = ((PersistentPortletEntityWrapper) portletEntity).getPersistentEntity();
            //Already persistent entity that still has prefs
            if (shouldBePersisted) {
                try {
                    this.portletEntityDao.updatePortletEntity(persistentEntity);
                } catch (HibernateOptimisticLockingFailureException e) {
                    //Check if this exception is from the entity being deleted from under us.
                    final boolean exists = this.portletEntityDao.portletEntityExists(persistentEntity.getPortletEntityId());
                    if (!exists) {
                        this.logger.warn("The persistent portlet has already been deleted: " + persistentEntity + ". The passed entity should be persistent so a new persistent entity will be created");
                        this.deletePortletEntity(request, portletEntity, true);
                        this.createPersistentEntity(persistentEntity, wrapperPortletEntityId);
                    } else {
                        throw e;
                    }
                }
            } else //Already persistent entity that should not be, DELETE!
            {
                //Capture identifiers needed to recreate the entity as session persistent
                final IPortletDefinitionId portletDefinitionId = portletEntity.getPortletDefinitionId();
                final String layoutNodeId = portletEntity.getLayoutNodeId();
                final int userId = portletEntity.getUserId();
                //Delete the persistent entity
                this.deletePortletEntity(request, portletEntity, false);
                //Create a new entity and stick it in the cache
                this.getOrCreatePortletEntity(request, portletDefinitionId, layoutNodeId, userId);
            }
        } else if (portletEntity instanceof SessionPortletEntityImpl) {
            //There are preferences on the interim entity, create an store it
            if (shouldBePersisted) {
                //Remove the session scoped entity from the request and session caches
                this.deletePortletEntity(request, portletEntity, false);
                final IPortletEntity persistentEntity = createPersistentEntity(portletEntity, wrapperPortletEntityId);
                if (this.logger.isTraceEnabled()) {
                    this.logger.trace("Session scoped entity " + wrapperPortletEntityId + " should now be persistent. Deleted it from session cache and created persistent portlet entity " + persistentEntity.getPortletEntityId());
                }
            } else //Session scoped entity that is still session scoped,
            {
                //Look for a persistent entity and delete it
                final String channelSubscribeId = portletEntity.getLayoutNodeId();
                final int userId = portletEntity.getUserId();
                IPortletEntity existingPersistentEntity = this.portletEntityDao.getPortletEntity(channelSubscribeId, userId);
                if (existingPersistentEntity != null) {
                    final IPortletEntityId consistentPortletEntityId = this.createConsistentPortletEntityId(existingPersistentEntity);
                    existingPersistentEntity = new PersistentPortletEntityWrapper(existingPersistentEntity, consistentPortletEntityId);
                    this.logger.warn("A persistent portlet entity already exists: " + existingPersistentEntity + ". The passed entity has no preferences so the persistent version will be deleted");
                    this.deletePortletEntity(request, existingPersistentEntity, false);
                    //Add to request cache
                    final PortletEntityCache<IPortletEntity> portletEntityMap = this.getPortletEntityMap(request);
                    portletEntityMap.storeIfAbsentEntity(portletEntity);
                    //Add to session cache
                    final PortletEntityCache<PortletEntityData> portletEntityDataMap = this.getPortletEntityDataMap(request);
                    portletEntityDataMap.storeIfAbsentEntity(((SessionPortletEntityImpl) portletEntity).getPortletEntityData());
                }
            }
        } else {
            throw new IllegalArgumentException("Invalid portlet entity implementation passed: " + portletEntity.getClass());
        }
    } finally {
        portletEntityLock.unlock();
    }
}
Also used : HibernateOptimisticLockingFailureException(org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock) IUserInstance(org.apereo.portal.user.IUserInstance) IPortletDefinitionId(org.apereo.portal.portlet.om.IPortletDefinitionId) IPerson(org.apereo.portal.security.IPerson) IPortletEntity(org.apereo.portal.portlet.om.IPortletEntity) IPortletEntityId(org.apereo.portal.portlet.om.IPortletEntityId)

Example 3 with HibernateOptimisticLockingFailureException

use of org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException in project uPortal by Jasig.

the class PortletCookieServiceImpl method updatePortalCookie.

/**
     * (non-Javadoc)
     *
     * @see
     *     org.apereo.portal.portlet.container.services.IPortletCookieService#updatePortalCookie(javax.servlet.http.HttpServletRequest,
     *     javax.servlet.http.HttpServletResponse)
     */
@Override
public void updatePortalCookie(HttpServletRequest request, HttpServletResponse response) {
    //Get the portal cookie object
    final IPortalCookie portalCookie = this.getOrCreatePortalCookie(request);
    //Create the browser cookie
    final Cookie cookie = this.convertToCookie(portalCookie, this.portalCookieAlwaysSecure || request.isSecure());
    //Update the expiration date of the portal cookie stored in the DB if the update interval has passed
    final DateTime expires = portalCookie.getExpires();
    if (DateTime.now().minusMillis(this.maxAgeUpdateInterval).isAfter(expires.minusSeconds(this.maxAge))) {
        try {
            this.portletCookieDao.updatePortalCookieExpiration(portalCookie, cookie.getMaxAge());
        } catch (HibernateOptimisticLockingFailureException e) {
            // Especially with ngPortal UI multiple requests for individual portlet content may come at
            // the same time.  Sometimes another thread updated the portal cookie between our dao fetch and
            // dao update.  If this happens, simply ignore the update since another thread has already
            // made the update.
            logger.debug("Attempted to update expired portal cookie but another thread beat me to it." + " Ignoring update since the other thread handled it.");
            return;
        }
        // Update expiration dates of portlet cookies stored in session
        removeExpiredPortletCookies(request);
    }
    //Update the cookie in the users browser
    response.addCookie(cookie);
}
Also used : IPortalCookie(org.apereo.portal.portlet.om.IPortalCookie) Cookie(javax.servlet.http.Cookie) IPortletCookie(org.apereo.portal.portlet.om.IPortletCookie) IPortalCookie(org.apereo.portal.portlet.om.IPortalCookie) HibernateOptimisticLockingFailureException(org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException) DateTime(org.joda.time.DateTime)

Aggregations

HibernateOptimisticLockingFailureException (org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException)3 IPortletEntity (org.apereo.portal.portlet.om.IPortletEntity)2 IPortletEntityId (org.apereo.portal.portlet.om.IPortletEntityId)2 Lock (java.util.concurrent.locks.Lock)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 Cookie (javax.servlet.http.Cookie)1 IPortalCookie (org.apereo.portal.portlet.om.IPortalCookie)1 IPortletCookie (org.apereo.portal.portlet.om.IPortletCookie)1 IPortletDefinitionId (org.apereo.portal.portlet.om.IPortletDefinitionId)1 IPerson (org.apereo.portal.security.IPerson)1 IUserInstance (org.apereo.portal.user.IUserInstance)1 DateTime (org.joda.time.DateTime)1