KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > tm > CMTTest


1 //$Id: CMTTest.java,v 1.15 2005/05/25 03:34:28 oneovthafew Exp $
2
package org.hibernate.test.tm;
3
4 import java.util.HashMap JavaDoc;
5 import java.util.Map JavaDoc;
6 import java.util.Iterator JavaDoc;
7
8 import javax.transaction.Transaction JavaDoc;
9
10 import junit.framework.Test;
11 import junit.framework.TestSuite;
12
13 import org.hibernate.EntityMode;
14 import org.hibernate.Session;
15 import org.hibernate.ScrollableResults;
16 import org.hibernate.ConnectionReleaseMode;
17 import org.hibernate.util.SerializationHelper;
18 import org.hibernate.cfg.Configuration;
19 import org.hibernate.cfg.Environment;
20 import org.hibernate.criterion.Order;
21 import org.hibernate.test.TestCase;
22
23 /**
24  * @author Gavin King
25  */

26 public class CMTTest extends TestCase {
27     
28     public CMTTest(String JavaDoc str) {
29         super(str);
30     }
31     
32     public void testConcurrent() throws Exception JavaDoc {
33         getSessions().getStatistics().clear();
34         
35         DummyTransactionManager.INSTANCE.begin();
36         Session s = openSession();
37         Map JavaDoc foo = new HashMap JavaDoc();
38         foo.put("name", "Foo");
39         foo.put("description", "a big foo");
40         s.persist("Item", foo);
41         Map JavaDoc bar = new HashMap JavaDoc();
42         bar.put("name", "Bar");
43         bar.put("description", "a small bar");
44         s.persist("Item", bar);
45         DummyTransactionManager.INSTANCE.commit();
46         
47         getSessions().evictEntity("Item");
48         
49         DummyTransactionManager.INSTANCE.begin();
50         Session s1 = openSession();
51         foo = (Map JavaDoc) s1.get("Item", "Foo");
52         //foo.put("description", "a big red foo");
53
//s1.flush();
54
Transaction JavaDoc tx1 = DummyTransactionManager.INSTANCE.suspend();
55         
56         DummyTransactionManager.INSTANCE.begin();
57         Session s2 = openSession();
58         foo = (Map JavaDoc) s2.get("Item", "Foo");
59         DummyTransactionManager.INSTANCE.commit();
60         
61         DummyTransactionManager.INSTANCE.resume(tx1);
62         tx1.commit();
63         
64         getSessions().evictEntity("Item");
65
66         DummyTransactionManager.INSTANCE.begin();
67         s1 = openSession();
68         s1.createCriteria("Item").list();
69         //foo.put("description", "a big red foo");
70
//s1.flush();
71
tx1 = DummyTransactionManager.INSTANCE.suspend();
72         
73         DummyTransactionManager.INSTANCE.begin();
74         s2 = openSession();
75         s2.createCriteria("Item").list();
76         DummyTransactionManager.INSTANCE.commit();
77         
78         DummyTransactionManager.INSTANCE.resume(tx1);
79         tx1.commit();
80         
81         DummyTransactionManager.INSTANCE.begin();
82         s2 = openSession();
83         s2.createCriteria("Item").list();
84         DummyTransactionManager.INSTANCE.commit();
85         
86         assertEquals( getSessions().getStatistics().getEntityLoadCount(), 7 );
87         assertEquals( getSessions().getStatistics().getEntityFetchCount(), 0 );
88         assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 3 );
89         assertEquals( getSessions().getStatistics().getQueryCacheHitCount(), 0 );
90         assertEquals( getSessions().getStatistics().getQueryCacheMissCount(), 0 );
91
92         DummyTransactionManager.INSTANCE.begin();
93         s = openSession();
94         s.createQuery("delete from Item").executeUpdate();
95         DummyTransactionManager.INSTANCE.commit();
96     }
97     
98     public void testConcurrentCachedQueries() throws Exception JavaDoc {
99         
100         DummyTransactionManager.INSTANCE.begin();
101         Session s = openSession();
102         Map JavaDoc foo = new HashMap JavaDoc();
103         foo.put("name", "Foo");
104         foo.put("description", "a big foo");
105         s.persist("Item", foo);
106         Map JavaDoc bar = new HashMap JavaDoc();
107         bar.put("name", "Bar");
108         bar.put("description", "a small bar");
109         s.persist("Item", bar);
110         DummyTransactionManager.INSTANCE.commit();
111         
112         synchronized (this) { wait(1000); }
113
114         getSessions().getStatistics().clear();
115         
116         getSessions().evictEntity("Item");
117
118         DummyTransactionManager.INSTANCE.begin();
119         Session s1 = openSession();
120         s1.createCriteria("Item").addOrder( Order.asc("description") )
121                 .setCacheable(true).list();
122         //foo.put("description", "a big red foo");
123
//s1.flush();
124
Transaction JavaDoc tx1 = DummyTransactionManager.INSTANCE.suspend();
125                 
126
127         DummyTransactionManager.INSTANCE.begin();
128         Session s2 = openSession();
129         s2.createCriteria("Item").addOrder( Order.asc("description") )
130                 .setCacheable(true).list();
131         DummyTransactionManager.INSTANCE.commit();
132         
133         DummyTransactionManager.INSTANCE.resume(tx1);
134         tx1.commit();
135         
136         DummyTransactionManager.INSTANCE.begin();
137         s2 = openSession();
138         s2.createCriteria("Item").addOrder( Order.asc("description") )
139                 .setCacheable(true).list();
140         DummyTransactionManager.INSTANCE.commit();
141         
142         assertEquals( getSessions().getStatistics().getEntityLoadCount(), 0 );
143         assertEquals( getSessions().getStatistics().getEntityFetchCount(), 0 );
144         assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 0 );
145         assertEquals( getSessions().getStatistics().getQueryCacheHitCount(), 0 );
146         assertEquals( getSessions().getStatistics().getQueryCacheMissCount(), 0 );
147         
148         DummyTransactionManager.INSTANCE.begin();
149         s = openSession();
150         s.createQuery("delete from Item").executeUpdate();
151         DummyTransactionManager.INSTANCE.commit();
152     }
153     
154     public void testCMT() throws Exception JavaDoc {
155         getSessions().getStatistics().clear();
156
157         DummyTransactionManager.INSTANCE.begin();
158         Session s = openSession();
159         DummyTransactionManager.INSTANCE.getTransaction().commit();
160         assertFalse( s.isOpen() );
161
162         assertEquals( getSessions().getStatistics().getFlushCount(), 0 );
163
164         DummyTransactionManager.INSTANCE.begin();
165         s = openSession();
166         DummyTransactionManager.INSTANCE.getTransaction().rollback();
167         assertFalse( s.isOpen() );
168
169         DummyTransactionManager.INSTANCE.begin();
170         s = openSession();
171         Map JavaDoc item = new HashMap JavaDoc();
172         item.put("name", "The Item");
173         item.put("description", "The only item we have");
174         s.persist("Item", item);
175         DummyTransactionManager.INSTANCE.getTransaction().commit();
176         assertFalse( s.isOpen() );
177
178         DummyTransactionManager.INSTANCE.begin();
179         s = openSession();
180         item = (Map JavaDoc) s.createQuery("from Item").uniqueResult();
181         assertNotNull(item);
182         s.delete(item);
183         DummyTransactionManager.INSTANCE.getTransaction().commit();
184         assertFalse( s.isOpen() );
185
186         assertEquals( getSessions().getStatistics().getTransactionCount(), 4 );
187         assertEquals( getSessions().getStatistics().getSuccessfulTransactionCount(), 3 );
188         assertEquals( getSessions().getStatistics().getEntityDeleteCount(), 1 );
189         assertEquals( getSessions().getStatistics().getEntityInsertCount(), 1 );
190         assertEquals( getSessions().getStatistics().getSessionOpenCount(), 4 );
191         assertEquals( getSessions().getStatistics().getSessionCloseCount(), 4 );
192         assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 1 );
193         assertEquals( getSessions().getStatistics().getFlushCount(), 2 );
194
195         DummyTransactionManager.INSTANCE.begin();
196         s = openSession();
197         s.createQuery("delete from Item").executeUpdate();
198         DummyTransactionManager.INSTANCE.commit();
199     
200     }
201
202     public void testCurrentSession() throws Exception JavaDoc {
203         DummyTransactionManager.INSTANCE.begin();
204         Session s = getSessions().getCurrentSession();
205         Session s2 = getSessions().getCurrentSession();
206         assertSame( s, s2 );
207         DummyTransactionManager.INSTANCE.getTransaction().commit();
208         assertFalse( s.isOpen() );
209
210         // TODO : would be nice to automate-test that the SF internal map actually gets cleaned up
211
// i verified that is does currently in my debugger...
212
}
213
214     public void testCurrentSessionWithIterate() throws Exception JavaDoc {
215         DummyTransactionManager.INSTANCE.begin();
216         Session s = openSession();
217         Map JavaDoc item1 = new HashMap JavaDoc();
218         item1.put( "name", "Item - 1" );
219         item1.put( "description", "The first item" );
220         s.persist( "Item", item1 );
221
222         Map JavaDoc item2 = new HashMap JavaDoc();
223         item2.put( "name", "Item - 2" );
224         item2.put( "description", "The second item" );
225         s.persist( "Item", item2 );
226         DummyTransactionManager.INSTANCE.getTransaction().commit();
227
228         // First, test iterating the partial iterator; iterate to past
229
// the first, but not the second, item
230
DummyTransactionManager.INSTANCE.begin();
231         s = getSessions().getCurrentSession();
232         Iterator JavaDoc itr = s.createQuery( "from Item" ).iterate();
233         if ( !itr.hasNext() ) {
234             fail( "No results in iterator" );
235         }
236         itr.next();
237         if ( !itr.hasNext() ) {
238             fail( "Only one result in iterator" );
239         }
240         DummyTransactionManager.INSTANCE.getTransaction().commit();
241
242         // Next, iterate the entire result
243
DummyTransactionManager.INSTANCE.begin();
244         s = getSessions().getCurrentSession();
245         itr = s.createQuery( "from Item" ).iterate();
246         if ( !itr.hasNext() ) {
247             fail( "No results in iterator" );
248         }
249         while ( itr.hasNext() ) {
250             itr.next();
251         }
252         DummyTransactionManager.INSTANCE.getTransaction().commit();
253
254         DummyTransactionManager.INSTANCE.begin();
255         s = openSession();
256         s.createQuery( "delete from Item" ).executeUpdate();
257         DummyTransactionManager.INSTANCE.getTransaction().commit();
258     }
259
260     public void testCurrentSessionWithScroll() throws Exception JavaDoc {
261         DummyTransactionManager.INSTANCE.begin();
262         Session s = openSession();
263         Map JavaDoc item1 = new HashMap JavaDoc();
264         item1.put( "name", "Item - 1" );
265         item1.put( "description", "The first item" );
266         s.persist( "Item", item1 );
267
268         Map JavaDoc item2 = new HashMap JavaDoc();
269         item2.put( "name", "Item - 2" );
270         item2.put( "description", "The second item" );
271         s.persist( "Item", item2 );
272         DummyTransactionManager.INSTANCE.getTransaction().commit();
273
274         // First, test partially scrolling the result (w/o closing)
275
DummyTransactionManager.INSTANCE.begin();
276         s = getSessions().getCurrentSession();
277         ScrollableResults results = s.createQuery( "from Item" ).scroll();
278         results.next();
279         DummyTransactionManager.INSTANCE.getTransaction().commit();
280
281         // Next, scroll the entire result (w/o closing)
282
DummyTransactionManager.INSTANCE.begin();
283         s = getSessions().getCurrentSession();
284         results = s.createQuery( "from Item" ).scroll();
285         while( !results.isLast() ) {
286             results.next();
287         }
288         DummyTransactionManager.INSTANCE.getTransaction().commit();
289
290         DummyTransactionManager.INSTANCE.begin();
291         s = getSessions().getCurrentSession();
292         results = s.createQuery( "from Item" ).scroll();
293         while( !results.isLast() ) {
294             results.next();
295         }
296         results.close();
297         DummyTransactionManager.INSTANCE.getTransaction().commit();
298
299         DummyTransactionManager.INSTANCE.begin();
300         s = openSession();
301         s.createQuery( "delete from Item" ).executeUpdate();
302         DummyTransactionManager.INSTANCE.getTransaction().commit();
303     }
304
305     public void testAggressiveReleaseWithExplicitDisconnectReconnect() throws Exception JavaDoc {
306         DummyTransactionManager.INSTANCE.begin();
307         Session s = getSessions().getCurrentSession();
308
309         s.createQuery( "from Item" ).list();
310
311         s.disconnect();
312         byte[] bytes = SerializationHelper.serialize( s );
313         s = ( Session ) SerializationHelper.deserialize( bytes );
314         s.reconnect();
315
316         s.createQuery( "from Item" ).list();
317
318         DummyTransactionManager.INSTANCE.getTransaction().commit();
319     }
320
321     public void testAggressiveReleaseWithConnectionRetreival() throws Exception JavaDoc {
322         DummyTransactionManager.INSTANCE.begin();
323         Session s = openSession();
324         Map JavaDoc item1 = new HashMap JavaDoc();
325         item1.put( "name", "Item - 1" );
326         item1.put( "description", "The first item" );
327         s.save( "Item", item1 );
328
329         Map JavaDoc item2 = new HashMap JavaDoc();
330         item2.put( "name", "Item - 2" );
331         item2.put( "description", "The second item" );
332         s.save( "Item", item2 );
333         DummyTransactionManager.INSTANCE.getTransaction().commit();
334
335         try {
336             DummyTransactionManager.INSTANCE.begin();
337             s = getSessions().getCurrentSession();
338             s.createQuery( "from Item" ).scroll().next();
339             s.connection();
340             DummyTransactionManager.INSTANCE.getTransaction().commit();
341         }
342         finally {
343             DummyTransactionManager.INSTANCE.begin();
344             s = openSession();
345             s.createQuery( "delete from Item" ).executeUpdate();
346             DummyTransactionManager.INSTANCE.getTransaction().commit();
347         }
348     }
349
350     protected String JavaDoc[] getMappings() {
351         return new String JavaDoc[] { "tm/Item.hbm.xml" };
352     }
353
354     public String JavaDoc getCacheConcurrencyStrategy() {
355         return "transactional";
356     }
357
358     public static Test suite() {
359         return new TestSuite(CMTTest.class);
360     }
361
362     protected void configure(Configuration cfg) {
363         cfg.setProperty(Environment.CONNECTION_PROVIDER, DummyConnectionProvider.class.getName());
364         cfg.setProperty(Environment.TRANSACTION_MANAGER_STRATEGY, DummyTransactionManagerLookup.class.getName());
365         cfg.setProperty(Environment.AUTO_CLOSE_SESSION, "true");
366         cfg.setProperty(Environment.FLUSH_BEFORE_COMPLETION, "true");
367         cfg.setProperty(Environment.RELEASE_CONNECTIONS, ConnectionReleaseMode.AFTER_STATEMENT.toString());
368         cfg.setProperty(Environment.GENERATE_STATISTICS, "true");
369         cfg.setProperty(Environment.USE_QUERY_CACHE, "true");
370         cfg.setProperty(Environment.DEFAULT_ENTITY_MODE, EntityMode.MAP.toString());
371     }
372 }
373
374
Popular Tags