Announcement Announcement Module
Collapse
No announcement yet.
Query.executeUpdate() - No transaction is currently active Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Query.executeUpdate() - No transaction is currently active

    Hi,
    I'm having trouble using the method executeUpdate (). An example:
    Code:
    public void update() {
    		DefaultTransactionDefinition def = new DefaultTransactionDefinition();
    		
    		def.setName("updateTx");
    		def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    		
    		TransactionStatus status = txManager.getTransaction(def);
    		Query query = entityManager.createQuery(" update Contact c set c.name = 'Daniel' where c.id = '2'");
    		query.executeUpdate();
    		
    		txManager.commit(status);
    }
    This code ever throw an Exception:
    Code:
    javax.persistence.TransactionRequiredException: 
    Exception Description: No transaction is currently active
    Any idea what might be happening?
    Last edited by d.frank; Feb 1st, 2011, 10:45 AM.

  • #2
    What transaction manager are you using?

    Comment


    • #3
      Hi pratikbhavsar,
      I am using org.springframework.orm.jpa.JpaTransactionManager.

      Comment


      • #4
        Use

        entityManager.getTransaction().begin()

        and

        entityManager.getTransaction().commit()

        to begin and end a JpaTransaction.

        Comment


        • #5
          Sorry, but I've tried that before.
          entityManager.getTransaction().begin() cause this exception:
          Code:
          java.lang.IllegalStateException : Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT instead

          Comment


          • #6
            Why don't you use Spring's transaction handling by annotation the method with @Transactional? Anyway, if you want to handle transactions programatically you either have to create an application managed EntityManager by using EntityManagerFactory and then use entityManager.getTransaction() or use Spring's EntityManager and call em.joinTransaction() after starting your own transaction. I'm not shure if the latter works with any transaction manager or if this works with JTA only.

            Robin.

            Comment


            • #7
              I am using EntityManagerFactory right now to solve this issue.
              I do not use @Transactional, because I need two different transactions in the same method.

              Comment


              • #8
                have this issue with entityManager().createNativeQuery(sql).executeUpda te() as well. singleResult(), resultList() both work without issue - just executeUpdate. @Transactional seems to be ineffective for this... any advice?

                Comment

                Working...
                X