Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 132 lines (118 sloc) 3.338 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
using System;

namespace NHibernate
{
/// <summary>
/// Instances represent a lock mode for a row of a relational database table.
/// </summary>
/// <remarks>
/// It is not intended that users spend much time worrying about locking since Hibernate
/// usually obtains exactly the right lock level automatically. Some "advanced" users may
/// wish to explicitly specify lock levels.
/// </remarks>
[Serializable]
public sealed class LockMode
{
private readonly int level;
private readonly string name;
private readonly int hashcode;

/// <summary>
///
/// </summary>
/// <param name="level"></param>
/// <param name="name"></param>
private LockMode(int level, string name)
{
this.level = level;
this.name = name;
unchecked
{
hashcode = (level * 37) ^ (name != null ? name.GetHashCode() : 0);
}
}

/// <summary></summary>
public override string ToString()
{
return name;
}

/// <summary>
/// Is this lock mode more restrictive than the given lock mode?
/// </summary>
/// <param name="mode"></param>
public bool GreaterThan(LockMode mode)
{
return level > mode.level;
}

/// <summary>
/// Is this lock mode less restrictive than the given lock mode?
/// </summary>
/// <param name="mode"></param>
public bool LessThan(LockMode mode)
{
return level < mode.level;
}

/// <summary>
/// No lock required.
/// </summary>
/// <remarks>
/// If an object is requested with this lock mode, a <c>Read</c> lock
/// might be obtained if necessary.
/// </remarks>
public static LockMode None = new LockMode(0, "None");

/// <summary>
/// A shared lock.
/// </summary>
/// <remarks>
/// Objects are loaded in <c>Read</c> mode by default
/// </remarks>
public static LockMode Read = new LockMode(5, "Read");

/// <summary>
/// An upgrade lock.
/// </summary>
/// <remarks>
/// Objects loaded in this lock mode are materialized using an
/// SQL <c>SELECT ... FOR UPDATE</c>
/// </remarks>
public static LockMode Upgrade = new LockMode(10, "Upgrade");

/// <summary>
/// Attempt to obtain an upgrade lock, using an Oracle-style
/// <c>SELECT ... FOR UPGRADE NOWAIT</c>.
/// </summary>
/// <remarks>
/// The semantics of this lock mode, once obtained, are the same as <c>Upgrade</c>
/// </remarks>
public static LockMode UpgradeNoWait = new LockMode(10, "UpgradeNoWait");

/// <summary>
/// A <c>Write</c> lock is obtained when an object is updated or inserted.
/// </summary>
/// <remarks>
/// This is not a valid mode for <c>Load()</c> or <c>Lock()</c>.
/// </remarks>
public static LockMode Write = new LockMode(10, "Write");

// TODO H3.2: Implement Force where required
/// <summary>
/// Similar to <see cref="Upgrade"/> except that, for versioned entities,
/// it results in a forced version increment.
/// </summary>
public static readonly LockMode Force = new LockMode(15, "Force");

public override bool Equals(object obj)
{
return Equals(obj as LockMode);
}

public bool Equals(LockMode other)
{
if (other == null)
{
return false;
}
if (ReferenceEquals(this, other))
{
return true;
}
return other.level == level && Equals(other.name, name);
}

public override int GetHashCode()
{
return hashcode;
}
}
}
Something went wrong with that request. Please try again.