KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > LockMode


1 //$Id: LockMode.java,v 1.1 2004/06/03 16:30:04 steveebersole Exp $
2
package org.hibernate;
3
4 import java.io.Serializable JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.Map JavaDoc;
7
8 /**
9  * Instances represent a lock mode for a row of a relational
10  * database table. It is not intended that users spend much
11  * time worrying about locking since Hibernate usually
12  * obtains exactly the right lock level automatically.
13  * Some "advanced" users may wish to explicitly specify lock
14  * levels.
15  *
16  * @see Session#lock(Object,LockMode)
17  * @author Gavin King
18  */

19 public final class LockMode implements Serializable JavaDoc {
20     private final int level;
21     private final String JavaDoc name;
22     private static final Map JavaDoc INSTANCES = new HashMap JavaDoc();
23
24     private LockMode(int level, String JavaDoc name) {
25         this.level=level;
26         this.name=name;
27     }
28     public String JavaDoc toString() {
29         return name;
30     }
31     /**
32      * Check if this lock mode is more restrictive than the given lock mode.
33      *
34      * @param mode LockMode to check
35      * @return true if this lock mode is more restrictive than given lock mode
36      */

37     public boolean greaterThan(LockMode mode) {
38         return level > mode.level;
39     }
40     /**
41      * Check if this lock mode is less restrictive than the given lock mode.
42      *
43      * @param mode LockMode to check
44      * @return true if this lock mode is less restrictive than given lock mode
45      */

46     public boolean lessThan(LockMode mode) {
47         return level < mode.level;
48     }
49     /**
50      * No lock required. If an object is requested with this lock
51      * mode, a <tt>READ</tt> lock will be obtained if it is
52      * necessary to actually read the state from the database,
53      * rather than pull it from a cache.<br>
54      * <br>
55      * This is the "default" lock mode.
56      */

57     public static final LockMode NONE = new LockMode(0, "NONE");
58     /**
59      * A shared lock. Objects in this lock mode were read from
60      * the database in the current transaction, rather than being
61      * pulled from a cache.
62      */

63     public static final LockMode READ = new LockMode(5, "READ");
64     /**
65      * An upgrade lock. Objects loaded in this lock mode are
66      * materialized using an SQL <tt>select ... for update</tt>.
67      */

68     public static final LockMode UPGRADE = new LockMode(10, "UPGRADE");
69     /**
70      * Attempt to obtain an upgrade lock, using an Oracle-style
71      * <tt>select for update nowait</tt>. The semantics of
72      * this lock mode, once obtained, are the same as
73      * <tt>UPGRADE</tt>.
74      */

75     public static final LockMode UPGRADE_NOWAIT = new LockMode(10, "UPGRADE_NOWAIT");
76     /**
77      * A <tt>WRITE</tt> lock is obtained when an object is updated
78      * or inserted. This is not a valid mode for <tt>load()</tt>
79      * or <tt>lock()</tt>.
80      */

81     public static final LockMode WRITE = new LockMode(10, "WRITE");
82
83     static {
84         INSTANCES.put( NONE.name, NONE );
85         INSTANCES.put( READ.name, READ );
86         INSTANCES.put( UPGRADE.name, UPGRADE );
87         INSTANCES.put( UPGRADE_NOWAIT.name, UPGRADE_NOWAIT );
88         INSTANCES.put( WRITE.name, WRITE );
89     }
90
91     private Object JavaDoc readResolve() {
92         return INSTANCES.get(name);
93     }
94
95 }
96
97
98
99
100
101
102
Popular Tags