KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > db > DbTransactionMode


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

3 package jodd.db;
4
5 import jodd.util.ObjectUtil;
6 import jodd.util.HashCodeUtil;
7 import jodd.jtx.JtxTransactionMode;
8
9 import java.sql.Connection JavaDoc;
10
11 /**
12  * Native SQL transaction mode for {@link DbSession} transactions.
13  */

14 public class DbTransactionMode {
15
16     public DbTransactionMode() {
17         this.isolation = ISOLATION_DEFAULT;
18         this.readOnlyMode = READ_ONLY;
19     }
20
21
22     /**
23      * Created db transaction mode from general {@link jodd.jtx.JtxTransactionMode}.
24      */

25     public DbTransactionMode(JtxTransactionMode txMode) {
26         switch (txMode.getIsolation()) {
27             case JtxTransactionMode.ISOLATION_DEFAULT: isolation = ISOLATION_DEFAULT; break;
28             case JtxTransactionMode.ISOLATION_NONE: isolation = ISOLATION_NONE; break;
29             case JtxTransactionMode.ISOLATION_READ_COMMITTED: isolation = ISOLATION_READ_COMMITTED; break;
30             case JtxTransactionMode.ISOLATION_READ_UNCOMMITTED: isolation = ISOLATION_READ_UNCOMMITTED; break;
31             case JtxTransactionMode.ISOLATION_REPEATABLE_READ: isolation = ISOLATION_REPEATABLE_READ; break;
32             case JtxTransactionMode.ISOLATION_SERIALIZABLE: isolation = ISOLATION_SERIALIZABLE; break;
33             default:
34                 throw new DbSqlException("Unknown transaction isolation mode '" + txMode.getIsolation() + "'.");
35         }
36         
37         readOnlyMode = txMode.isReadOnly();
38     }
39
40     // ---------------------------------------------------------------- isolation
41

42     /**
43      * Default isolation.
44      */

45     public static final int ISOLATION_DEFAULT = -1;
46     /**
47      * @see Connection#TRANSACTION_NONE
48      */

49     public static final int ISOLATION_NONE = Connection.TRANSACTION_NONE;
50     /**
51      * @see Connection#TRANSACTION_READ_UNCOMMITTED
52      */

53     public static final int ISOLATION_READ_UNCOMMITTED = Connection.TRANSACTION_READ_UNCOMMITTED;
54     /**
55      * @see Connection#TRANSACTION_READ_COMMITTED
56      */

57     public static final int ISOLATION_READ_COMMITTED = Connection.TRANSACTION_READ_COMMITTED;
58     /**
59      * @see Connection#TRANSACTION_REPEATABLE_READ
60      */

61     public static final int ISOLATION_REPEATABLE_READ = Connection.TRANSACTION_REPEATABLE_READ;
62     /**
63      * @see Connection#TRANSACTION_SERIALIZABLE
64      */

65     public static final int ISOLATION_SERIALIZABLE = Connection.TRANSACTION_SERIALIZABLE;
66
67     private int isolation;
68
69     public int getIsolation() {
70         return isolation;
71     }
72
73     public DbTransactionMode setIsolation(int isolation) {
74         this.isolation = isolation;
75         return this;
76 }
77
78     public DbTransactionMode isolationNone() {
79         this.isolation = ISOLATION_NONE;
80         return this;
81     }
82     public DbTransactionMode isolationReadUncommitted() {
83         this.isolation = ISOLATION_READ_UNCOMMITTED;
84         return this;
85     }
86     public DbTransactionMode isolationReadCommited() {
87         this.isolation = ISOLATION_READ_COMMITTED;
88         return this;
89     }
90     public DbTransactionMode isolationRepeatableRead() {
91         this.isolation = ISOLATION_REPEATABLE_READ;
92         return this;
93     }
94     public DbTransactionMode isolationSerializable() {
95         this.isolation = ISOLATION_SERIALIZABLE;
96         return this;
97     }
98
99
100
101     // ---------------------------------------------------------------- read-only
102

103     public static final boolean READ_ONLY = true;
104     public static final boolean READ_WRITE = false;
105
106     private boolean readOnlyMode = READ_ONLY;
107
108     public boolean isReadOnly() {
109         return readOnlyMode;
110     }
111
112     public DbTransactionMode setReadOnly(boolean readOnly) {
113         this.readOnlyMode = readOnly;
114         return this;
115     }
116
117
118     // ---------------------------------------------------------------- equals & hashCode
119

120     public boolean equals(Object JavaDoc object) {
121         if (this == object) {
122             return true;
123         }
124         if (ObjectUtil.equalsType(object, this) == false) {
125             return false;
126         }
127         final DbTransactionMode mode = (DbTransactionMode) object;
128
129         return (mode.getIsolation() == this.isolation) &&
130                 (mode.isReadOnly() == this.readOnlyMode);
131     }
132
133     public int hashCode() {
134         int result = HashCodeUtil.SEED;
135         result = HashCodeUtil.hash(result, this.readOnlyMode);
136         result = HashCodeUtil.hash(result, this.isolation);
137         return result;
138     }
139
140
141 }
142
Popular Tags