33
33
* levels.
34
34
*
35
35
* @author Gavin King
36
+ *
36
37
* @see Session#lock(Object, LockMode)
37
38
*/
38
39
public enum LockMode {
@@ -44,44 +45,44 @@ public enum LockMode {
44
45
* <br>
45
46
* This is the "default" lock mode.
46
47
*/
47
- NONE ( 0 ),
48
+ NONE ( 0 , "none" ),
48
49
/**
49
50
* A shared lock. Objects in this lock mode were read from
50
51
* the database in the current transaction, rather than being
51
52
* pulled from a cache.
52
53
*/
53
- READ ( 5 ),
54
+ READ ( 5 , "read" ),
54
55
/**
55
56
* An upgrade lock. Objects loaded in this lock mode are
56
57
* materialized using an SQL <tt>select ... for update</tt>.
57
58
*
58
59
* @deprecated instead use PESSIMISTIC_WRITE
59
60
*/
60
61
@ Deprecated
61
- UPGRADE ( 10 ),
62
+ UPGRADE ( 10 , "upgrade" ),
62
63
/**
63
64
* Attempt to obtain an upgrade lock, using an Oracle-style
64
65
* <tt>select for update nowait</tt>. The semantics of
65
66
* this lock mode, once obtained, are the same as
66
67
* <tt>UPGRADE</tt>.
67
68
*/
68
- UPGRADE_NOWAIT ( 10 ),
69
+ UPGRADE_NOWAIT ( 10 , "upgrade-nowait" ),
69
70
70
71
/**
71
72
* Attempt to obtain an upgrade lock, using an Oracle-style
72
73
* <tt>select for update skip locked</tt>. The semantics of
73
74
* this lock mode, once obtained, are the same as
74
75
* <tt>UPGRADE</tt>.
75
76
*/
76
- UPGRADE_SKIPLOCKED ( 10 ),
77
+ UPGRADE_SKIPLOCKED ( 10 , "upgrade-skiplocked" ),
77
78
78
79
/**
79
80
* A <tt>WRITE</tt> lock is obtained when an object is updated
80
81
* or inserted. This lock mode is for internal use only and is
81
82
* not a valid mode for <tt>load()</tt> or <tt>lock()</tt> (both
82
83
* of which throw exceptions if WRITE is specified).
83
84
*/
84
- WRITE ( 10 ),
85
+ WRITE ( 10 , "write" ),
85
86
86
87
/**
87
88
* Similar to {@link #UPGRADE} except that, for versioned entities,
@@ -90,7 +91,7 @@ public enum LockMode {
90
91
* @deprecated instead use PESSIMISTIC_FORCE_INCREMENT
91
92
*/
92
93
@ Deprecated
93
- FORCE ( 15 ),
94
+ FORCE ( 15 , "force" ),
94
95
95
96
/**
96
97
* start of javax.persistence.LockModeType equivalent modes
@@ -100,34 +101,37 @@ public enum LockMode {
100
101
* Optimistically assume that transaction will not experience contention for
101
102
* entities. The entity version will be verified near the transaction end.
102
103
*/
103
- OPTIMISTIC ( 6 ),
104
+ OPTIMISTIC ( 6 , "optimistic" ),
104
105
105
106
/**
106
107
* Optimistically assume that transaction will not experience contention for
107
108
* entities. The entity version will be verified and incremented near the transaction end.
108
109
*/
109
- OPTIMISTIC_FORCE_INCREMENT ( 7 ),
110
+ OPTIMISTIC_FORCE_INCREMENT ( 7 , "optimistic_force_increment" ),
110
111
111
112
/**
112
113
* Implemented as PESSIMISTIC_WRITE.
113
114
* TODO: introduce separate support for PESSIMISTIC_READ
114
115
*/
115
- PESSIMISTIC_READ ( 12 ),
116
+ PESSIMISTIC_READ ( 12 , "pessimistic_read" ),
116
117
117
118
/**
118
119
* Transaction will obtain a database lock immediately.
119
120
* TODO: add PESSIMISTIC_WRITE_NOWAIT
120
121
*/
121
- PESSIMISTIC_WRITE ( 13 ),
122
+ PESSIMISTIC_WRITE ( 13 , "pessimistic_write" ),
122
123
123
124
/**
124
125
* Transaction will immediately increment the entity version.
125
126
*/
126
- PESSIMISTIC_FORCE_INCREMENT ( 17 );
127
+ PESSIMISTIC_FORCE_INCREMENT ( 17 , "pessimistic_force_increment" );
128
+
127
129
private final int level ;
130
+ private final String externalForm ;
128
131
129
- private LockMode (int level ) {
132
+ private LockMode (int level , String externalForm ) {
130
133
this .level = level ;
134
+ this .externalForm = externalForm ;
131
135
}
132
136
133
137
/**
@@ -151,4 +155,22 @@ public boolean greaterThan(LockMode mode) {
151
155
public boolean lessThan (LockMode mode ) {
152
156
return level < mode .level ;
153
157
}
158
+
159
+ public String toExternalForm () {
160
+ return externalForm ;
161
+ }
162
+
163
+ public static LockMode fromExternalForm (String externalForm ) {
164
+ if ( externalForm == null ) {
165
+ return NONE ;
166
+ }
167
+
168
+ for ( LockMode lockMode : LockMode .values () ) {
169
+ if ( lockMode .externalForm .equalsIgnoreCase ( externalForm ) ) {
170
+ return lockMode ;
171
+ }
172
+ }
173
+
174
+ throw new IllegalArgumentException ( "Unable to interpret LockMode reference from incoming external form : " + externalForm );
175
+ }
154
176
}
0 commit comments