/tags/warp-persistWithJpaAndHibernate/test/com/wideplay/warp/hibernate/ManualLocalTransactionsTest.java

http://warp-persist.googlecode.com/ · Java · 95 lines · 69 code · 17 blank · 9 comment · 2 complexity · 4248abb9f721c6c79e5b66fdb6383284 MD5 · raw file

  1. package com.wideplay.warp.hibernate;
  2. import com.google.inject.Injector;
  3. import com.google.inject.Guice;
  4. import com.google.inject.Inject;
  5. import com.google.inject.AbstractModule;
  6. import com.google.inject.matcher.Matchers;
  7. import com.wideplay.codemonkey.web.startup.Initializer;
  8. import com.wideplay.warp.persist.*;
  9. import java.util.Date;
  10. import org.testng.annotations.BeforeClass;
  11. import org.testng.annotations.Test;
  12. import org.hibernate.cfg.AnnotationConfiguration;
  13. import org.hibernate.cfg.Configuration;
  14. import org.hibernate.Session;
  15. import org.hibernate.SessionFactory;
  16. import org.hibernate.context.ManagedSessionContext;
  17. import org.hibernate.criterion.Expression;
  18. /**
  19. * Created with IntelliJ IDEA.
  20. * On: 2/06/2007
  21. *
  22. * @author Dhanji R. Prasanna
  23. * @since 1.0
  24. */
  25. public class ManualLocalTransactionsTest {
  26. private Injector injector;
  27. private static final String UNIQUE_TEXT = "some unique text" + new Date();
  28. private static final String UNIQUE_TEXT_2 = "some other unique text" + new Date();
  29. @BeforeClass
  30. public void pre() {
  31. injector = Guice.createInjector(PersistenceService.usingHibernate()
  32. .across(UnitOfWork.REQUEST)
  33. .transactedWith(TransactionStrategy.LOCAL)
  34. .forAll(Matchers.any())
  35. .buildModule(),
  36. new AbstractModule() {
  37. protected void configure() {
  38. bind(Configuration.class).toInstance(new AnnotationConfiguration()
  39. .addAnnotatedClass(HibernateTestEntity.class)
  40. .setProperties(Initializer.loadProperties("spr-managed-persistence.properties")));
  41. }
  42. });
  43. //startup persistence
  44. injector.getInstance(PersistenceService.class)
  45. .start();
  46. }
  47. @Test
  48. public void testSimpleCrossTxnWork() {
  49. org.hibernate.classic.Session session1 = injector.getInstance(SessionFactory.class).openSession();
  50. ManagedSessionContext.bind(session1);
  51. HibernateTestEntity entity = injector.getInstance(TransactionalObject.class).runOperationInTxn();
  52. injector.getInstance(ManualLocalTransactionsTest.TransactionalObject.class).runOperationInTxn2();
  53. assert injector.getInstance(Session.class).contains(entity) : "Session appears to have been closed across txns!";
  54. session1.close();
  55. //try to query them back out
  56. Session session = injector.getInstance(SessionFactory.class).openSession();
  57. assert null != session.createCriteria(HibernateTestEntity.class).add(Expression.eq("text", UNIQUE_TEXT)).uniqueResult();
  58. assert null != session.createCriteria(HibernateTestEntity.class).add(Expression.eq("text", UNIQUE_TEXT_2)).uniqueResult();
  59. session.close();
  60. }
  61. public static class TransactionalObject {
  62. @Inject
  63. Session session;
  64. @Transactional
  65. public HibernateTestEntity runOperationInTxn() {
  66. HibernateTestEntity entity = new HibernateTestEntity();
  67. entity.setText(UNIQUE_TEXT);
  68. session.persist(entity);
  69. return entity;
  70. }
  71. @Transactional
  72. public void runOperationInTxn2() {
  73. HibernateTestEntity entity = new HibernateTestEntity();
  74. entity.setText(UNIQUE_TEXT_2);
  75. session.persist(entity);
  76. }
  77. }
  78. }