Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
33 | PointedEar | 1 | /* |
2 | * Hibernate, Relational Persistence for Idiomatic Java |
||
3 | * |
||
4 | * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as |
||
5 | * indicated by the @author tags or express copyright attribution |
||
6 | * statements applied by the authors. All third-party contributions are |
||
7 | * distributed under license by Red Hat Middleware LLC. |
||
8 | * |
||
9 | * This copyrighted material is made available to anyone wishing to use, modify, |
||
10 | * copy, or redistribute it subject to the terms and conditions of the GNU |
||
11 | * Lesser General Public License, as published by the Free Software Foundation. |
||
12 | * |
||
13 | * This program is distributed in the hope that it will be useful, |
||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
||
15 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
||
16 | * for more details. |
||
17 | * |
||
18 | * You should have received a copy of the GNU Lesser General Public License |
||
19 | * along with this distribution; if not, write to: |
||
20 | * Free Software Foundation, Inc. |
||
21 | * 51 Franklin Street, Fifth Floor |
||
22 | * Boston, MA 02110-1301 USA |
||
23 | * |
||
24 | */ |
||
25 | package org.hibernate; |
||
26 | |||
27 | import java.io.Serializable; |
||
28 | import java.util.HashMap; |
||
29 | import java.util.Map; |
||
30 | |||
31 | /** |
||
32 | * Instances represent a lock mode for a row of a relational |
||
33 | * database table. It is not intended that users spend much |
||
34 | * time worrying about locking since Hibernate usually |
||
35 | * obtains exactly the right lock level automatically. |
||
36 | * Some "advanced" users may wish to explicitly specify lock |
||
37 | * levels. |
||
38 | * |
||
39 | * @see Session#lock(Object,LockMode) |
||
40 | * @author Gavin King |
||
41 | */ |
||
42 | public final class LockMode implements Serializable { |
||
43 | private final int level; |
||
44 | private final String name; |
||
45 | private static final Map INSTANCES = new HashMap(); |
||
46 | |||
47 | private LockMode(int level, String name) { |
||
48 | this.level=level; |
||
49 | this.name=name; |
||
50 | } |
||
51 | public String toString() { |
||
52 | return name; |
||
53 | } |
||
54 | /** |
||
55 | * Check if this lock mode is more restrictive than the given lock mode. |
||
56 | * |
||
57 | * @param mode LockMode to check |
||
58 | * @return true if this lock mode is more restrictive than given lock mode |
||
59 | */ |
||
60 | public boolean greaterThan(LockMode mode) { |
||
61 | return level > mode.level; |
||
62 | } |
||
63 | /** |
||
64 | * Check if this lock mode is less restrictive than the given lock mode. |
||
65 | * |
||
66 | * @param mode LockMode to check |
||
67 | * @return true if this lock mode is less restrictive than given lock mode |
||
68 | */ |
||
69 | public boolean lessThan(LockMode mode) { |
||
70 | return level < mode.level; |
||
71 | } |
||
72 | /** |
||
73 | * No lock required. If an object is requested with this lock |
||
74 | * mode, a <tt>READ</tt> lock will be obtained if it is |
||
75 | * necessary to actually read the state from the database, |
||
76 | * rather than pull it from a cache.<br> |
||
77 | * <br> |
||
78 | * This is the "default" lock mode. |
||
79 | */ |
||
80 | public static final LockMode NONE = new LockMode(0, "NONE"); |
||
81 | /** |
||
82 | * A shared lock. Objects in this lock mode were read from |
||
83 | * the database in the current transaction, rather than being |
||
84 | * pulled from a cache. |
||
85 | */ |
||
86 | public static final LockMode READ = new LockMode(5, "READ"); |
||
87 | /** |
||
88 | * An upgrade lock. Objects loaded in this lock mode are |
||
89 | * materialized using an SQL <tt>select ... for update</tt>. |
||
90 | */ |
||
91 | public static final LockMode UPGRADE = new LockMode(10, "UPGRADE"); |
||
92 | /** |
||
93 | * Attempt to obtain an upgrade lock, using an Oracle-style |
||
94 | * <tt>select for update nowait</tt>. The semantics of |
||
95 | * this lock mode, once obtained, are the same as |
||
96 | * <tt>UPGRADE</tt>. |
||
97 | */ |
||
98 | public static final LockMode UPGRADE_NOWAIT = new LockMode(10, "UPGRADE_NOWAIT"); |
||
99 | /** |
||
100 | * A <tt>WRITE</tt> lock is obtained when an object is updated |
||
101 | * or inserted. This lock mode is for internal use only and is |
||
102 | * not a valid mode for <tt>load()</tt> or <tt>lock()</tt> (both |
||
103 | * of which throw exceptions if WRITE is specified). |
||
104 | */ |
||
105 | public static final LockMode WRITE = new LockMode(10, "WRITE"); |
||
106 | |||
107 | /** |
||
108 | * Similiar to {@link #UPGRADE} except that, for versioned entities, |
||
109 | * it results in a forced version increment. |
||
110 | */ |
||
111 | public static final LockMode FORCE = new LockMode( 15, "FORCE" ); |
||
112 | |||
113 | static { |
||
114 | INSTANCES.put( NONE.name, NONE ); |
||
115 | INSTANCES.put( READ.name, READ ); |
||
116 | INSTANCES.put( UPGRADE.name, UPGRADE ); |
||
117 | INSTANCES.put( UPGRADE_NOWAIT.name, UPGRADE_NOWAIT ); |
||
118 | INSTANCES.put( WRITE.name, WRITE ); |
||
119 | INSTANCES.put( FORCE.name, FORCE ); |
||
120 | } |
||
121 | |||
122 | private Object readResolve() { |
||
123 | return parse( name ); |
||
124 | } |
||
125 | |||
126 | public static LockMode parse(String name) { |
||
127 | return ( LockMode ) INSTANCES.get(name); |
||
128 | } |
||
129 | } |