KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > jtx > JtxTransactionMode


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.jtx;
4
5 import jodd.util.ObjectUtil;
6 import jodd.util.HashCodeUtil;
7
8 /**
9  * Transaction mode.
10  *
11  * todo Add tx timeout
12  */

13 public class JtxTransactionMode {
14
15     public JtxTransactionMode() {
16         this.propagation = PROPAGATION_REQUIRED;
17         this.isolation = ISOLATION_DEFAULT;
18         this.readOnlyMode = READ_ONLY;
19     }
20
21     // ---------------------------------------------------------------- propagation behaviour
22

23     public static final int PROPAGATION_REQUIRED = 1;
24     public static final int PROPAGATION_SUPPORTS = 2;
25     public static final int PROPAGATION_MANDATORY = 3;
26     public static final int PROPAGATION_REQUIRES_NEW = 4;
27     public static final int PROPAGATION_NOT_SUPPORTED = 5;
28     public static final int PROPAGATION_NEVER = 6;
29
30     protected int propagation;
31
32     public int getPropagation() {
33         return propagation;
34     }
35
36     public JtxTransactionMode setPropagation(int propagation) {
37         this.propagation = propagation;
38         return this;
39     }
40
41     /**
42      * Propagation required.
43      * <pre>
44      * None -> T2 (same session, new tx)
45      * T1 -> T1 (same session, join tx)
46      * </pre>
47      */

48     public JtxTransactionMode propagationRequired() {
49         this.propagation = PROPAGATION_REQUIRED;
50         return this;
51     }
52
53     /**
54      * Propagation supports.
55      * <pre>
56      * None -> None (same session)
57      * T1 -> T1 (same session, join tx)
58      * </pre>
59      */

60     public JtxTransactionMode propagationSupports() {
61         this.propagation = PROPAGATION_SUPPORTS;
62         return this;
63     }
64
65     /**
66      * Propagation mandatory.
67      * <pre>
68      * None -> Error
69      * T1 -> T1 (same session, join tx)
70      * </pre>
71      */

72     public JtxTransactionMode propagationMandatory() {
73         this.propagation = PROPAGATION_MANDATORY;
74         return this;
75     }
76
77     /**
78      * Propagation requires new.
79      * <pre>
80      * None -> T2 (same session, new tx)
81      * T1 -> T2 (new session, new tx)
82      * </pre>
83      */

84     public JtxTransactionMode propagationRequiresNew() {
85         this.propagation = PROPAGATION_REQUIRES_NEW;
86         return this;
87     }
88
89     /**
90      * Propagation not supported.
91      * <pre>
92      * None -> None (same session)
93      * T1 -> None (new session, no tx)
94      * </pre>
95      */

96     public JtxTransactionMode propagationNotSupported() {
97         this.propagation = PROPAGATION_NOT_SUPPORTED;
98         return this;
99     }
100     /**
101      * Propagation never.
102      * <pre>
103      * None -> None (same session)
104      * T1 -> Error
105      * </pre>
106      */

107     public JtxTransactionMode propagationNever() {
108         this.propagation = PROPAGATION_NEVER;
109         return this;
110     }
111
112
113     // ---------------------------------------------------------------- isolation
114

115     public static final int ISOLATION_DEFAULT = 0;
116     public static final int ISOLATION_NONE = 1;
117     public static final int ISOLATION_READ_UNCOMMITTED = 2;
118     public static final int ISOLATION_READ_COMMITTED = 3;
119     public static final int ISOLATION_REPEATABLE_READ = 4;
120     public static final int ISOLATION_SERIALIZABLE = 5;
121
122     private int isolation;
123
124     public int getIsolation() {
125         return isolation;
126     }
127
128     public JtxTransactionMode setIsolation(int isolation) {
129         this.isolation = isolation;
130         return this;
131 }
132
133     public JtxTransactionMode isolationNone() {
134         this.isolation = ISOLATION_NONE;
135         return this;
136     }
137     public JtxTransactionMode isolationReadUncommitted() {
138         this.isolation = ISOLATION_READ_UNCOMMITTED;
139         return this;
140     }
141     public JtxTransactionMode isolationReadCommited() {
142         this.isolation = ISOLATION_READ_COMMITTED;
143         return this;
144     }
145     public JtxTransactionMode isolationRepeatableRead() {
146         this.isolation = ISOLATION_REPEATABLE_READ;
147         return this;
148     }
149     public JtxTransactionMode isolationSerializable() {
150         this.isolation = ISOLATION_SERIALIZABLE;
151         return this;
152     }
153
154
155
156     // ---------------------------------------------------------------- read-only
157

158     public static final boolean READ_ONLY = true;
159     public static final boolean READ_WRITE = false;
160
161     private boolean readOnlyMode = READ_ONLY;
162
163     public boolean isReadOnly() {
164         return readOnlyMode;
165     }
166
167     public JtxTransactionMode setReadOnly(boolean readOnly) {
168         this.readOnlyMode = readOnly;
169         return this;
170     }
171
172
173     // ---------------------------------------------------------------- equals & hashCode
174

175     public boolean equals(Object JavaDoc object) {
176         if (this == object) {
177             return true;
178         }
179         if (ObjectUtil.equalsType(object, this) == false) {
180             return false;
181         }
182         final JtxTransactionMode mode = (JtxTransactionMode) object;
183
184         return (mode.getPropagation() == this.propagation) &&
185                 (mode.getIsolation() == this.isolation) &&
186                 (mode.isReadOnly() == this.readOnlyMode);
187     }
188
189     public int hashCode() {
190         int result = HashCodeUtil.SEED;
191         result = HashCodeUtil.hash(result, this.propagation);
192         result = HashCodeUtil.hash(result, this.readOnlyMode);
193         result = HashCodeUtil.hash(result, this.isolation);
194         return result;
195     }
196
197
198 }
199
Popular Tags