Results 1 to 2 of 2

Thread: PlatformTransactionManager, not working

  1. #1
    Join Date
    Mar 2009
    Posts
    18

    Default PlatformTransactionManager, not working

    Hello all,

    I'm testing programmatically manage transaction using the example here


    http://static.springframework.org/sp...ogrammatic-ptm

    Code:
       DefaultTransactionDefinition def = new DefaultTransactionDefinition();
    // explicitly setting the transaction name is something that can only be done programmatically
    def.setName("SomeTxName");
    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
    
    TransactionStatus status = txManager.getTransaction(def);
    try {
      // execute your business logic here
    }
    catch (MyException ex) {
      txManager.rollback(status);
      throw ex;
    }
    txManager.commit(status);
    What's not clear to me is how to perform the middle part between the transaction.

    I tried

    Code:
        @PersistenceUnit(unitName = "springappPU")
        private EntityManagerFactory emf;
        public void setEntityManagerFactory(EntityManagerFactory emf) {
            this.emf = emf;
        }
        private EntityManager getEntityManager() {
            return  emf.createEntityManager();
        }
        
        PlatformTransactionManager txnManager = null;
    
        public void setTxnManager(PlatformTransactionManager mgr){
            txnManager = mgr;
        }
      
        .....
     
        try {
                EntityManager em = this.getEntityManager();
                em.persist(sponsor);
        }
        catch (MyException ex) {
          txManager.rollback(status);
          throw ex;
        }
        txManager.commit(status);
    Looking at the log files, no insert statement was logged, no errors/exceptions.

    I also tried to get EntityManager by

    Code:
       EntityManager em = txnManager.getEntityManagerFactory().createEntityManager();
    same issue, no insert, no errors, no exceptions

    what am I doing wrong?

    Thanks

  2. #2
    Join Date
    Mar 2009
    Posts
    18

    Default

    Never mind, turns out to be non-Spring related, but would be good if the Spring documentation stated though.

    I had to do "em.joinTransaction();" to make it work since I have Application-Managed Entity Manager

    http://www.hibernate.org/hib_docs/en...nsactions.html

    says

    1) "Application-managed entity manager are always EXTENDED."

    2) "All application managed entity manager and container managed persistence contexts defined as such are EXTENDED. This means that the persistence context type goes beyond the transaction life cycle"

    3) "Some modifications operations can be executed outside a transaction, but they are queued until the persistence context join a transaction: this is the case of persist(), merge(), remove()."

    It was confusing though, because under "Application Managed Transaction", it also says

    "It is not legal to call entityManager.joinTransaction() if no JTA transaction is involved."

    huh? but using RESOURCE_LOCAL, i just managed to do .joinTransaction().

    Anyway, hope it helps others.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •