KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > hibernate > HibernateTransactionManagerTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.orm.hibernate;
18
19 import java.io.IOException JavaDoc;
20 import java.sql.Connection JavaDoc;
21 import java.sql.DatabaseMetaData JavaDoc;
22 import java.sql.SQLException JavaDoc;
23 import java.sql.Savepoint JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26
27 import javax.sql.DataSource JavaDoc;
28
29 import junit.framework.TestCase;
30 import net.sf.hibernate.FlushMode;
31 import net.sf.hibernate.HibernateException;
32 import net.sf.hibernate.Interceptor;
33 import net.sf.hibernate.JDBCException;
34 import net.sf.hibernate.Query;
35 import net.sf.hibernate.Session;
36 import net.sf.hibernate.SessionFactory;
37 import net.sf.hibernate.Transaction;
38 import org.easymock.MockControl;
39
40 import org.springframework.beans.factory.BeanFactory;
41 import org.springframework.core.JdkVersion;
42 import org.springframework.dao.DataIntegrityViolationException;
43 import org.springframework.jdbc.datasource.DriverManagerDataSource;
44 import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy;
45 import org.springframework.jdbc.support.SQLStateSQLExceptionTranslator;
46 import org.springframework.transaction.CannotCreateTransactionException;
47 import org.springframework.transaction.PlatformTransactionManager;
48 import org.springframework.transaction.TransactionDefinition;
49 import org.springframework.transaction.TransactionStatus;
50 import org.springframework.transaction.UnexpectedRollbackException;
51 import org.springframework.transaction.support.TransactionCallback;
52 import org.springframework.transaction.support.TransactionCallbackWithoutResult;
53 import org.springframework.transaction.support.TransactionSynchronizationManager;
54 import org.springframework.transaction.support.TransactionTemplate;
55
56 /**
57  * @author Juergen Hoeller
58  * @since 02.05.2003
59  */

60 public class HibernateTransactionManagerTests extends TestCase {
61
62     public void testTransactionCommit() throws SQLException JavaDoc, HibernateException {
63         MockControl dsControl = MockControl.createControl(DataSource JavaDoc.class);
64         final DataSource JavaDoc ds = (DataSource JavaDoc) dsControl.getMock();
65         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
66         Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
67         MockControl sfControl = MockControl.createControl(SessionFactory.class);
68         final SessionFactory sf = (SessionFactory) sfControl.getMock();
69         MockControl sessionControl = MockControl.createControl(Session.class);
70         Session session = (Session) sessionControl.getMock();
71         MockControl txControl = MockControl.createControl(Transaction.class);
72         Transaction tx = (Transaction) txControl.getMock();
73         MockControl queryControl = MockControl.createControl(Query.class);
74         Query query = (Query) queryControl.getMock();
75
76         final List JavaDoc list = new ArrayList JavaDoc();
77         list.add("test");
78         con.getTransactionIsolation();
79         conControl.setReturnValue(Connection.TRANSACTION_READ_COMMITTED);
80         con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
81         conControl.setVoidCallable(1);
82         con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
83         conControl.setVoidCallable(1);
84         con.isReadOnly();
85         conControl.setReturnValue(false, 1);
86         sf.openSession();
87         sfControl.setReturnValue(session, 1);
88         session.beginTransaction();
89         sessionControl.setReturnValue(tx, 1);
90         session.connection();
91         sessionControl.setReturnValue(con, 2);
92         session.createQuery("some query string");
93         sessionControl.setReturnValue(query, 1);
94         query.setTimeout(10);
95         queryControl.setReturnValue(query, 1);
96         query.list();
97         queryControl.setReturnValue(list, 1);
98         session.close();
99         sessionControl.setReturnValue(null, 1);
100         tx.commit();
101         txControl.setVoidCallable(1);
102         dsControl.replay();
103         conControl.replay();
104         sfControl.replay();
105         sessionControl.replay();
106         txControl.replay();
107         queryControl.replay();
108
109         HibernateTransactionManager tm = new HibernateTransactionManager();
110         tm.setJdbcExceptionTranslator(new SQLStateSQLExceptionTranslator());
111         tm.setSessionFactory(sf);
112         tm.setDataSource(ds);
113         TransactionTemplate tt = new TransactionTemplate(tm);
114         tt.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
115         tt.setTimeout(10);
116         assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
117         assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
118         assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
119
120         Object JavaDoc result = tt.execute(new TransactionCallback() {
121             public Object JavaDoc doInTransaction(TransactionStatus status) {
122                 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
123                 assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(ds));
124                 assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
125                 assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
126                 HibernateTemplate ht = new HibernateTemplate(sf);
127                 return ht.find("some query string");
128             }
129         });
130         assertTrue("Correct result list", result == list);
131
132         assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
133         assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
134         assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
135         dsControl.verify();
136         conControl.verify();
137         sfControl.verify();
138         sessionControl.verify();
139         txControl.verify();
140         queryControl.verify();
141     }
142
143     public void testTransactionRollback() throws HibernateException, SQLException JavaDoc {
144         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
145         Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
146         MockControl sfControl = MockControl.createControl(SessionFactory.class);
147         final SessionFactory sf = (SessionFactory) sfControl.getMock();
148         MockControl sessionControl = MockControl.createControl(Session.class);
149         Session session = (Session) sessionControl.getMock();
150         MockControl txControl = MockControl.createControl(Transaction.class);
151         Transaction tx = (Transaction) txControl.getMock();
152
153         sf.openSession();
154         sfControl.setReturnValue(session, 1);
155         session.beginTransaction();
156         sessionControl.setReturnValue(tx, 1);
157         session.close();
158         sessionControl.setReturnValue(null, 1);
159         tx.rollback();
160         txControl.setVoidCallable(1);
161         session.connection();
162         sessionControl.setReturnValue(con, 2);
163         con.isReadOnly();
164         conControl.setReturnValue(false, 1);
165         sfControl.replay();
166         sessionControl.replay();
167         txControl.replay();
168
169         PlatformTransactionManager tm = new HibernateTransactionManager(sf);
170         TransactionTemplate tt = new TransactionTemplate(tm);
171         assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
172         assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
173
174         try {
175             tt.execute(new TransactionCallback() {
176                 public Object JavaDoc doInTransaction(TransactionStatus status) {
177                     assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
178                     HibernateTemplate ht = new HibernateTemplate(sf);
179                     return ht.executeFind(new HibernateCallback() {
180                         public Object JavaDoc doInHibernate(Session session) {
181                             throw new RuntimeException JavaDoc("application exception");
182                         }
183                     });
184                 }
185             });
186             fail("Should have thrown RuntimeException");
187         }
188         catch (RuntimeException JavaDoc ex) {
189             // expected
190
}
191
192         assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
193         assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
194         sfControl.verify();
195         sessionControl.verify();
196         txControl.verify();
197     }
198
199     public void testTransactionRollbackOnly() throws HibernateException, SQLException JavaDoc {
200         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
201         Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
202         MockControl sfControl = MockControl.createControl(SessionFactory.class);
203         final SessionFactory sf = (SessionFactory) sfControl.getMock();
204         MockControl sessionControl = MockControl.createControl(Session.class);
205         Session session = (Session) sessionControl.getMock();
206         MockControl txControl = MockControl.createControl(Transaction.class);
207         Transaction tx = (Transaction) txControl.getMock();
208
209         sf.openSession();
210         sfControl.setReturnValue(session, 1);
211         session.beginTransaction();
212         sessionControl.setReturnValue(tx, 1);
213         session.flush();
214         sessionControl.setVoidCallable(1);
215         session.close();
216         sessionControl.setReturnValue(null, 1);
217         tx.rollback();
218         txControl.setVoidCallable(1);
219         session.connection();
220         sessionControl.setReturnValue(con, 2);
221         con.isReadOnly();
222         conControl.setReturnValue(false, 1);
223         sfControl.replay();
224         sessionControl.replay();
225         txControl.replay();
226
227         PlatformTransactionManager tm = new HibernateTransactionManager(sf);
228         TransactionTemplate tt = new TransactionTemplate(tm);
229         assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
230
231         tt.execute(new TransactionCallback() {
232             public Object JavaDoc doInTransaction(TransactionStatus status) {
233                 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
234                 HibernateTemplate ht = new HibernateTemplate(sf);
235                 ht.setFlushMode(HibernateTemplate.FLUSH_EAGER);
236                 ht.execute(new HibernateCallback() {
237                     public Object JavaDoc doInHibernate(Session session) {
238                         return null;
239                     }
240                 });
241                 status.setRollbackOnly();
242                 return null;
243             }
244         });
245
246         assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
247         sfControl.verify();
248         sessionControl.verify();
249         txControl.verify();
250     }
251
252     public void testParticipatingTransactionWithCommit() throws HibernateException, SQLException JavaDoc {
253         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
254         Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
255         MockControl sfControl = MockControl.createControl(SessionFactory.class);
256         final SessionFactory sf = (SessionFactory) sfControl.getMock();
257         MockControl sessionControl = MockControl.createControl(Session.class);
258         Session session = (Session) sessionControl.getMock();
259         MockControl txControl = MockControl.createControl(Transaction.class);
260         Transaction tx = (Transaction) txControl.getMock();
261
262         sf.openSession();
263         sfControl.setReturnValue(session, 1);
264         session.beginTransaction();
265         sessionControl.setReturnValue(tx, 1);
266         session.flush();
267         sessionControl.setVoidCallable(1);
268         session.close();
269         sessionControl.setReturnValue(null, 1);
270         tx.commit();
271         txControl.setVoidCallable(1);
272         session.connection();
273         sessionControl.setReturnValue(con, 2);
274         con.isReadOnly();
275         conControl.setReturnValue(false, 1);
276         sfControl.replay();
277         sessionControl.replay();
278         txControl.replay();
279
280         PlatformTransactionManager tm = new HibernateTransactionManager(sf);
281         final TransactionTemplate tt = new TransactionTemplate(tm);
282         final List JavaDoc l = new ArrayList JavaDoc();
283         l.add("test");
284
285         Object JavaDoc result = tt.execute(new TransactionCallback() {
286             public Object JavaDoc doInTransaction(TransactionStatus status) {
287                 return tt.execute(new TransactionCallback() {
288                     public Object JavaDoc doInTransaction(TransactionStatus status) {
289                         HibernateTemplate ht = new HibernateTemplate(sf);
290                         ht.setFlushMode(HibernateTemplate.FLUSH_EAGER);
291                         return ht.executeFind(new HibernateCallback() {
292                             public Object JavaDoc doInHibernate(Session session) {
293                                 return l;
294                             }
295                         });
296                     }
297                 });
298             }
299         });
300         assertTrue("Correct result list", result == l);
301
302         sfControl.verify();
303         sessionControl.verify();
304         txControl.verify();
305     }
306
307     public void testParticipatingTransactionWithRollback() throws HibernateException, SQLException JavaDoc {
308         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
309         Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
310         MockControl sfControl = MockControl.createControl(SessionFactory.class);
311         final SessionFactory sf = (SessionFactory) sfControl.getMock();
312         MockControl sessionControl = MockControl.createControl(Session.class);
313         Session session = (Session) sessionControl.getMock();
314         MockControl txControl = MockControl.createControl(Transaction.class);
315         Transaction tx = (Transaction) txControl.getMock();
316
317         sf.openSession();
318         sfControl.setReturnValue(session, 1);
319         session.beginTransaction();
320         sessionControl.setReturnValue(tx, 1);
321         session.close();
322         sessionControl.setReturnValue(null, 1);
323         tx.rollback();
324         txControl.setVoidCallable(1);
325         session.connection();
326         sessionControl.setReturnValue(con, 2);
327         con.isReadOnly();
328         conControl.setReturnValue(false, 1);
329         sfControl.replay();
330         sessionControl.replay();
331         txControl.replay();
332
333         PlatformTransactionManager tm = new HibernateTransactionManager(sf);
334         final TransactionTemplate tt = new TransactionTemplate(tm);
335         try {
336             tt.execute(new TransactionCallback() {
337                 public Object JavaDoc doInTransaction(TransactionStatus status) {
338                     return tt.execute(new TransactionCallback() {
339                         public Object JavaDoc doInTransaction(TransactionStatus status) {
340                             HibernateTemplate ht = new HibernateTemplate(sf);
341                             ht.setFlushMode(HibernateTemplate.FLUSH_EAGER);
342                             return ht.executeFind(new HibernateCallback() {
343                                 public Object JavaDoc doInHibernate(Session session) {
344                                     throw new RuntimeException JavaDoc("application exception");
345                                 }
346                             });
347                         }
348                     });
349                 }
350             });
351             fail("Should have thrown RuntimeException");
352         }
353         catch (RuntimeException JavaDoc ex) {
354             // expected
355
}
356
357         sfControl.verify();
358         sessionControl.verify();
359         txControl.verify();
360     }
361
362     public void testParticipatingTransactionWithRollbackOnly() throws HibernateException, SQLException JavaDoc {
363         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
364         Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
365         MockControl sfControl = MockControl.createControl(SessionFactory.class);
366         final SessionFactory sf = (SessionFactory) sfControl.getMock();
367         MockControl sessionControl = MockControl.createControl(Session.class);
368         Session session = (Session) sessionControl.getMock();
369         MockControl txControl = MockControl.createControl(Transaction.class);
370         Transaction tx = (Transaction) txControl.getMock();
371
372         sf.openSession();
373         sfControl.setReturnValue(session, 1);
374         session.beginTransaction();
375         sessionControl.setReturnValue(tx, 1);
376         session.close();
377         sessionControl.setReturnValue(null, 1);
378         tx.rollback();
379         txControl.setVoidCallable(1);
380         session.connection();
381         sessionControl.setReturnValue(con, 2);
382         con.isReadOnly();
383         conControl.setReturnValue(false, 1);
384         sfControl.replay();
385         sessionControl.replay();
386         txControl.replay();
387
388         PlatformTransactionManager tm = new HibernateTransactionManager(sf);
389         final TransactionTemplate tt = new TransactionTemplate(tm);
390         final List JavaDoc l = new ArrayList JavaDoc();
391         l.add("test");
392
393         try {
394             tt.execute(new TransactionCallback() {
395                 public Object JavaDoc doInTransaction(TransactionStatus status) {
396                     return tt.execute(new TransactionCallback() {
397                         public Object JavaDoc doInTransaction(TransactionStatus status) {
398                             HibernateTemplate ht = new HibernateTemplate(sf);
399                             ht.execute(new HibernateCallback() {
400                                 public Object JavaDoc doInHibernate(Session session) {
401                                     return l;
402                                 }
403                             });
404                             status.setRollbackOnly();
405                             return null;
406                         }
407                     });
408                 }
409             });
410             fail("Should have thrown UnexpectedRollbackException");
411         }
412         catch (UnexpectedRollbackException ex) {
413             // expected
414
}
415
416         sfControl.verify();
417         sessionControl.verify();
418         txControl.verify();
419     }
420
421     public void testParticipatingTransactionWithWithRequiresNew() throws HibernateException, SQLException JavaDoc {
422         MockControl sfControl = MockControl.createControl(SessionFactory.class);
423         final SessionFactory sf = (SessionFactory) sfControl.getMock();
424         MockControl session1Control = MockControl.createControl(Session.class);
425         Session session1 = (Session) session1Control.getMock();
426         MockControl session2Control = MockControl.createControl(Session.class);
427         Session session2 = (Session) session2Control.getMock();
428         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
429         Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
430         MockControl txControl = MockControl.createControl(Transaction.class);
431         Transaction tx = (Transaction) txControl.getMock();
432
433         sf.openSession();
434         sfControl.setReturnValue(session1, 1);
435         sf.openSession();
436         sfControl.setReturnValue(session2, 1);
437         session1.beginTransaction();
438         session1Control.setReturnValue(tx, 1);
439         session2.beginTransaction();
440         session2Control.setReturnValue(tx, 1);
441         session2.flush();
442         session2Control.setVoidCallable(1);
443         session1.close();
444         session1Control.setReturnValue(null, 1);
445         session2.close();
446         session2Control.setReturnValue(null, 1);
447         tx.commit();
448         txControl.setVoidCallable(2);
449         session1.connection();
450         session1Control.setReturnValue(con, 2);
451         session2.connection();
452         session2Control.setReturnValue(con, 2);
453         con.isReadOnly();
454         conControl.setReturnValue(false, 2);
455         sfControl.replay();
456         session1Control.replay();
457         session2Control.replay();
458         conControl.replay();
459         txControl.replay();
460
461         PlatformTransactionManager tm = new HibernateTransactionManager(sf);
462         final TransactionTemplate tt = new TransactionTemplate(tm);
463         tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
464
465         assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
466         tt.execute(new TransactionCallback() {
467             public Object JavaDoc doInTransaction(TransactionStatus status) {
468                 final SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sf);
469                 assertTrue("Has thread session", holder != null);
470                 assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
471                 assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
472                 tt.execute(new TransactionCallback() {
473                     public Object JavaDoc doInTransaction(TransactionStatus status) {
474                         HibernateTemplate ht = new HibernateTemplate(sf);
475                         ht.setFlushMode(HibernateTemplate.FLUSH_EAGER);
476                         return ht.executeFind(new HibernateCallback() {
477                             public Object JavaDoc doInHibernate(Session session) {
478                                 assertTrue("Not enclosing session", session != holder.getSession());
479                                 assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
480                                 assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
481                                 return null;
482                             }
483                         });
484                     }
485                 });
486                 assertTrue("Same thread session as before",
487                         holder.getSession() == SessionFactoryUtils.getSession(sf, false));
488                 assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
489                 assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
490                 return null;
491             }
492         });
493         assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
494
495         sfControl.verify();
496         session1Control.verify();
497         session2Control.verify();
498         conControl.verify();
499         txControl.verify();
500     }
501
502     public void testParticipatingTransactionWithWithNotSupported() throws HibernateException, SQLException JavaDoc {
503         MockControl sfControl = MockControl.createControl(SessionFactory.class);
504         final SessionFactory sf = (SessionFactory) sfControl.getMock();
505         MockControl sessionControl = MockControl.createControl(Session.class);
506         Session session = (Session) sessionControl.getMock();
507         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
508         Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
509         MockControl txControl = MockControl.createControl(Transaction.class);
510         Transaction tx = (Transaction) txControl.getMock();
511
512         sf.openSession();
513         sfControl.setReturnValue(session, 2);
514         session.getSessionFactory();
515         sessionControl.setReturnValue(sf, 1);
516         session.beginTransaction();
517         sessionControl.setReturnValue(tx, 1);
518         session.getFlushMode();
519         sessionControl.setReturnValue(FlushMode.AUTO, 1);
520         session.flush();
521         sessionControl.setVoidCallable(2);
522         session.close();
523         sessionControl.setReturnValue(null, 2);
524         tx.commit();
525         txControl.setVoidCallable(1);
526         session.connection();
527         sessionControl.setReturnValue(con, 2);
528         con.isReadOnly();
529         conControl.setReturnValue(false, 1);
530         sfControl.replay();
531         sessionControl.replay();
532         conControl.replay();
533         txControl.replay();
534
535         HibernateTransactionManager tm = new HibernateTransactionManager(sf);
536         final TransactionTemplate tt = new TransactionTemplate(tm);
537         tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
538
539         assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
540         tt.execute(new TransactionCallback() {
541             public Object JavaDoc doInTransaction(TransactionStatus status) {
542                 SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sf);
543                 assertTrue("Has thread session", holder != null);
544                 assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
545                 assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
546                 tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
547                 tt.execute(new TransactionCallback() {
548                     public Object JavaDoc doInTransaction(TransactionStatus status) {
549                         assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
550                         assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
551                         assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
552                         HibernateTemplate ht = new HibernateTemplate(sf);
553                         ht.setFlushMode(HibernateTemplate.FLUSH_EAGER);
554                         return ht.executeFind(new HibernateCallback() {
555                             public Object JavaDoc doInHibernate(Session session) {
556                                 return null;
557                             }
558                         });
559                     }
560                 });
561                 assertTrue("Same thread session as before",
562                         holder.getSession() == SessionFactoryUtils.getSession(sf, false));
563                 assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
564                 assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
565                 return null;
566             }
567         });
568         assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
569
570         sfControl.verify();
571         sessionControl.verify();
572         txControl.verify();
573     }
574
575     public void testTransactionWithPropagationSupports() throws HibernateException, SQLException JavaDoc {
576         MockControl sfControl = MockControl.createControl(SessionFactory.class);
577         final SessionFactory sf = (SessionFactory) sfControl.getMock();
578         MockControl sessionControl = MockControl.createControl(Session.class);
579         Session session = (Session) sessionControl.getMock();
580         sf.openSession();
581         sfControl.setReturnValue(session, 1);
582         session.getSessionFactory();
583         sessionControl.setReturnValue(sf, 1);
584         session.getFlushMode();
585         sessionControl.setReturnValue(FlushMode.NEVER, 1);
586         session.flush();
587         sessionControl.setVoidCallable(1);
588         session.close();
589         sessionControl.setReturnValue(null, 1);
590         sfControl.replay();
591         sessionControl.replay();
592
593         PlatformTransactionManager tm = new HibernateTransactionManager(sf);
594         TransactionTemplate tt = new TransactionTemplate(tm);
595         tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
596         assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
597
598         tt.execute(new TransactionCallback() {
599             public Object JavaDoc doInTransaction(TransactionStatus status) {
600                 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
601                 assertTrue("Is not new transaction", !status.isNewTransaction());
602                 assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
603                 assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
604                 HibernateTemplate ht = new HibernateTemplate(sf);
605                 ht.setFlushMode(HibernateTemplate.FLUSH_EAGER);
606                 ht.execute(new HibernateCallback() {
607                     public Object JavaDoc doInHibernate(Session session) {
608                         return null;
609                     }
610                 });
611                 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
612                 return null;
613             }
614         });
615
616         assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
617         sfControl.verify();
618         sessionControl.verify();
619     }
620
621     public void testTransactionCommitWithEntityInterceptor() throws HibernateException, SQLException JavaDoc {
622         MockControl interceptorControl = MockControl.createControl(net.sf.hibernate.Interceptor.class);
623         Interceptor entityInterceptor = (Interceptor) interceptorControl.getMock();
624         interceptorControl.replay();
625         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
626         Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
627         MockControl sfControl = MockControl.createControl(SessionFactory.class);
628         final SessionFactory sf = (SessionFactory) sfControl.getMock();
629         MockControl sessionControl = MockControl.createControl(Session.class);
630         Session session = (Session) sessionControl.getMock();
631         MockControl txControl = MockControl.createControl(Transaction.class);
632         Transaction tx = (Transaction) txControl.getMock();
633
634         sf.openSession(entityInterceptor);
635         sfControl.setReturnValue(session, 1);
636         session.beginTransaction();
637         sessionControl.setReturnValue(tx, 1);
638         session.close();
639         sessionControl.setReturnValue(null, 1);
640         tx.commit();
641         txControl.setVoidCallable(1);
642         session.connection();
643         sessionControl.setReturnValue(con, 2);
644         con.isReadOnly();
645         conControl.setReturnValue(false, 1);
646         sfControl.replay();
647         sessionControl.replay();
648         txControl.replay();
649         conControl.replay();
650
651         HibernateTransactionManager tm = new HibernateTransactionManager(sf);
652         tm.setEntityInterceptor(entityInterceptor);
653         TransactionTemplate tt = new TransactionTemplate(tm);
654         tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
655         final List JavaDoc l = new ArrayList JavaDoc();
656         l.add("test");
657         assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
658         assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
659
660         Object JavaDoc result = tt.execute(new TransactionCallback() {
661             public Object JavaDoc doInTransaction(TransactionStatus status) {
662                 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
663                 HibernateTemplate ht = new HibernateTemplate(sf);
664                 return ht.executeFind(new HibernateCallback() {
665                     public Object JavaDoc doInHibernate(Session session) throws HibernateException {
666                         return l;
667                     }
668                 });
669             }
670         });
671         assertTrue("Correct result list", result == l);
672
673         assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
674         assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
675         interceptorControl.verify();
676         sfControl.verify();
677         sessionControl.verify();
678         txControl.verify();
679         conControl.verify();
680     }
681
682     public void testTransactionCommitWithEntityInterceptorBeanName() throws HibernateException, SQLException JavaDoc {
683         MockControl interceptorControl = MockControl.createControl(net.sf.hibernate.Interceptor.class);
684         Interceptor entityInterceptor = (Interceptor) interceptorControl.getMock();
685         interceptorControl.replay();
686         MockControl interceptor2Control = MockControl.createControl(net.sf.hibernate.Interceptor.class);
687         Interceptor entityInterceptor2 = (Interceptor) interceptor2Control.getMock();
688         interceptor2Control.replay();
689
690         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
691         Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
692         MockControl sfControl = MockControl.createControl(SessionFactory.class);
693         final SessionFactory sf = (SessionFactory) sfControl.getMock();
694         MockControl sessionControl = MockControl.createControl(Session.class);
695         Session session = (Session) sessionControl.getMock();
696         MockControl txControl = MockControl.createControl(Transaction.class);
697         Transaction tx = (Transaction) txControl.getMock();
698
699         sf.openSession(entityInterceptor);
700         sfControl.setReturnValue(session, 1);
701         sf.openSession(entityInterceptor2);
702         sfControl.setReturnValue(session, 1);
703         session.beginTransaction();
704         sessionControl.setReturnValue(tx, 2);
705         session.close();
706         sessionControl.setReturnValue(null, 2);
707         tx.commit();
708         txControl.setVoidCallable(2);
709         session.connection();
710         sessionControl.setReturnValue(con, 4);
711         con.isReadOnly();
712         conControl.setReturnValue(false, 2);
713         sfControl.replay();
714         sessionControl.replay();
715         txControl.replay();
716         conControl.replay();
717
718         MockControl beanFactoryControl = MockControl.createControl(BeanFactory.class);
719         BeanFactory beanFactory = (BeanFactory) beanFactoryControl.getMock();
720         beanFactory.getBean("entityInterceptor", Interceptor.class);
721         beanFactoryControl.setReturnValue(entityInterceptor, 1);
722         beanFactory.getBean("entityInterceptor", Interceptor.class);
723         beanFactoryControl.setReturnValue(entityInterceptor2, 1);
724         beanFactoryControl.replay();
725         
726         HibernateTransactionManager tm = new HibernateTransactionManager(sf);
727         tm.setEntityInterceptorBeanName("entityInterceptor");
728         tm.setBeanFactory(beanFactory);
729
730         TransactionTemplate tt = new TransactionTemplate(tm);
731         tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
732         assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
733         assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
734
735         for (int i = 0; i < 2; i++) {
736             tt.execute(new TransactionCallbackWithoutResult() {
737                 public void doInTransactionWithoutResult(TransactionStatus status) {
738                     assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
739                     HibernateTemplate ht = new HibernateTemplate(sf);
740                     ht.execute(new HibernateCallback() {
741                         public Object JavaDoc doInHibernate(Session session) throws HibernateException {
742                             return null;
743                         }
744                     });
745                 }
746             });
747         }
748
749         assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
750         assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
751         interceptorControl.verify();
752         interceptor2Control.verify();
753         sfControl.verify();
754         sessionControl.verify();
755         txControl.verify();
756         conControl.verify();
757         beanFactoryControl.verify();
758     }
759
760     public void testTransactionCommitWithReadOnly() throws HibernateException, SQLException JavaDoc {
761         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
762         Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
763         MockControl sfControl = MockControl.createControl(SessionFactory.class);
764         final SessionFactory sf = (SessionFactory) sfControl.getMock();
765         MockControl sessionControl = MockControl.createControl(Session.class);
766         Session session = (Session) sessionControl.getMock();
767         MockControl txControl = MockControl.createControl(Transaction.class);
768         Transaction tx = (Transaction) txControl.getMock();
769
770         sf.openSession();
771         sfControl.setReturnValue(session, 1);
772         session.beginTransaction();
773         sessionControl.setReturnValue(tx, 1);
774         session.setFlushMode(FlushMode.NEVER);
775         sessionControl.setVoidCallable(1);
776         session.connection();
777         sessionControl.setReturnValue(con, 2);
778         session.close();
779         sessionControl.setReturnValue(null, 1);
780         tx.commit();
781         txControl.setVoidCallable(1);
782         con.setReadOnly(true);
783         conControl.setVoidCallable(1);
784         con.isReadOnly();
785         conControl.setReturnValue(true, 1);
786         con.setReadOnly(false);
787         conControl.setVoidCallable(1);
788         sfControl.replay();
789         sessionControl.replay();
790         txControl.replay();
791         conControl.replay();
792
793         HibernateTransactionManager tm = new HibernateTransactionManager(sf);
794         TransactionTemplate tt = new TransactionTemplate(tm);
795         tt.setReadOnly(true);
796         final List JavaDoc l = new ArrayList JavaDoc();
797         l.add("test");
798         assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
799         assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
800
801         Object JavaDoc result = tt.execute(new TransactionCallback() {
802             public Object JavaDoc doInTransaction(TransactionStatus status) {
803                 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
804                 assertTrue(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
805                 assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
806                 HibernateTemplate ht = new HibernateTemplate(sf);
807                 return ht.executeFind(new HibernateCallback() {
808                     public Object JavaDoc doInHibernate(Session session) throws HibernateException {
809                         return l;
810                     }
811                 });
812             }
813         });
814         assertTrue("Correct result list", result == l);
815
816         assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
817         assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
818         sfControl.verify();
819         sessionControl.verify();
820         txControl.verify();
821         conControl.verify();
822     }
823
824     public void testTransactionCommitWithFlushFailure() throws HibernateException, SQLException JavaDoc {
825         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
826         Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
827         MockControl sfControl = MockControl.createControl(SessionFactory.class);
828         final SessionFactory sf = (SessionFactory) sfControl.getMock();
829         MockControl sessionControl = MockControl.createControl(Session.class);
830         Session session = (Session) sessionControl.getMock();
831         MockControl txControl = MockControl.createControl(Transaction.class);
832         Transaction tx = (Transaction) txControl.getMock();
833
834         sf.openSession();
835         sfControl.setReturnValue(session, 1);
836         session.beginTransaction();
837         sessionControl.setReturnValue(tx, 1);
838         tx.commit();
839         SQLException JavaDoc sqlEx = new SQLException JavaDoc("argh", "27");
840         txControl.setThrowable(new JDBCException("mymsg", sqlEx), 1);
841         session.close();
842         sessionControl.setReturnValue(null, 1);
843         tx.rollback();
844         txControl.setVoidCallable(1);
845         session.connection();
846         sessionControl.setReturnValue(con, 2);
847         con.isReadOnly();
848         conControl.setReturnValue(false, 1);
849
850         sfControl.replay();
851         sessionControl.replay();
852         txControl.replay();
853         conControl.replay();
854
855         HibernateTransactionManager tm = new HibernateTransactionManager(sf);
856         TransactionTemplate tt = new TransactionTemplate(tm);
857         final List JavaDoc l = new ArrayList JavaDoc();
858         l.add("test");
859         assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
860         assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
861
862         try {
863             tt.execute(new TransactionCallback() {
864                 public Object JavaDoc doInTransaction(TransactionStatus status) {
865                     assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
866                     HibernateTemplate ht = new HibernateTemplate(sf);
867                     return ht.executeFind(new HibernateCallback() {
868                         public Object JavaDoc doInHibernate(Session session) throws HibernateException {
869                             return l;
870                         }
871                     });
872                 }
873             });
874             fail("Should have thrown DataIntegrityViolationException");
875         }
876         catch (DataIntegrityViolationException ex) {
877             // expected
878
assertEquals(sqlEx, ex.getCause());
879             assertTrue(ex.getMessage().indexOf("mymsg") != -1);
880         }
881
882         assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
883         assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
884         sfControl.verify();
885         sessionControl.verify();
886         txControl.verify();
887         conControl.verify();
888     }
889
890     public void testTransactionCommitWithPreBound() throws HibernateException, SQLException JavaDoc {
891         MockControl dsControl = MockControl.createControl(DataSource JavaDoc.class);
892         final DataSource JavaDoc ds = (DataSource JavaDoc) dsControl.getMock();
893         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
894         Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
895         MockControl sfControl = MockControl.createControl(SessionFactory.class);
896         final SessionFactory sf = (SessionFactory) sfControl.getMock();
897         MockControl sessionControl = MockControl.createControl(Session.class);
898         final Session session = (Session) sessionControl.getMock();
899         MockControl txControl = MockControl.createControl(Transaction.class);
900         Transaction tx = (Transaction) txControl.getMock();
901
902         session.beginTransaction();
903         sessionControl.setReturnValue(tx, 1);
904         session.getFlushMode();
905         sessionControl.setReturnValue(FlushMode.NEVER, 1);
906         session.setFlushMode(FlushMode.AUTO);
907         sessionControl.setVoidCallable(1);
908         session.setFlushMode(FlushMode.NEVER);
909         sessionControl.setVoidCallable(1);
910         session.connection();
911         sessionControl.setReturnValue(con, 2);
912         con.getTransactionIsolation();
913         conControl.setReturnValue(Connection.TRANSACTION_READ_COMMITTED);
914         con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
915         conControl.setVoidCallable(1);
916         con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
917         conControl.setVoidCallable(1);
918         tx.commit();
919         txControl.setVoidCallable(1);
920         con.isReadOnly();
921         conControl.setReturnValue(false, 1);
922         dsControl.replay();
923         conControl.replay();
924         sfControl.replay();
925         sessionControl.replay();
926         txControl.replay();
927
928         HibernateTransactionManager tm = new HibernateTransactionManager();
929         tm.setSessionFactory(sf);
930         tm.setDataSource(ds);
931         TransactionTemplate tt = new TransactionTemplate(tm);
932         tt.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
933         final List JavaDoc l = new ArrayList JavaDoc();
934         l.add("test");
935         assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
936         assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
937         TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
938         assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
939
940         Object JavaDoc result = tt.execute(new TransactionCallback() {
941             public Object JavaDoc doInTransaction(TransactionStatus status) {
942                 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
943                 assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(ds));
944                 SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sf);
945                 assertTrue("Has thread transaction", sessionHolder.getTransaction() != null);
946                 HibernateTemplate ht = new HibernateTemplate(sf);
947                 ht.setExposeNativeSession(true);
948                 return ht.executeFind(new HibernateCallback() {
949                     public Object JavaDoc doInHibernate(Session sess) throws HibernateException {
950                         assertEquals(session, sess);
951                         return l;
952                     }
953                 });
954             }
955         });
956         assertTrue("Correct result list", result == l);
957
958         assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
959         SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sf);
960         assertTrue("Hasn't thread transaction", sessionHolder.getTransaction() == null);
961         TransactionSynchronizationManager.unbindResource(sf);
962         assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
963         assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
964         dsControl.verify();
965         conControl.verify();
966         sfControl.verify();
967         sessionControl.verify();
968         txControl.verify();
969     }
970
971     public void testTransactionRollbackWithPreBound() throws HibernateException, SQLException JavaDoc {
972         MockControl dsControl = MockControl.createControl(DataSource JavaDoc.class);
973         final DataSource JavaDoc ds = (DataSource JavaDoc) dsControl.getMock();
974         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
975         Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
976         MockControl sfControl = MockControl.createControl(SessionFactory.class);
977         final SessionFactory sf = (SessionFactory) sfControl.getMock();
978         MockControl sessionControl = MockControl.createControl(Session.class);
979         final Session session = (Session) sessionControl.getMock();
980         MockControl tx1Control = MockControl.createControl(Transaction.class);
981         final Transaction tx1 = (Transaction) tx1Control.getMock();
982         MockControl tx2Control = MockControl.createControl(Transaction.class);
983         final Transaction tx2 = (Transaction) tx2Control.getMock();
984
985         session.beginTransaction();
986         sessionControl.setReturnValue(tx1, 1);
987         tx1.rollback();
988         tx1Control.setVoidCallable(1);
989         session.clear();
990         sessionControl.setVoidCallable(1);
991         session.beginTransaction();
992         sessionControl.setReturnValue(tx2, 1);
993         tx2.commit();
994         tx2Control.setVoidCallable(1);
995
996         session.getFlushMode();
997         sessionControl.setReturnValue(FlushMode.NEVER, 2);
998         session.setFlushMode(FlushMode.AUTO);
999         sessionControl.setVoidCallable(2);
1000        session.setFlushMode(FlushMode.NEVER);
1001        sessionControl.setVoidCallable(2);
1002        session.connection();
1003        sessionControl.setReturnValue(con, 4);
1004        con.isReadOnly();
1005        conControl.setReturnValue(false, 2);
1006
1007        dsControl.replay();
1008        conControl.replay();
1009        sfControl.replay();
1010        sessionControl.replay();
1011        tx1Control.replay();
1012        tx2Control.replay();
1013
1014        HibernateTransactionManager tm = new HibernateTransactionManager();
1015        tm.setSessionFactory(sf);
1016        tm.setDataSource(ds);
1017        final TransactionTemplate tt = new TransactionTemplate(tm);
1018        assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
1019        assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
1020        TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
1021        assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
1022
1023        try {
1024            tt.execute(new TransactionCallbackWithoutResult() {
1025                public void doInTransactionWithoutResult(TransactionStatus status) {
1026                    assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
1027                    assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(ds));
1028                    SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sf);
1029                    assertEquals(tx1, sessionHolder.getTransaction());
1030                    tt.execute(new TransactionCallbackWithoutResult() {
1031                        public void doInTransactionWithoutResult(TransactionStatus status) {
1032                            status.setRollbackOnly();
1033                            HibernateTemplate ht = new HibernateTemplate(sf);
1034                            ht.setExposeNativeSession(true);
1035                            ht.execute(new HibernateCallback() {
1036                                public Object JavaDoc doInHibernate(Session sess) throws HibernateException {
1037                                    assertEquals(session, sess);
1038                                    return null;
1039                                }
1040                            });
1041                        }
1042                    });
1043                }
1044            });
1045            fail("Should have thrown UnexpectedRollbackException");
1046        }
1047        catch (UnexpectedRollbackException ex) {
1048            // expected
1049
}
1050
1051        assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
1052        SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sf);
1053        assertTrue("Hasn't thread transaction", sessionHolder.getTransaction() == null);
1054        assertTrue("Not marked rollback-only", !sessionHolder.isRollbackOnly());
1055
1056        tt.execute(new TransactionCallbackWithoutResult() {
1057            public void doInTransactionWithoutResult(TransactionStatus status) {
1058                assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
1059                assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(ds));
1060                SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sf);
1061                assertEquals(tx2, sessionHolder.getTransaction());
1062                HibernateTemplate ht = new HibernateTemplate(sf);
1063                ht.setExposeNativeSession(true);
1064                ht.execute(new HibernateCallback() {
1065                    public Object JavaDoc doInHibernate(Session sess) throws HibernateException {
1066                        assertEquals(session, sess);
1067                        return null;
1068                    }
1069                });
1070            }
1071        });
1072
1073        assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
1074        assertTrue("Hasn't thread transaction", sessionHolder.getTransaction() == null);
1075        TransactionSynchronizationManager.unbindResource(sf);
1076        assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
1077        assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
1078
1079        dsControl.verify();
1080        conControl.verify();
1081        sfControl.verify();
1082        sessionControl.verify();
1083        tx1Control.verify();
1084        tx2Control.verify();
1085    }
1086
1087    public void testExistingTransactionWithPropagationNestedAndRollback() throws SQLException JavaDoc, HibernateException {
1088        doTestExistingTransactionWithPropagationNestedAndRollback(false);
1089    }
1090
1091    public void testExistingTransactionWithManualSavepointAndRollback() throws SQLException JavaDoc, HibernateException {
1092        doTestExistingTransactionWithPropagationNestedAndRollback(true);
1093    }
1094
1095    private void doTestExistingTransactionWithPropagationNestedAndRollback(final boolean manualSavepoint)
1096            throws SQLException JavaDoc, HibernateException {
1097
1098        if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_14) {
1099            return;
1100        }
1101
1102        MockControl dsControl = MockControl.createControl(DataSource JavaDoc.class);
1103        final DataSource JavaDoc ds = (DataSource JavaDoc) dsControl.getMock();
1104        MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
1105        Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
1106        MockControl mdControl = MockControl.createControl(DatabaseMetaData JavaDoc.class);
1107        DatabaseMetaData JavaDoc md = (DatabaseMetaData JavaDoc) mdControl.getMock();
1108        MockControl spControl = MockControl.createControl(Savepoint JavaDoc.class);
1109        Savepoint JavaDoc sp = (Savepoint JavaDoc) spControl.getMock();
1110        MockControl sfControl = MockControl.createControl(SessionFactory.class);
1111        final SessionFactory sf = (SessionFactory) sfControl.getMock();
1112        MockControl sessionControl = MockControl.createControl(Session.class);
1113        Session session = (Session) sessionControl.getMock();
1114        MockControl txControl = MockControl.createControl(Transaction.class);
1115        Transaction tx = (Transaction) txControl.getMock();
1116        MockControl queryControl = MockControl.createControl(Query.class);
1117        Query query = (Query) queryControl.getMock();
1118
1119        final List JavaDoc list = new ArrayList JavaDoc();
1120        list.add("test");
1121        con.isReadOnly();
1122        conControl.setReturnValue(false, 1);
1123        sf.openSession();
1124        sfControl.setReturnValue(session, 1);
1125        session.beginTransaction();
1126        sessionControl.setReturnValue(tx, 1);
1127        session.connection();
1128        sessionControl.setReturnValue(con, 2);
1129        md.supportsSavepoints();
1130        mdControl.setReturnValue(true, 1);
1131        con.getMetaData();
1132        conControl.setReturnValue(md, 1);
1133        con.setSavepoint();
1134        conControl.setReturnValue(sp, 1);
1135        con.rollback(sp);
1136        conControl.setVoidCallable(1);
1137        session.createQuery("some query string");
1138        sessionControl.setReturnValue(query, 1);
1139        query.list();
1140        queryControl.setReturnValue(list, 1);
1141        session.close();
1142        sessionControl.setReturnValue(null, 1);
1143        tx.commit();
1144        txControl.setVoidCallable(1);
1145        dsControl.replay();
1146        conControl.replay();
1147        mdControl.replay();
1148        spControl.replay();
1149        sfControl.replay();
1150        sessionControl.replay();
1151        txControl.replay();
1152        queryControl.replay();
1153
1154        HibernateTransactionManager tm = new HibernateTransactionManager();
1155        tm.setJdbcExceptionTranslator(new SQLStateSQLExceptionTranslator());
1156        tm.setNestedTransactionAllowed(true);
1157        tm.setSessionFactory(sf);
1158        tm.setDataSource(ds);
1159        final TransactionTemplate tt = new TransactionTemplate(tm);
1160        tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
1161        assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
1162        assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
1163        assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
1164
1165        Object JavaDoc result = tt.execute(new TransactionCallback() {
1166            public Object JavaDoc doInTransaction(TransactionStatus status) {
1167                assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
1168                assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(ds));
1169                if (manualSavepoint) {
1170                    Object JavaDoc savepoint = status.createSavepoint();
1171                    status.rollbackToSavepoint(savepoint);
1172                }
1173                else {
1174                    tt.execute(new TransactionCallbackWithoutResult() {
1175                        protected void doInTransactionWithoutResult(TransactionStatus status) {
1176                            assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
1177                            assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(ds));
1178                            status.setRollbackOnly();
1179                        }
1180                    });
1181                }
1182                HibernateTemplate ht = new HibernateTemplate(sf);
1183                return ht.find("some query string");
1184            }
1185        });
1186        assertTrue("Correct result list", result == list);
1187
1188        assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
1189        assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
1190        assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
1191        dsControl.verify();
1192        conControl.verify();
1193        mdControl.verify();
1194        spControl.verify();
1195        sfControl.verify();
1196        sessionControl.verify();
1197        txControl.verify();
1198        queryControl.verify();
1199    }
1200
1201    public void testTransactionCommitWithNonExistingDatabase() throws HibernateException, IOException JavaDoc {
1202        final DriverManagerDataSource ds = new DriverManagerDataSource();
1203        LocalSessionFactoryBean lsfb = new LocalSessionFactoryBean();
1204        lsfb.setDataSource(ds);
1205        lsfb.afterPropertiesSet();
1206        final SessionFactory sf = (SessionFactory) lsfb.getObject();
1207
1208        HibernateTransactionManager tm = new HibernateTransactionManager();
1209        tm.setSessionFactory(sf);
1210        tm.afterPropertiesSet();
1211        TransactionTemplate tt = new TransactionTemplate(tm);
1212        tt.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
1213        tt.setTimeout(10);
1214        assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
1215        assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
1216        assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
1217
1218        try {
1219            tt.execute(new TransactionCallback() {
1220                public Object JavaDoc doInTransaction(TransactionStatus status) {
1221                    assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
1222                    assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(ds));
1223                    HibernateTemplate ht = new HibernateTemplate(sf);
1224                    return ht.find("from java.lang.Object");
1225                }
1226            });
1227            fail("Should have thrown CannotCreateTransactionException");
1228        }
1229        catch (CannotCreateTransactionException ex) {
1230            // expected
1231
}
1232
1233        assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
1234        assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
1235        assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
1236    }
1237
1238    public void testTransactionCommitWithNonExistingDatabaseAndLazyConnection() throws HibernateException, IOException JavaDoc {
1239        DriverManagerDataSource dsTarget = new DriverManagerDataSource();
1240        final LazyConnectionDataSourceProxy ds = new LazyConnectionDataSourceProxy();
1241        ds.setTargetDataSource(dsTarget);
1242        ds.setDefaultAutoCommit(true);
1243        ds.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
1244        
1245        LocalSessionFactoryBean lsfb = new LocalSessionFactoryBean();
1246        lsfb.setDataSource(ds);
1247        lsfb.afterPropertiesSet();
1248        final SessionFactory sf = (SessionFactory) lsfb.getObject();
1249
1250        HibernateTransactionManager tm = new HibernateTransactionManager();
1251        tm.setSessionFactory(sf);
1252        tm.afterPropertiesSet();
1253        TransactionTemplate tt = new TransactionTemplate(tm);
1254        tt.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
1255        tt.setTimeout(10);
1256        assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
1257        assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
1258        assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
1259
1260        tt.execute(new TransactionCallback() {
1261            public Object JavaDoc doInTransaction(TransactionStatus status) {
1262                assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
1263                assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(ds));
1264                HibernateTemplate ht = new HibernateTemplate(sf);
1265                return ht.find("from java.lang.Object");
1266            }
1267        });
1268
1269        assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
1270        assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
1271        assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
1272    }
1273
1274    protected void tearDown() {
1275        assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
1276        assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
1277        assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
1278        assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
1279    }
1280
1281}
1282
Popular Tags