/// <summary> 
		/// Construct a locking strategy based on SQL UPDATE statements.
		/// </summary>
		/// <param name="lockable">The metadata for the entity to be locked. </param>
		/// <param name="lockMode">Indictates the type of lock to be acquired. </param>
		/// <remarks>
		/// read-locks are not valid for this strategy.
		/// </remarks>
		public UpdateLockingStrategy(ILockable lockable, LockMode lockMode)
		{
			this.lockable = lockable;
			this.lockMode = lockMode;
			if (lockMode.LessThan(LockMode.Upgrade))
			{
				throw new HibernateException("[" + lockMode + "] not valid for update statement");
			}
			if (!lockable.IsVersioned)
			{
				log.Warn("write locks via update not supported for non-versioned entities [" + lockable.EntityName + "]");
				sql = null;
			}
			else
			{
				sql = GenerateLockString();
			}
		}
Ejemplo n.º 2
0
 /// <summary>
 /// Construct a locking strategy based on SQL UPDATE statements.
 /// </summary>
 /// <param name="lockable">The metadata for the entity to be locked. </param>
 /// <param name="lockMode">Indicates the type of lock to be acquired. </param>
 /// <remarks>
 /// read-locks are not valid for this strategy.
 /// </remarks>
 public UpdateLockingStrategy(ILockable lockable, LockMode lockMode)
 {
     this.lockable = lockable;
     this.lockMode = lockMode;
     if (lockMode.LessThan(LockMode.Upgrade))
     {
         throw new HibernateException("[" + lockMode + "] not valid for update statement");
     }
     if (!lockable.IsVersioned)
     {
         log.Warn("write locks via update not supported for non-versioned entities [" + lockable.EntityName + "]");
         sql = null;
     }
     else
     {
         sql = GenerateLockString();
     }
 }
		/// <summary>
		/// Actually do all the hard work of loading up an object
		/// </summary>
		/// <param name="theClass"></param>
		/// <param name="id"></param>
		/// <param name="optionalObject"></param>
		/// <param name="lockMode"></param>
		/// <param name="checkDeleted"></param>
		/// <returns></returns>
		/// <remarks>
		/// 1. see if it is already loaded
		/// 2. see if it is cached
		/// 3. actually go to the database
		/// </remarks>
		private object DoLoad( System.Type theClass, object id, object optionalObject, LockMode lockMode, bool checkDeleted )
		{
			//DONT need to flush before a load by id, because ids are constant

			if( log.IsDebugEnabled )
			{
				log.Debug( "attempting to resolve " + MessageHelper.InfoString( theClass, id ) );
			}

			IClassPersister persister = GetClassPersister( theClass );
			Key key = new Key( id, persister );

			if( optionalObject != null )
			{
				persister.SetIdentifier( optionalObject, id );
			}

			// LOOK FOR LOADED OBJECT 
			// Look for Status.Loaded object
			object old = GetEntity( key );
			if( old != null )
			{
				//if this object was already loaded
				EntityEntry oldEntry = GetEntry( old );
				Status status = oldEntry.Status;
				if( checkDeleted && ( status == Status.Deleted || status == Status.Gone ) )
				{
					throw new ObjectDeletedException( "The object with that id was deleted", id, theClass );
				}
				UpgradeLock( old, oldEntry, lockMode );
				if( log.IsDebugEnabled )
				{
					log.Debug( "resolved object in session cache " + MessageHelper.InfoString( persister, id ) );
				}
				return old;

			}
			else
			{
				// check to see if we know already that it does not exist:
				if( nonExists.Contains( key ) )
				{
					log.Debug( "entity does not exist" );
					return null;
				}

				// LOOK IN CACHE
				CacheEntry entry = persister.HasCache && lockMode.LessThan( LockMode.Read ) ?
					( CacheEntry ) persister.Cache.Get( id, Timestamp ) :
					null;

				if( entry != null )
				{
					return AssembleCacheEntry( entry, id, persister, optionalObject );
				}
				else
				{
					//GO TO DATABASE
					if( log.IsDebugEnabled )
					{
						log.Debug( "object not resolved in any cache " + MessageHelper.InfoString( persister, id ) );
					}
					object result = persister.Load( id, optionalObject, lockMode, this );
					if( result == null )
					{
						// remember it doesn't exist, in case of next time
						AddNonExist( key );
					}
					return result;
				}
			}
		}