001: //$Id: LockMode.java 9581 2006-03-09 15:50:15Z epbernard $
002: package org.hibernate;
003:
004: import java.io.Serializable;
005: import java.util.HashMap;
006: import java.util.Map;
007:
008: /**
009: * Instances represent a lock mode for a row of a relational
010: * database table. It is not intended that users spend much
011: * time worrying about locking since Hibernate usually
012: * obtains exactly the right lock level automatically.
013: * Some "advanced" users may wish to explicitly specify lock
014: * levels.
015: *
016: * @see Session#lock(Object,LockMode)
017: * @author Gavin King
018: */
019: public final class LockMode implements Serializable {
020: private final int level;
021: private final String name;
022: private static final Map INSTANCES = new HashMap();
023:
024: private LockMode(int level, String name) {
025: this .level = level;
026: this .name = name;
027: }
028:
029: public String toString() {
030: return name;
031: }
032:
033: /**
034: * Check if this lock mode is more restrictive than the given lock mode.
035: *
036: * @param mode LockMode to check
037: * @return true if this lock mode is more restrictive than given lock mode
038: */
039: public boolean greaterThan(LockMode mode) {
040: return level > mode.level;
041: }
042:
043: /**
044: * Check if this lock mode is less restrictive than the given lock mode.
045: *
046: * @param mode LockMode to check
047: * @return true if this lock mode is less restrictive than given lock mode
048: */
049: public boolean lessThan(LockMode mode) {
050: return level < mode.level;
051: }
052:
053: /**
054: * No lock required. If an object is requested with this lock
055: * mode, a <tt>READ</tt> lock will be obtained if it is
056: * necessary to actually read the state from the database,
057: * rather than pull it from a cache.<br>
058: * <br>
059: * This is the "default" lock mode.
060: */
061: public static final LockMode NONE = new LockMode(0, "NONE");
062: /**
063: * A shared lock. Objects in this lock mode were read from
064: * the database in the current transaction, rather than being
065: * pulled from a cache.
066: */
067: public static final LockMode READ = new LockMode(5, "READ");
068: /**
069: * An upgrade lock. Objects loaded in this lock mode are
070: * materialized using an SQL <tt>select ... for update</tt>.
071: */
072: public static final LockMode UPGRADE = new LockMode(10, "UPGRADE");
073: /**
074: * Attempt to obtain an upgrade lock, using an Oracle-style
075: * <tt>select for update nowait</tt>. The semantics of
076: * this lock mode, once obtained, are the same as
077: * <tt>UPGRADE</tt>.
078: */
079: public static final LockMode UPGRADE_NOWAIT = new LockMode(10,
080: "UPGRADE_NOWAIT");
081: /**
082: * A <tt>WRITE</tt> lock is obtained when an object is updated
083: * or inserted. This lock mode is for internal use only and is
084: * not a valid mode for <tt>load()</tt> or <tt>lock()</tt> (both
085: * of which throw exceptions if WRITE is specified).
086: */
087: public static final LockMode WRITE = new LockMode(10, "WRITE");
088:
089: /**
090: * Similiar to {@link #UPGRADE} except that, for versioned entities,
091: * it results in a forced version increment.
092: */
093: public static final LockMode FORCE = new LockMode(15, "FORCE");
094:
095: static {
096: INSTANCES.put(NONE.name, NONE);
097: INSTANCES.put(READ.name, READ);
098: INSTANCES.put(UPGRADE.name, UPGRADE);
099: INSTANCES.put(UPGRADE_NOWAIT.name, UPGRADE_NOWAIT);
100: INSTANCES.put(WRITE.name, WRITE);
101: INSTANCES.put(FORCE.name, FORCE);
102: }
103:
104: private Object readResolve() {
105: return parse(name);
106: }
107:
108: public static LockMode parse(String name) {
109: return (LockMode) INSTANCES.get(name);
110: }
111: }
|