KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > db > DbTransationTest


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

3 package jodd.db;
4
5 import junit.framework.TestCase;
6 import jodd.db.pool.CoreConnectionPool;
7 import jodd.db.jtx.DbJtxResourceManager;
8 import jodd.db.jtx.DbJtxTransactionManager;
9 import jodd.jtx.JtxTransactionManager;
10 import jodd.jtx.JtxTransaction;
11 import jodd.jtx.JtxTransactionMode;
12
13 import java.sql.ResultSet JavaDoc;
14 import java.sql.SQLException JavaDoc;
15
16 public class DbTransationTest extends TestCase {
17
18     DbJtxTransactionManager tm;
19     CoreConnectionPool pool;
20
21     protected void setUp() throws Exception JavaDoc {
22         super.setUp();
23         //noinspection NonConstantStringShouldBeStringBuffer
24
String JavaDoc dbFile = System.getProperty("prj.build.dir");
25         if (dbFile == null) {
26             dbFile = "";
27         }
28         dbFile += "/db/jodd-db";
29         pool = new CoreConnectionPool();
30         pool.setDriver("org.hsqldb.jdbcDriver");
31         pool.setUrl("jdbc:hsqldb:" + dbFile);
32         pool.setUser("sa");
33         pool.setPassword("");
34         pool.init();
35         tm = new DbJtxTransactionManager();
36         tm.createDbResourceManager(pool);
37     }
38
39
40     protected void tearDown() throws Exception JavaDoc {
41         super.tearDown();
42         tm.close();
43     }
44
45
46     // ---------------------------------------------------------------- misc
47

48     public void testIsolation() throws SQLException JavaDoc {
49
50         JtxTransactionManager manager = new JtxTransactionManager();
51         manager.registerResourceManager(new DbJtxResourceManager(pool, "db"));
52
53         JtxTransaction tx1 = manager.requestTransaction(new JtxTransactionMode().propagationRequired().setReadOnly(false));
54         DbSession session1 = (DbSession) tx1.getResource("db");
55         assertNotNull(session1);
56         JtxTransaction tx2 = manager.requestTransaction(new JtxTransactionMode().propagationNotSupported());
57         DbSession session2 = (DbSession) tx2.getResource("db");
58         assertNotNull(session2);
59
60         assertFalse(session1 == session2);
61         assertEquals(2, manager.totalTransactions());
62         assertEquals(1, manager.totalActiveTransactions());
63
64         DbQuery query = new DbQuery(session1, "insert into GIRLS values(4, 'Jeniffer', 'fighting')");
65         assertEquals(1, query.executeUpdate());
66         query.close();
67
68         DbQuery query2 = new DbQuery(session2, "select count(*) from GIRLS");
69         ResultSet JavaDoc rs = query2.execute();
70         if (rs.next()) {
71             assertEquals(4, rs.getInt(1));
72         }
73
74         session1.rollbackTransaction();
75
76         rs = query2.execute();
77         if (rs.next()) {
78             assertEquals(3, rs.getInt(1));
79         }
80         rs.close();
81
82         //tx2.commit();
83
//tx1.commit();
84
manager.commitAll();
85         assertEquals(0, manager.totalTransactions());
86     }
87
88     // ---------------------------------------------------------------- presentation layer
89

90     public void testAction() {
91         assertNotNull(tm);
92         service0();
93         JtxTransaction tx1 = service1();
94         service2(tx1);
95         service3(tx1);
96         tm.commitAll();
97         assertEquals(0, tm.totalActiveTransactions());
98         assertEquals(0, tm.totalTransactions());
99     }
100
101
102     // ---------------------------------------------------------------- service layer
103

104
105     DbSession s0;
106
107     void service0() {
108         assertFalse(tm.isUnderTransaction());
109         assertEquals(0, tm.totalTransactions());
110         JtxTransaction tx = tm.createTransaction(new JtxTransactionMode());
111         assertTrue(tm.isUnderTransaction());
112         assertFalse(tm.isUnderActiveTransaction());
113         assertEquals(1, tm.totalTransactions());
114         tx.begin();
115         assertTrue(tm.isUnderActiveTransaction());
116         s0 = anyDao();
117         service0_1(tx);
118         assertTrue(tm.isUnderActiveTransaction());
119         assertTrue(tm.isUnderTransaction());
120         tx.commit();
121         assertFalse(tm.isUnderActiveTransaction());
122         assertFalse(tm.isUnderTransaction());
123         assertEquals(0, tm.totalTransactions());
124     }
125
126     void service0_1(JtxTransaction uptx) {
127         assertTrue(tm.isUnderTransaction());
128         assertTrue(tm.isUnderActiveTransaction());
129         JtxTransaction tx = tm.getTransaction();
130         assertTrue(tm.isUnderActiveTransaction());
131         assertTrue(tm.isUnderTransaction());
132         assertEquals(1, tm.totalTransactions());
133         assertEquals(uptx, tx);
134         assertEquals(s0, anyDao());
135     }
136
137     // ---------------------------------------------------------------- service layer
138

139     DbSession s1;
140
141     // service #1 REQUIRED
142
JtxTransaction service1() {
143         assertFalse(tm.isUnderTransaction());
144         JtxTransaction tx = tm.requestTransaction(new JtxTransactionMode().propagationRequired());
145         assertTrue(tm.isUnderTransaction());
146         assertTrue(tm.isUnderActiveTransaction());
147         assertEquals(1, tm.totalActiveTransactions());
148         assertEquals(1, tm.totalTransactions());
149         s1 = anyDao();
150         return tx;
151     }
152
153     // service #2 REQUIRES NEW
154
void service2(JtxTransaction tx1) {
155         assertTrue(tm.isUnderTransaction());
156         assertTrue(tm.isUnderActiveTransaction());
157         JtxTransaction tx = tm.requestTransaction(new JtxTransactionMode().propagationRequiresNew());
158         assertTrue(tm.isUnderTransaction());
159         assertTrue(tm.isUnderActiveTransaction());
160         assertEquals(2, tm.totalActiveTransactions());
161         assertEquals(2, tm.totalTransactions());
162         assertNotSame(tx1, tx);
163         assertNotSame(s1, anyDao());
164         assertTrue(tm.isUnderTransaction());
165         assertTrue(tm.isUnderActiveTransaction());
166         tx.commit();
167         assertTrue(tm.isUnderTransaction());
168         assertTrue(tm.isUnderActiveTransaction());
169     }
170
171     // service #3 REQUIRED
172
void service3(JtxTransaction tx1) {
173         assertTrue(tm.isUnderTransaction());
174         assertTrue(tm.isUnderActiveTransaction());
175         JtxTransaction tx = tm.requestTransaction(new JtxTransactionMode().propagationRequired());
176         assertTrue(tm.isUnderTransaction());
177         assertTrue(tm.isUnderActiveTransaction());
178         assertEquals(tx1, tx);
179         assertEquals(1, tm.totalActiveTransactions());
180         assertEquals(1, tm.totalTransactions());
181         assertEquals(s1, anyDao());
182         service3_1(tx);
183         assertEquals(s1, anyDao());
184         assertTrue(tm.isUnderTransaction());
185         assertTrue(tm.isUnderActiveTransaction());
186     }
187
188     // service #3_1 NOT SUPPORTED
189
void service3_1(JtxTransaction tx3) {
190         assertTrue(tm.isUnderTransaction());
191         assertTrue(tm.isUnderActiveTransaction());
192         JtxTransaction tx = tm.requestTransaction(new JtxTransactionMode().propagationNotSupported());
193         assertTrue(tm.isUnderTransaction());
194         assertFalse(tm.isUnderActiveTransaction());
195         assertNotSame(tx3, tx);
196         assertEquals(1, tm.totalActiveTransactions());
197         assertEquals(2, tm.totalTransactions());
198         assertNotSame(s1, anyDao());
199         assertNotSame(s1, anyDao());
200         assertTrue(tm.isUnderTransaction());
201         assertFalse(tm.isUnderActiveTransaction());
202         tx.commit();
203         assertTrue(tm.isUnderTransaction());
204         assertTrue(tm.isUnderActiveTransaction());
205     }
206
207     // ---------------------------------------------------------------- dao layer
208

209     DbSession anyDao() {
210         DbSession session1 = tm.getDbSession();
211         DbSession session2 = (DbSession) tm.getResource(DbJtxResourceManager.DEFAULT_RESOURCE_TYPE);
212         assertEquals(session1, session2);
213         return session1;
214     }
215
216
217 }
218
Popular Tags