Results 1 to 8 of 8

Thread: Query.executeUpdate() - No transaction is currently active

  1. #1
    Join Date
    Jan 2011
    Posts
    5

    Default [SOLVED] - 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 at 10:45 AM.

  2. #2

    Default

    What transaction manager are you using?

  3. #3
    Join Date
    Jan 2011
    Posts
    5

    Default

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

  4. #4

    Default

    Use

    entityManager.getTransaction().begin()

    and

    entityManager.getTransaction().commit()

    to begin and end a JpaTransaction.

  5. #5
    Join Date
    Jan 2011
    Posts
    5

    Default

    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

  6. #6
    Join Date
    Oct 2008
    Posts
    26

    Default

    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.

  7. #7
    Join Date
    Jan 2011
    Posts
    5

    Default

    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.

  8. #8
    Join Date
    Oct 2008
    Posts
    7

    Default

    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?

Tags for this Thread

Posting Permissions

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