1 16 17 package org.springframework.orm.hibernate; 18 19 import java.util.ArrayList ; 20 import java.util.List ; 21 22 import javax.transaction.RollbackException ; 23 import javax.transaction.Status ; 24 import javax.transaction.Synchronization ; 25 import javax.transaction.TransactionManager ; 26 import javax.transaction.UserTransaction ; 27 28 import junit.framework.TestCase; 29 import net.sf.hibernate.FlushMode; 30 import net.sf.hibernate.HibernateException; 31 import net.sf.hibernate.Session; 32 import net.sf.hibernate.SessionFactory; 33 import net.sf.hibernate.engine.SessionFactoryImplementor; 34 import net.sf.hibernate.engine.SessionImplementor; 35 import org.easymock.MockControl; 36 37 import org.springframework.dao.DataAccessException; 38 import org.springframework.transaction.MockJtaTransaction; 39 import org.springframework.transaction.TransactionDefinition; 40 import org.springframework.transaction.TransactionStatus; 41 import org.springframework.transaction.UnexpectedRollbackException; 42 import org.springframework.transaction.jta.JtaTransactionManager; 43 import org.springframework.transaction.support.TransactionCallback; 44 import org.springframework.transaction.support.TransactionCallbackWithoutResult; 45 import org.springframework.transaction.support.TransactionSynchronizationManager; 46 import org.springframework.transaction.support.TransactionTemplate; 47 48 52 public class HibernateJtaTransactionTests extends TestCase { 53 54 public void testParticipatingJtaTransactionWithWithRequiresNew() throws Exception { 55 MockControl utControl = MockControl.createControl(UserTransaction .class); 56 UserTransaction ut = (UserTransaction ) utControl.getMock(); 57 MockControl tmControl = MockControl.createControl(TransactionManager .class); 58 TransactionManager tm = (TransactionManager ) tmControl.getMock(); 59 MockControl tx1Control = MockControl.createControl(javax.transaction.Transaction .class); 60 javax.transaction.Transaction tx1 = (javax.transaction.Transaction ) tx1Control.getMock(); 61 MockControl sfControl = MockControl.createControl(SessionFactory.class); 62 final SessionFactory sf = (SessionFactory) sfControl.getMock(); 63 MockControl session1Control = MockControl.createControl(Session.class); 64 Session session1 = (Session) session1Control.getMock(); 65 MockControl session2Control = MockControl.createControl(Session.class); 66 Session session2 = (Session) session2Control.getMock(); 67 68 ut.getStatus(); 69 utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1); 70 ut.getStatus(); 71 utControl.setReturnValue(Status.STATUS_ACTIVE, 3); 72 ut.begin(); 73 utControl.setVoidCallable(2); 74 tm.suspend(); 75 tmControl.setReturnValue(tx1, 1); 76 tm.resume(tx1); 77 tmControl.setVoidCallable(1); 78 ut.commit(); 79 utControl.setVoidCallable(2); 80 81 sf.openSession(); 82 sfControl.setReturnValue(session1, 1); 83 sf.openSession(); 84 sfControl.setReturnValue(session2, 1); 85 session1.getSessionFactory(); 86 session1Control.setReturnValue(sf, 1); 87 session2.getSessionFactory(); 88 session2Control.setReturnValue(sf, 1); 89 session1.getFlushMode(); 90 session1Control.setReturnValue(FlushMode.AUTO, 1); 91 session2.getFlushMode(); 92 session2Control.setReturnValue(FlushMode.AUTO, 1); 93 session1.flush(); 94 session1Control.setVoidCallable(1); 95 session2.flush(); 96 session2Control.setVoidCallable(2); 97 session1.close(); 98 session1Control.setReturnValue(null, 1); 99 session2.close(); 100 session2Control.setReturnValue(null, 1); 101 102 utControl.replay(); 103 tmControl.replay(); 104 sfControl.replay(); 105 session1Control.replay(); 106 session2Control.replay(); 107 108 JtaTransactionManager ptm = new JtaTransactionManager(); 109 ptm.setUserTransaction(ut); 110 ptm.setTransactionManager(tm); 111 final TransactionTemplate tt = new TransactionTemplate(ptm); 112 tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); 113 114 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 115 tt.execute(new TransactionCallback() { 116 public Object doInTransaction(TransactionStatus status) { 117 SessionFactoryUtils.getSession(sf, true); 118 final SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sf); 119 assertTrue("Has thread session", holder != null); 120 tt.execute(new TransactionCallback() { 121 public Object doInTransaction(TransactionStatus status) { 122 HibernateTemplate ht = new HibernateTemplate(sf); 123 ht.setFlushMode(HibernateTemplate.FLUSH_EAGER); 124 return ht.executeFind(new HibernateCallback() { 125 public Object doInHibernate(Session session) { 126 assertTrue("Not enclosing session", session != holder.getSession()); 127 return null; 128 } 129 }); 130 } 131 }); 132 assertTrue("Same thread session as before", 133 holder.getSession() == SessionFactoryUtils.getSession(sf, false)); 134 return null; 135 } 136 }); 137 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 138 139 utControl.verify(); 140 tmControl.verify(); 141 sfControl.verify(); 142 session1Control.verify(); 143 session2Control.verify(); 144 } 145 146 public void testJtaTransactionCommit() throws Exception { 147 doTestJtaTransactionCommit(Status.STATUS_NO_TRANSACTION, false); 148 } 149 150 public void testJtaTransactionCommitWithReadOnly() throws Exception { 151 doTestJtaTransactionCommit(Status.STATUS_NO_TRANSACTION, true); 152 } 153 154 public void testJtaTransactionCommitWithExisting() throws Exception { 155 doTestJtaTransactionCommit(Status.STATUS_ACTIVE, false); 156 } 157 158 public void testJtaTransactionCommitWithExistingAndReadOnly() throws Exception { 159 doTestJtaTransactionCommit(Status.STATUS_ACTIVE, true); 160 } 161 162 private void doTestJtaTransactionCommit(int status, final boolean readOnly) throws Exception { 163 MockControl utControl = MockControl.createControl(UserTransaction .class); 164 UserTransaction ut = (UserTransaction ) utControl.getMock(); 165 ut.getStatus(); 166 utControl.setReturnValue(status, 1); 167 if (status == Status.STATUS_NO_TRANSACTION) { 168 ut.begin(); 169 utControl.setVoidCallable(1); 170 ut.getStatus(); 171 utControl.setReturnValue(Status.STATUS_ACTIVE, 1); 172 ut.commit(); 173 utControl.setVoidCallable(1); 174 } 175 utControl.replay(); 176 177 MockControl sfControl = MockControl.createControl(SessionFactory.class); 178 final SessionFactory sf = (SessionFactory) sfControl.getMock(); 179 final MockControl sessionControl = MockControl.createControl(Session.class); 180 final Session session = (Session) sessionControl.getMock(); 181 sf.openSession(); 182 sfControl.setReturnValue(session, 1); 183 session.getSessionFactory(); 184 sessionControl.setReturnValue(sf, 1); 185 if (readOnly) { 186 session.setFlushMode(FlushMode.NEVER); 187 sessionControl.setVoidCallable(1); 188 } 189 sfControl.replay(); 190 sessionControl.replay(); 191 192 JtaTransactionManager ptm = new JtaTransactionManager(ut); 193 TransactionTemplate tt = new TransactionTemplate(ptm); 194 tt.setReadOnly(readOnly); 195 final List l = new ArrayList (); 196 l.add("test"); 197 assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); 198 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 199 200 Object result = tt.execute(new TransactionCallback() { 201 public Object doInTransaction(TransactionStatus status) { 202 try { 203 assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive()); 204 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 205 HibernateTemplate ht = new HibernateTemplate(sf); 206 ht.setExposeNativeSession(true); 207 List htl = ht.executeFind(new HibernateCallback() { 208 public Object doInHibernate(Session sess) { 209 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 210 assertEquals(session, sess); 211 return l; 212 } 213 }); 214 ht = new HibernateTemplate(sf); 215 ht.setExposeNativeSession(true); 216 htl = ht.executeFind(new HibernateCallback() { 217 public Object doInHibernate(Session sess) { 218 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 219 assertEquals(session, sess); 220 return l; 221 } 222 }); 223 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 224 sessionControl.verify(); 225 sessionControl.reset(); 226 try { 227 if (!readOnly) { 228 session.getFlushMode(); 229 sessionControl.setReturnValue(FlushMode.AUTO, 1); 230 session.flush(); 231 sessionControl.setVoidCallable(1); 232 } 233 session.close(); 234 sessionControl.setReturnValue(null, 1); 235 } 236 catch (HibernateException e) { 237 } 238 sessionControl.replay(); 239 return htl; 240 } 241 catch (Error err) { 242 err.printStackTrace(); 243 throw err; 244 } 245 } 246 }); 247 248 assertTrue("Correct result list", result == l); 249 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 250 assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); 251 252 utControl.verify(); 253 sfControl.verify(); 254 sessionControl.verify(); 255 } 256 257 public void testJtaTransactionCommitWithJtaTm() throws Exception { 258 doTestJtaTransactionCommitWithJtaTm(Status.STATUS_NO_TRANSACTION); 259 } 260 261 public void testJtaTransactionCommitWithJtaTmAndExisting() throws Exception { 262 doTestJtaTransactionCommitWithJtaTm(Status.STATUS_ACTIVE); 263 } 264 265 private void doTestJtaTransactionCommitWithJtaTm(int status) throws Exception { 266 MockControl utControl = MockControl.createControl(UserTransaction .class); 267 UserTransaction ut = (UserTransaction ) utControl.getMock(); 268 ut.getStatus(); 269 utControl.setReturnValue(status, 1); 270 if (status == Status.STATUS_NO_TRANSACTION) { 271 ut.begin(); 272 utControl.setVoidCallable(1); 273 ut.getStatus(); 274 utControl.setReturnValue(Status.STATUS_ACTIVE, 1); 275 ut.commit(); 276 utControl.setVoidCallable(1); 277 } 278 279 MockControl tmControl = MockControl.createControl(TransactionManager .class); 280 TransactionManager tm = (TransactionManager ) tmControl.getMock(); 281 MockJtaTransaction transaction = new MockJtaTransaction(); 282 tm.getStatus(); 283 tmControl.setReturnValue(Status.STATUS_ACTIVE, 6); 284 tm.getTransaction(); 285 tmControl.setReturnValue(transaction, 6); 286 287 MockControl sfControl = MockControl.createControl(SessionFactoryImplementor.class); 288 final SessionFactoryImplementor sf = (SessionFactoryImplementor) sfControl.getMock(); 289 final MockControl sessionControl = MockControl.createControl(SessionImplementor.class); 290 final SessionImplementor session = (SessionImplementor) sessionControl.getMock(); 291 sf.getConnectionProvider(); 292 sfControl.setReturnValue(null, 2); 293 sf.getTransactionManager(); 294 sfControl.setReturnValue(tm, 1); 295 sf.openSession(); 296 sfControl.setReturnValue(session, 1); 297 298 utControl.replay(); 299 tmControl.replay(); 300 sfControl.replay(); 301 sessionControl.replay(); 302 303 JtaTransactionManager ptm = new JtaTransactionManager(ut); 304 TransactionTemplate tt = new TransactionTemplate(ptm); 305 final List l = new ArrayList (); 306 l.add("test"); 307 assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); 308 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 309 310 Object result = tt.execute(new TransactionCallback() { 311 public Object doInTransaction(TransactionStatus status) { 312 try { 313 assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive()); 314 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 315 HibernateTemplate ht = new HibernateTemplate(sf); 316 ht.setExposeNativeSession(true); 317 List htl = ht.executeFind(new HibernateCallback() { 318 public Object doInHibernate(Session sess) { 319 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 320 assertEquals(session, sess); 321 return l; 322 } 323 }); 324 ht = new HibernateTemplate(sf); 325 ht.setExposeNativeSession(true); 326 htl = ht.executeFind(new HibernateCallback() { 327 public Object doInHibernate(Session sess) { 328 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 329 assertEquals(session, sess); 330 return l; 331 } 332 }); 333 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 334 sessionControl.verify(); 335 sessionControl.reset(); 336 try { 337 session.getFlushMode(); 338 sessionControl.setReturnValue(FlushMode.AUTO, 1); 339 session.flush(); 340 sessionControl.setVoidCallable(1); 341 session.close(); 342 sessionControl.setReturnValue(null, 1); 343 } 344 catch (HibernateException e) { 345 } 346 sessionControl.replay(); 347 return htl; 348 } 349 catch (Error err) { 350 err.printStackTrace(); 351 throw err; 352 } 353 } 354 }); 355 356 assertTrue("Correct result list", result == l); 357 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 358 assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); 359 360 utControl.verify(); 361 sfControl.verify(); 362 sessionControl.verify(); 363 } 364 365 public void testJtaTransactionWithFlushFailure() throws Exception { 366 MockControl utControl = MockControl.createControl(UserTransaction .class); 367 UserTransaction ut = (UserTransaction ) utControl.getMock(); 368 ut.getStatus(); 369 utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1); 370 ut.begin(); 371 utControl.setVoidCallable(1); 372 ut.rollback(); 373 utControl.setVoidCallable(1); 374 utControl.replay(); 375 376 MockControl sfControl = MockControl.createControl(SessionFactory.class); 377 final SessionFactory sf = (SessionFactory) sfControl.getMock(); 378 final MockControl sessionControl = MockControl.createControl(Session.class); 379 final Session session = (Session) sessionControl.getMock(); 380 sf.openSession(); 381 sfControl.setReturnValue(session, 1); 382 session.getSessionFactory(); 383 sessionControl.setReturnValue(sf, 1); 384 sfControl.replay(); 385 sessionControl.replay(); 386 387 JtaTransactionManager ptm = new JtaTransactionManager(ut); 388 TransactionTemplate tt = new TransactionTemplate(ptm); 389 final List l = new ArrayList (); 390 l.add("test"); 391 final HibernateException flushEx = new HibernateException("flush failure"); 392 assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); 393 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 394 395 try { 396 tt.execute(new TransactionCallback() { 397 public Object doInTransaction(TransactionStatus status) { 398 try { 399 assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive()); 400 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 401 HibernateTemplate ht = new HibernateTemplate(sf); 402 ht.setExposeNativeSession(true); 403 List htl = ht.executeFind(new HibernateCallback() { 404 public Object doInHibernate(Session sess) { 405 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 406 assertEquals(session, sess); 407 return l; 408 } 409 }); 410 ht = new HibernateTemplate(sf); 411 ht.setExposeNativeSession(true); 412 htl = ht.executeFind(new HibernateCallback() { 413 public Object doInHibernate(Session sess) { 414 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 415 assertEquals(session, sess); 416 return l; 417 } 418 }); 419 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 420 sessionControl.verify(); 421 sessionControl.reset(); 422 try { 423 session.getFlushMode(); 424 sessionControl.setReturnValue(FlushMode.AUTO, 1); 425 session.flush(); 426 sessionControl.setThrowable(flushEx); 427 session.close(); 428 sessionControl.setReturnValue(null, 1); 429 } 430 catch (HibernateException e) { 431 } 432 sessionControl.replay(); 433 return htl; 434 } 435 catch (Error err) { 436 err.printStackTrace(); 437 throw err; 438 } 439 } 440 }); 441 } 442 catch (DataAccessException ex) { 443 assertTrue(flushEx == ex.getCause()); 445 } 446 447 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 448 assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); 449 450 utControl.verify(); 451 sfControl.verify(); 452 sessionControl.verify(); 453 } 454 455 public void testJtaTransactionRollback() throws Exception { 456 MockControl utControl = MockControl.createControl(UserTransaction .class); 457 UserTransaction ut = (UserTransaction ) utControl.getMock(); 458 ut.getStatus(); 459 utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1); 460 ut.begin(); 461 utControl.setVoidCallable(1); 462 ut.rollback(); 463 utControl.setVoidCallable(1); 464 utControl.replay(); 465 466 MockControl sfControl = MockControl.createControl(SessionFactory.class); 467 final SessionFactory sf = (SessionFactory) sfControl.getMock(); 468 final MockControl sessionControl = MockControl.createControl(Session.class); 469 final Session session = (Session) sessionControl.getMock(); 470 sf.openSession(); 471 sfControl.setReturnValue(session, 1); 472 session.getSessionFactory(); 473 sessionControl.setReturnValue(sf, 1); 474 sfControl.replay(); 475 sessionControl.replay(); 476 477 JtaTransactionManager ptm = new JtaTransactionManager(ut); 478 TransactionTemplate tt = new TransactionTemplate(ptm); 479 final List l = new ArrayList (); 480 l.add("test"); 481 assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); 482 483 Object result = tt.execute(new TransactionCallback() { 484 public Object doInTransaction(TransactionStatus status) { 485 try { 486 assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive()); 487 HibernateTemplate ht = new HibernateTemplate(sf); 488 List htl = ht.executeFind(new HibernateCallback() { 489 public Object doInHibernate(Session session) { 490 return l; 491 } 492 }); 493 status.setRollbackOnly(); 494 sessionControl.verify(); 495 sessionControl.reset(); 496 try { 497 session.close(); 498 } 499 catch (HibernateException ex) { 500 } 501 sessionControl.setReturnValue(null, 1); 502 sessionControl.replay(); 503 return htl; 504 } 505 catch (Error err) { 506 err.printStackTrace(); 507 throw err; 508 } 509 } 510 }); 511 assertTrue("Correct result list", result == l); 512 513 assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); 514 utControl.verify(); 515 sfControl.verify(); 516 sessionControl.verify(); 517 } 518 519 public void testJtaTransactionCommitWithPreBound() throws Exception { 520 doTestJtaTransactionCommitWithPreBound(false, false, false); 521 } 522 523 public void testJtaTransactionCommitWithPreBoundAndReadOnly() throws Exception { 524 doTestJtaTransactionCommitWithPreBound(false, false, true); 525 } 526 527 public void testJtaTransactionCommitWithPreBoundAndFlushModeNever() throws Exception { 528 doTestJtaTransactionCommitWithPreBound(false, true, false); 529 } 530 531 public void testJtaTransactionCommitWithPreBoundAndFlushModeNeverAndReadOnly() throws Exception { 532 doTestJtaTransactionCommitWithPreBound(false, true, true); 533 } 534 535 public void testJtaTransactionCommitWithJtaTmAndPreBound() throws Exception { 536 doTestJtaTransactionCommitWithPreBound(true, false, false); 537 } 538 539 public void testJtaTransactionCommitWithJtaTmAndPreBoundAndReadOnly() throws Exception { 540 doTestJtaTransactionCommitWithPreBound(true, false, true); 541 } 542 543 public void testJtaTransactionCommitWithJtaTmAndPreBoundAndFlushModeNever() throws Exception { 544 doTestJtaTransactionCommitWithPreBound(true, true, false); 545 } 546 547 public void testJtaTransactionCommitWithJtaTmAndPreBoundAndFlushModeNeverAndReadOnly() throws Exception { 548 doTestJtaTransactionCommitWithPreBound(true, true, true); 549 } 550 551 protected void doTestJtaTransactionCommitWithPreBound( 552 boolean jtaTm, final boolean flushNever, final boolean readOnly) throws Exception { 553 554 MockControl utControl = MockControl.createControl(UserTransaction .class); 555 UserTransaction ut = (UserTransaction ) utControl.getMock(); 556 ut.getStatus(); 557 utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1); 558 ut.getStatus(); 559 utControl.setReturnValue(Status.STATUS_ACTIVE, 1); 560 ut.begin(); 561 utControl.setVoidCallable(1); 562 ut.commit(); 563 utControl.setVoidCallable(1); 564 565 MockControl tmControl = MockControl.createControl(TransactionManager .class); 566 TransactionManager tm = (TransactionManager ) tmControl.getMock(); 567 if (jtaTm) { 568 MockJtaTransaction transaction = new MockJtaTransaction(); 569 tm.getStatus(); 570 tmControl.setReturnValue(Status.STATUS_ACTIVE, 1); 571 tm.getTransaction(); 572 tmControl.setReturnValue(transaction, 1); 573 } 574 575 MockControl sfControl = MockControl.createControl(SessionFactoryImplementor.class); 576 final SessionFactoryImplementor sf = (SessionFactoryImplementor) sfControl.getMock(); 577 final MockControl sessionControl = MockControl.createControl(SessionImplementor.class); 578 final SessionImplementor session = (SessionImplementor) sessionControl.getMock(); 579 sf.getConnectionProvider(); 580 sfControl.setReturnValue(null, 1); 581 sf.getTransactionManager(); 582 sfControl.setReturnValue((jtaTm ? tm : null), 1); 583 session.getFlushMode(); 584 if (flushNever) { 585 sessionControl.setReturnValue(FlushMode.NEVER, 1); 586 if (!readOnly) { 587 session.setFlushMode(FlushMode.AUTO); 588 sessionControl.setVoidCallable(1); 589 } 590 } 591 else { 592 sessionControl.setReturnValue(FlushMode.AUTO, 1); 593 } 594 595 utControl.replay(); 596 tmControl.replay(); 597 sfControl.replay(); 598 sessionControl.replay(); 599 600 TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session)); 601 try { 602 JtaTransactionManager ptm = new JtaTransactionManager(ut); 603 TransactionTemplate tt = new TransactionTemplate(ptm); 604 tt.setReadOnly(readOnly); 605 final List l = new ArrayList (); 606 l.add("test"); 607 assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); 608 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 609 610 Object result = tt.execute(new TransactionCallback() { 611 public Object doInTransaction(TransactionStatus status) { 612 try { 613 assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive()); 614 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 615 HibernateTemplate ht = new HibernateTemplate(sf); 616 ht.setExposeNativeSession(true); 617 List htl = null; 618 for (int i = 0; i < 5; i++) { 619 htl = ht.executeFind(new HibernateCallback() { 620 public Object doInHibernate(Session sess) { 621 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 622 assertEquals(session, sess); 623 return l; 624 } 625 }); 626 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 627 } 628 sessionControl.verify(); 629 sessionControl.reset(); 630 try { 631 if (!readOnly) { 632 session.getFlushMode(); 633 sessionControl.setReturnValue(FlushMode.AUTO, 1); 634 session.flush(); 635 sessionControl.setVoidCallable(1); 636 if (flushNever) { 637 session.setFlushMode(FlushMode.NEVER); 638 sessionControl.setVoidCallable(1); 639 } 640 } 641 session.afterTransactionCompletion(true); 642 sessionControl.setVoidCallable(1); 643 } 644 catch (HibernateException e) { 645 } 646 sessionControl.replay(); 647 return htl; 648 } 649 catch (Error err) { 650 err.printStackTrace(); 651 throw err; 652 } 653 } 654 }); 655 656 assertTrue("Correct result list", result == l); 657 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 658 assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); 659 } 660 finally { 661 TransactionSynchronizationManager.unbindResource(sf); 662 } 663 664 utControl.verify(); 665 tmControl.verify(); 666 sfControl.verify(); 667 sessionControl.verify(); 668 } 669 670 public void testJtaTransactionRollbackWithPreBound() throws Exception { 671 MockControl utControl = MockControl.createControl(UserTransaction .class); 672 UserTransaction ut = (UserTransaction ) utControl.getMock(); 673 ut.getStatus(); 674 utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1); 675 ut.getStatus(); 676 utControl.setReturnValue(Status.STATUS_ACTIVE, 1); 677 ut.getStatus(); 678 utControl.setReturnValue(Status.STATUS_MARKED_ROLLBACK, 1); 679 ut.begin(); 680 utControl.setVoidCallable(1); 681 ut.setRollbackOnly(); 682 utControl.setVoidCallable(1); 683 RollbackException rex = new RollbackException (); 684 ut.commit(); 685 utControl.setThrowable(rex, 1); 686 utControl.replay(); 687 688 MockControl sfControl = MockControl.createControl(SessionFactory.class); 689 final SessionFactory sf = (SessionFactory) sfControl.getMock(); 690 final MockControl sessionControl = MockControl.createControl(Session.class); 691 final Session session = (Session) sessionControl.getMock(); 692 session.getSessionFactory(); 693 sessionControl.setReturnValue(sf, 1); 694 session.getFlushMode(); 695 sessionControl.setReturnValue(FlushMode.AUTO, 1); 696 sfControl.replay(); 697 sessionControl.replay(); 698 699 TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session)); 700 try { 701 JtaTransactionManager ptm = new JtaTransactionManager(ut); 702 final TransactionTemplate tt = new TransactionTemplate(ptm); 703 assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); 704 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 705 706 tt.execute(new TransactionCallbackWithoutResult() { 707 public void doInTransactionWithoutResult(TransactionStatus status) { 708 tt.execute(new TransactionCallbackWithoutResult() { 709 public void doInTransactionWithoutResult(TransactionStatus status) { 710 status.setRollbackOnly(); 711 try { 712 assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive()); 713 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 714 HibernateTemplate ht = new HibernateTemplate(sf); 715 ht.setExposeNativeSession(true); 716 for (int i = 0; i < 5; i++) { 717 ht.execute(new HibernateCallback() { 718 public Object doInHibernate(Session sess) { 719 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 720 assertEquals(session, sess); 721 return null; 722 } 723 }); 724 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 725 } 726 sessionControl.verify(); 727 sessionControl.reset(); 728 session.getFlushMode(); 729 sessionControl.setReturnValue(FlushMode.AUTO, 1); 730 session.flush(); 731 sessionControl.setVoidCallable(1); 732 session.clear(); 733 sessionControl.setVoidCallable(1); 734 sessionControl.replay(); 735 } 736 catch (Exception ex) { 737 ex.printStackTrace(); 738 } 739 catch (Error err) { 740 err.printStackTrace(); 741 throw err; 742 } 743 } 744 }); 745 } 746 }); 747 fail("Should have thrown UnexpectedRollbackException"); 748 } 749 catch (UnexpectedRollbackException ex) { 750 assertEquals(rex, ex.getCause()); 752 } 753 finally { 754 TransactionSynchronizationManager.unbindResource(sf); 755 } 756 757 utControl.verify(); 758 sfControl.verify(); 759 sessionControl.verify(); 760 } 761 762 public void testJtaSessionSynchronization() throws Exception { 763 MockControl tmControl = MockControl.createControl(TransactionManager .class); 764 TransactionManager tm = (TransactionManager ) tmControl.getMock(); 765 MockJtaTransaction transaction = new MockJtaTransaction(); 766 tm.getStatus(); 767 tmControl.setReturnValue(Status.STATUS_ACTIVE, 6); 768 tm.getTransaction(); 769 tmControl.setReturnValue(transaction, 6); 770 771 MockControl sfControl = MockControl.createControl(SessionFactoryImplementor.class); 772 final SessionFactoryImplementor sf = (SessionFactoryImplementor) sfControl.getMock(); 773 final MockControl sessionControl = MockControl.createControl(Session.class); 774 final Session session = (Session) sessionControl.getMock(); 775 sf.getConnectionProvider(); 776 sfControl.setReturnValue(null, 1); 777 sf.openSession(); 778 sfControl.setReturnValue(session, 1); 779 sf.getTransactionManager(); 780 sfControl.setReturnValue(tm, 6); 781 session.getFlushMode(); 782 sessionControl.setReturnValue(FlushMode.AUTO, 1); 783 session.flush(); 784 sessionControl.setVoidCallable(1); 785 session.close(); 786 sessionControl.setReturnValue(null, 1); 787 788 tmControl.replay(); 789 sfControl.replay(); 790 sessionControl.replay(); 791 792 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 793 HibernateTemplate ht = new HibernateTemplate(sf); 794 ht.setExposeNativeSession(true); 795 for (int i = 0; i < 5; i++) { 796 ht.executeFind(new HibernateCallback() { 797 public Object doInHibernate(Session sess) { 798 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 799 assertEquals(session, sess); 800 return null; 801 } 802 }); 803 } 804 805 Synchronization synchronization = transaction.getSynchronization(); 806 assertTrue("JTA synchronization registered", synchronization != null); 807 synchronization.beforeCompletion(); 808 synchronization.afterCompletion(Status.STATUS_COMMITTED); 809 810 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 811 assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); 812 813 tmControl.verify(); 814 sfControl.verify(); 815 sessionControl.verify(); 816 } 817 818 public void testJtaSessionSynchronizationWithRollback() throws Exception { 819 MockControl tmControl = MockControl.createControl(TransactionManager .class); 820 TransactionManager tm = (TransactionManager ) tmControl.getMock(); 821 MockJtaTransaction transaction = new MockJtaTransaction(); 822 tm.getStatus(); 823 tmControl.setReturnValue(Status.STATUS_ACTIVE, 6); 824 tm.getTransaction(); 825 tmControl.setReturnValue(transaction, 6); 826 827 MockControl sfControl = MockControl.createControl(SessionFactoryImplementor.class); 828 final SessionFactoryImplementor sf = (SessionFactoryImplementor) sfControl.getMock(); 829 final MockControl sessionControl = MockControl.createControl(Session.class); 830 final Session session = (Session) sessionControl.getMock(); 831 sf.getConnectionProvider(); 832 sfControl.setReturnValue(null, 1); 833 sf.openSession(); 834 sfControl.setReturnValue(session, 1); 835 sf.getTransactionManager(); 836 sfControl.setReturnValue(tm, 6); 837 session.close(); 838 sessionControl.setReturnValue(null, 1); 839 840 tmControl.replay(); 841 sfControl.replay(); 842 sessionControl.replay(); 843 844 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 845 HibernateTemplate ht = new HibernateTemplate(sf); 846 ht.setExposeNativeSession(true); 847 for (int i = 0; i < 5; i++) { 848 ht.executeFind(new HibernateCallback() { 849 public Object doInHibernate(Session sess) { 850 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 851 assertEquals(session, sess); 852 return null; 853 } 854 }); 855 } 856 857 Synchronization synchronization = transaction.getSynchronization(); 858 assertTrue("JTA synchronization registered", synchronization != null); 859 synchronization.afterCompletion(Status.STATUS_ROLLEDBACK); 860 861 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 862 assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); 863 864 tmControl.verify(); 865 sfControl.verify(); 866 sessionControl.verify(); 867 } 868 869 public void testJtaSessionSynchronizationWithRollbackByOtherThread() throws Exception { 870 MockControl tmControl = MockControl.createControl(TransactionManager .class); 871 TransactionManager tm = (TransactionManager ) tmControl.getMock(); 872 MockJtaTransaction transaction = new MockJtaTransaction(); 873 tm.getStatus(); 874 tmControl.setReturnValue(Status.STATUS_ACTIVE, 6); 875 tm.getStatus(); 876 tmControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 2); 877 tm.getTransaction(); 878 tmControl.setReturnValue(transaction, 6); 879 880 MockControl sfControl = MockControl.createControl(SessionFactoryImplementor.class); 881 final SessionFactoryImplementor sf = (SessionFactoryImplementor) sfControl.getMock(); 882 final MockControl sessionControl = MockControl.createControl(Session.class); 883 final Session session = (Session) sessionControl.getMock(); 884 sf.getConnectionProvider(); 885 sfControl.setReturnValue(null, 1); 886 sf.openSession(); 887 sfControl.setReturnValue(session, 2); 888 sf.getTransactionManager(); 889 sfControl.setReturnValue(tm, 7); 890 session.setFlushMode(FlushMode.NEVER); 891 sessionControl.setVoidCallable(1); 892 session.close(); 893 sessionControl.setReturnValue(null, 2); 894 895 tmControl.replay(); 896 sfControl.replay(); 897 sessionControl.replay(); 898 899 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 900 final HibernateTemplate ht = new HibernateTemplate(sf); 901 ht.setExposeNativeSession(true); 902 for (int i = 0; i < 5; i++) { 903 ht.executeFind(new HibernateCallback() { 904 public Object doInHibernate(Session sess) { 905 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 906 assertEquals(session, sess); 907 return null; 908 } 909 }); 910 } 911 912 final Synchronization synchronization = transaction.getSynchronization(); 913 assertTrue("JTA synchronization registered", synchronization != null); 914 Thread thread = new Thread () { 915 public void run() { 916 synchronization.afterCompletion(Status.STATUS_ROLLEDBACK); 917 } 918 }; 919 thread.start(); 920 thread.join(); 921 922 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 923 assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); 924 925 TransactionTemplate tt = new TransactionTemplate(new JtaTransactionManager(tm)); 926 tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS); 927 tt.setReadOnly(true); 928 tt.execute(new TransactionCallbackWithoutResult() { 929 protected void doInTransactionWithoutResult(TransactionStatus status) { 930 assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive()); 931 for (int i = 0; i < 5; i++) { 932 ht.executeFind(new HibernateCallback() { 933 public Object doInHibernate(Session sess) { 934 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 935 assertEquals(session, sess); 936 return null; 937 } 938 }); 939 } 940 } 941 }); 942 943 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 944 assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); 945 946 tmControl.verify(); 947 sfControl.verify(); 948 sessionControl.verify(); 949 } 950 951 public void testJtaSessionSynchronizationWithFlushFailure() throws Exception { 952 MockControl tmControl = MockControl.createControl(TransactionManager .class); 953 TransactionManager tm = (TransactionManager ) tmControl.getMock(); 954 MockJtaTransaction transaction = new MockJtaTransaction(); 955 tm.getStatus(); 956 tmControl.setReturnValue(Status.STATUS_ACTIVE, 6); 957 tm.getTransaction(); 958 tmControl.setReturnValue(transaction, 6); 959 tm.setRollbackOnly(); 960 tmControl.setVoidCallable(1); 961 962 final HibernateException flushEx = new HibernateException("flush failure"); 963 MockControl sfControl = MockControl.createControl(SessionFactoryImplementor.class); 964 final SessionFactoryImplementor sf = (SessionFactoryImplementor) sfControl.getMock(); 965 final MockControl sessionControl = MockControl.createControl(Session.class); 966 final Session session = (Session) sessionControl.getMock(); 967 sf.getConnectionProvider(); 968 sfControl.setReturnValue(null, 1); 969 sf.openSession(); 970 sfControl.setReturnValue(session, 1); 971 sf.getTransactionManager(); 972 sfControl.setReturnValue(tm, 6); 973 session.getFlushMode(); 974 sessionControl.setReturnValue(FlushMode.AUTO, 1); 975 session.flush(); 976 sessionControl.setThrowable(flushEx, 1); 977 session.close(); 978 sessionControl.setReturnValue(null, 1); 979 980 tmControl.replay(); 981 sfControl.replay(); 982 sessionControl.replay(); 983 984 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 985 HibernateTemplate ht = new HibernateTemplate(sf); 986 ht.setExposeNativeSession(true); 987 for (int i = 0; i < 5; i++) { 988 ht.executeFind(new HibernateCallback() { 989 public Object doInHibernate(Session sess) { 990 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 991 assertEquals(session, sess); 992 return null; 993 } 994 }); 995 } 996 997 Synchronization synchronization = transaction.getSynchronization(); 998 assertTrue("JTA synchronization registered", synchronization != null); 999 synchronization.beforeCompletion(); 1000 synchronization.afterCompletion(Status.STATUS_COMMITTED); 1001 1002 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 1003 assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); 1004 1005 tmControl.verify(); 1006 sfControl.verify(); 1007 sessionControl.verify(); 1008 } 1009 1010 public void testJtaSessionSynchronizationWithSuspendedTransaction() throws Exception { 1011 MockControl tmControl = MockControl.createControl(TransactionManager .class); 1012 TransactionManager tm = (TransactionManager ) tmControl.getMock(); 1013 MockJtaTransaction transaction1 = new MockJtaTransaction(); 1014 MockJtaTransaction transaction2 = new MockJtaTransaction(); 1015 tm.getStatus(); 1016 tmControl.setReturnValue(Status.STATUS_ACTIVE, 5); 1017 tm.getTransaction(); 1018 tmControl.setReturnValue(transaction1, 2); 1019 tm.getTransaction(); 1020 tmControl.setReturnValue(transaction2, 3); 1021 1022 MockControl sfControl = MockControl.createControl(SessionFactoryImplementor.class); 1023 final SessionFactoryImplementor sf = (SessionFactoryImplementor) sfControl.getMock(); 1024 final MockControl session1Control = MockControl.createControl(Session.class); 1025 final Session session1 = (Session) session1Control.getMock(); 1026 final MockControl session2Control = MockControl.createControl(Session.class); 1027 final Session session2 = (Session) session2Control.getMock(); 1028 sf.getConnectionProvider(); 1029 sfControl.setReturnValue(null, 1); 1030 sf.openSession(); 1031 sfControl.setReturnValue(session1, 1); 1032 sf.openSession(); 1033 sfControl.setReturnValue(session2, 1); 1034 sf.getTransactionManager(); 1035 sfControl.setReturnValue(tm, 5); 1036 session1.getFlushMode(); 1037 session1Control.setReturnValue(FlushMode.AUTO, 1); 1038 session2.getFlushMode(); 1039 session2Control.setReturnValue(FlushMode.AUTO, 1); 1040 session1.flush(); 1041 session1Control.setVoidCallable(1); 1042 session2.flush(); 1043 session2Control.setVoidCallable(1); 1044 session1.close(); 1045 session1Control.setReturnValue(null, 1); 1046 session2.close(); 1047 session2Control.setReturnValue(null, 1); 1048 1049 tmControl.replay(); 1050 sfControl.replay(); 1051 session1Control.replay(); 1052 session2Control.replay(); 1053 1054 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 1055 HibernateTemplate ht = new HibernateTemplate(sf); 1056 ht.setExposeNativeSession(true); 1057 ht.executeFind(new HibernateCallback() { 1058 public Object doInHibernate(Session sess) { 1059 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 1060 assertEquals(session1, sess); 1061 return null; 1062 } 1063 }); 1064 ht.executeFind(new HibernateCallback() { 1065 public Object doInHibernate(Session sess) { 1066 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 1067 assertEquals(session2, sess); 1068 return null; 1069 } 1070 }); 1071 1072 Synchronization synchronization2 = transaction2.getSynchronization(); 1073 assertTrue("JTA synchronization registered", synchronization2 != null); 1074 synchronization2.beforeCompletion(); 1075 synchronization2.afterCompletion(Status.STATUS_COMMITTED); 1076 1077 Synchronization synchronization1 = transaction1.getSynchronization(); 1078 assertTrue("JTA synchronization registered", synchronization1 != null); 1079 synchronization1.beforeCompletion(); 1080 synchronization1.afterCompletion(Status.STATUS_COMMITTED); 1081 1082 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 1083 assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); 1084 1085 tmControl.verify(); 1086 sfControl.verify(); 1087 session1Control.verify(); 1088 session2Control.verify(); 1089 } 1090 1091 public void testJtaSessionSynchronizationWithNonSessionFactoryImplementor() throws Exception { 1092 MockControl tmControl = MockControl.createControl(TransactionManager .class); 1093 TransactionManager tm = (TransactionManager ) tmControl.getMock(); 1094 MockJtaTransaction transaction = new MockJtaTransaction(); 1095 tm.getStatus(); 1096 tmControl.setReturnValue(Status.STATUS_ACTIVE, 6); 1097 tm.getTransaction(); 1098 tmControl.setReturnValue(transaction, 6); 1099 1100 MockControl sfControl = MockControl.createControl(SessionFactory.class); 1101 final SessionFactory sf = (SessionFactory) sfControl.getMock(); 1102 final MockControl sessionControl = MockControl.createControl(Session.class); 1103 final Session session = (Session) sessionControl.getMock(); 1104 MockControl sfiControl = MockControl.createControl(SessionFactoryImplementor.class); 1105 final SessionFactoryImplementor sfi = (SessionFactoryImplementor) sfiControl.getMock(); 1106 sf.openSession(); 1107 sfControl.setReturnValue(session, 1); 1108 session.getSessionFactory(); 1109 sessionControl.setReturnValue(sfi, 6); 1110 sfi.getTransactionManager(); 1111 sfiControl.setReturnValue(tm, 6); 1112 session.getFlushMode(); 1113 sessionControl.setReturnValue(FlushMode.AUTO, 1); 1114 session.flush(); 1115 sessionControl.setVoidCallable(1); 1116 session.close(); 1117 sessionControl.setReturnValue(null, 1); 1118 1119 tmControl.replay(); 1120 sfControl.replay(); 1121 sessionControl.replay(); 1122 sfiControl.replay(); 1123 1124 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 1125 HibernateTemplate ht = new HibernateTemplate(sf); 1126 ht.setExposeNativeSession(true); 1127 for (int i = 0; i < 5; i++) { 1128 ht.executeFind(new HibernateCallback() { 1129 public Object doInHibernate(Session sess) { 1130 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 1131 assertEquals(session, sess); 1132 return null; 1133 } 1134 }); 1135 } 1136 1137 Synchronization synchronization = transaction.getSynchronization(); 1138 assertTrue("JTA Synchronization registered", synchronization != null); 1139 synchronization.beforeCompletion(); 1140 synchronization.afterCompletion(Status.STATUS_COMMITTED); 1141 1142 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 1143 assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); 1144 1145 tmControl.verify(); 1146 sfControl.verify(); 1147 sessionControl.verify(); 1148 sfiControl.verify(); 1149 } 1150 1151 public void testJtaSessionSynchronizationWithSpringTransactionLaterOn() throws Exception { 1152 MockControl utControl = MockControl.createControl(UserTransaction .class); 1153 UserTransaction ut = (UserTransaction ) utControl.getMock(); 1154 MockControl tmControl = MockControl.createControl(TransactionManager .class); 1155 TransactionManager tm = (TransactionManager ) tmControl.getMock(); 1156 MockJtaTransaction transaction = new MockJtaTransaction(); 1157 ut.getStatus(); 1158 utControl.setReturnValue(Status.STATUS_ACTIVE, 1); 1159 tm.getStatus(); 1160 tmControl.setReturnValue(Status.STATUS_ACTIVE, 6); 1161 tm.getTransaction(); 1162 tmControl.setReturnValue(transaction, 6); 1163 1164 MockControl sfControl = MockControl.createControl(SessionFactoryImplementor.class); 1165 final SessionFactoryImplementor sf = (SessionFactoryImplementor) sfControl.getMock(); 1166 final MockControl sessionControl = MockControl.createControl(Session.class); 1167 final Session session = (Session) sessionControl.getMock(); 1168 sf.getConnectionProvider(); 1169 sfControl.setReturnValue(null, 1); 1170 sf.openSession(); 1171 sfControl.setReturnValue(session, 1); 1172 sf.getTransactionManager(); 1173 sfControl.setReturnValue(tm, 6); 1174 session.getFlushMode(); 1175 sessionControl.setReturnValue(FlushMode.AUTO, 1); 1176 session.flush(); 1177 sessionControl.setVoidCallable(1); 1178 session.close(); 1179 sessionControl.setReturnValue(null, 1); 1180 1181 utControl.replay(); 1182 tmControl.replay(); 1183 sfControl.replay(); 1184 sessionControl.replay(); 1185 1186 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 1187 final HibernateTemplate ht = new HibernateTemplate(sf); 1188 ht.setExposeNativeSession(true); 1189 for (int i = 0; i < 2; i++) { 1190 ht.executeFind(new HibernateCallback() { 1191 public Object doInHibernate(Session sess) { 1192 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 1193 assertEquals(session, sess); 1194 return null; 1195 } 1196 }); 1197 } 1198 1199 TransactionTemplate tt = new TransactionTemplate(new JtaTransactionManager(ut)); 1200 tt.execute(new TransactionCallbackWithoutResult() { 1201 protected void doInTransactionWithoutResult(TransactionStatus status) { 1202 for (int i = 2; i < 5; i++) { 1203 ht.executeFind(new HibernateCallback() { 1204 public Object doInHibernate(Session sess) { 1205 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 1206 assertEquals(session, sess); 1207 return null; 1208 } 1209 }); 1210 } 1211 } 1212 }); 1213 1214 Synchronization synchronization = transaction.getSynchronization(); 1215 assertTrue("JTA synchronization registered", synchronization != null); 1216 synchronization.beforeCompletion(); 1217 synchronization.afterCompletion(Status.STATUS_COMMITTED); 1218 1219 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 1220 assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); 1221 1222 utControl.verify(); 1223 tmControl.verify(); 1224 sfControl.verify(); 1225 sessionControl.verify(); 1226 } 1227 1228 public void testJtaSessionSynchronizationWithPreBound() throws Exception { 1229 doTestJtaSessionSynchronizationWithPreBound(false); 1230 } 1231 1232 public void testJtaJtaSessionSynchronizationWithPreBoundAndFlushNever() throws Exception { 1233 doTestJtaSessionSynchronizationWithPreBound(true); 1234 } 1235 1236 private void doTestJtaSessionSynchronizationWithPreBound(boolean flushNever) throws Exception { 1237 MockControl tmControl = MockControl.createControl(TransactionManager .class); 1238 TransactionManager tm = (TransactionManager ) tmControl.getMock(); 1239 MockJtaTransaction transaction = new org.springframework.transaction.MockJtaTransaction(); 1240 tm.getStatus(); 1241 tmControl.setReturnValue(Status.STATUS_ACTIVE, 6); 1242 tm.getTransaction(); 1243 tmControl.setReturnValue(transaction, 6); 1244 1245 MockControl sfControl = MockControl.createControl(SessionFactoryImplementor.class); 1246 final SessionFactoryImplementor sf = (SessionFactoryImplementor) sfControl.getMock(); 1247 final MockControl sessionControl = MockControl.createControl(Session.class); 1248 final Session session = (Session) sessionControl.getMock(); 1249 sf.getConnectionProvider(); 1250 sfControl.setReturnValue(null, 1); 1251 sf.getTransactionManager(); 1252 sfControl.setReturnValue(tm, 6); 1253 session.getFlushMode(); 1254 if (flushNever) { 1255 sessionControl.setReturnValue(FlushMode.NEVER, 1); 1256 session.setFlushMode(FlushMode.AUTO); 1257 sessionControl.setVoidCallable(1); 1258 } 1259 else { 1260 sessionControl.setReturnValue(FlushMode.AUTO, 1); 1261 } 1262 1263 tmControl.replay(); 1264 sfControl.replay(); 1265 sessionControl.replay(); 1266 1267 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 1268 TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session)); 1269 try { 1270 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 1271 HibernateTemplate ht = new HibernateTemplate(sf); 1272 ht.setExposeNativeSession(true); 1273 for (int i = 0; i < 5; i++) { 1274 ht.executeFind(new HibernateCallback() { 1275 public Object doInHibernate(Session sess) { 1276 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 1277 assertEquals(session, sess); 1278 return null; 1279 } 1280 }); 1281 } 1282 1283 sessionControl.verify(); 1284 sessionControl.reset(); 1285 session.getFlushMode(); 1286 sessionControl.setReturnValue(FlushMode.AUTO, 1); 1287 session.flush(); 1288 sessionControl.setVoidCallable(1); 1289 if (flushNever) { 1290 session.setFlushMode(FlushMode.NEVER); 1291 sessionControl.setVoidCallable(1); 1292 } 1293 sessionControl.replay(); 1294 1295 Synchronization synchronization = transaction.getSynchronization(); 1296 assertTrue("JTA synchronization registered", synchronization != null); 1297 synchronization.beforeCompletion(); 1298 synchronization.afterCompletion(Status.STATUS_COMMITTED); 1299 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 1300 } 1301 finally { 1302 TransactionSynchronizationManager.unbindResource(sf); 1303 } 1304 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 1305 1306 tmControl.verify(); 1307 sfControl.verify(); 1308 sessionControl.verify(); 1309 } 1310 1311 public void testJtaSessionSynchronizationWithRemoteTransaction() throws Exception { 1312 MockControl tmControl = MockControl.createControl(TransactionManager .class); 1313 TransactionManager tm = (TransactionManager ) tmControl.getMock(); 1314 MockJtaTransaction transaction = new MockJtaTransaction(); 1315 1316 MockControl sfControl = MockControl.createControl(SessionFactoryImplementor.class); 1317 final SessionFactoryImplementor sf = (SessionFactoryImplementor) sfControl.getMock(); 1318 final MockControl sessionControl = MockControl.createControl(Session.class); 1319 final Session session = (Session) sessionControl.getMock(); 1320 1321 for (int j = 0; j < 2; j++) { 1322 tmControl.reset(); 1323 sfControl.reset(); 1324 sessionControl.reset(); 1325 1326 tm.getStatus(); 1327 tmControl.setReturnValue(Status.STATUS_ACTIVE, 6); 1328 tm.getTransaction(); 1329 tmControl.setReturnValue(transaction, 6); 1330 1331 sf.getConnectionProvider(); 1332 sfControl.setReturnValue(null, 1); 1333 sf.openSession(); 1334 sfControl.setReturnValue(session, 1); 1335 sf.getTransactionManager(); 1336 sfControl.setReturnValue(tm, 6); 1337 session.getFlushMode(); 1338 sessionControl.setReturnValue(FlushMode.AUTO, 1); 1339 session.flush(); 1340 sessionControl.setVoidCallable(1); 1341 session.close(); 1342 sessionControl.setReturnValue(null, 1); 1343 1344 tmControl.replay(); 1345 sfControl.replay(); 1346 sessionControl.replay(); 1347 1348 if (j == 0) { 1349 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 1350 } 1351 else { 1352 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 1353 } 1354 1355 HibernateTemplate ht = new HibernateTemplate(sf); 1356 ht.setExposeNativeSession(true); 1357 for (int i = 0; i < 5; i++) { 1358 ht.executeFind(new HibernateCallback() { 1359 public Object doInHibernate(Session sess) { 1360 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 1361 assertEquals(session, sess); 1362 return null; 1363 } 1364 }); 1365 } 1366 1367 final Synchronization synchronization = transaction.getSynchronization(); 1368 assertTrue("JTA synchronization registered", synchronization != null); 1369 1370 Thread synch = new Thread () { 1373 public void run() { 1374 synchronization.beforeCompletion(); 1375 synchronization.afterCompletion(Status.STATUS_COMMITTED); 1376 } 1377 }; 1378 synch.start(); 1379 synch.join(); 1380 1381 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 1382 SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sf); 1383 assertTrue("Thread session holder empty", sessionHolder.isEmpty()); 1384 assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); 1385 1386 tmControl.verify(); 1387 sfControl.verify(); 1388 sessionControl.verify(); 1389 } 1390 1391 TransactionSynchronizationManager.unbindResource(sf); 1392 } 1393 1394 protected void tearDown() { 1395 assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty()); 1396 assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); 1397 assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly()); 1398 assertFalse(TransactionSynchronizationManager.isActualTransactionActive()); 1399 } 1400 1401} 1402 | Popular Tags |