Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to start container-managed transaction before calling executeUpdate()

 
Volodymyr Lysenko
Ranch Hand
Posts: 512
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

I want to call named query that performs UPDATE sql query.

Named query Topic.updateViewQuantity is as follows:
UPDATE Topic t SET t.viewQuantity=t.viewQuantity + 1 WHERE t.id = :topicId[/code]
I have session scoped facade that uses container-managed transaction. The result of calling query.executeUpdate is :

Caused by: javax.persistence.TransactionRequiredException: executeUpdate is not supported for a Query object obtained through non-transactional access of a container-managed transactional EntityManager
at com.sun.enterprise.container.common.impl.QueryWrapper.executeUpdate(QueryWrapper.java:225)
at com.javahelp.session.TopicFacade.doUpdateQuery(TopicFacade.java:204)
at com.javahelp.session.TopicFacade.incrementViewQuantity(TopicFacade.java:97)
at com.javahelp.backingbeans.TopicBacking.<init>(TopicBacking.java:66)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at java.lang.Class.newInstance0(Class.java:372)
at java.lang.Class.newInstance(Class.java:325)
at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:188)

I tried to put query.executeUpdate in separate method and call the whole method within utx.begin and utx.commit but unfortunately it does not help.
I know exactly utx.begin() works well because utx.getStatus() returns 0 which means that transaction is active.
If I remove utx.begin and utx.commit the same exception is thrown.

How to start container-managed transaction before calling executeUpdate()?
How to execute UPDATE query via @NamedQuery in active contaner managed transaction?
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1103
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to point out something first. What you refer is actually a JTA transaction. Now, JTA transaction can be handled by the container (which is called CMT) or by the bean (BMT).
What you are now doing is BMT (and hence the usage of UserTransaction instance).

In this case, you should either a) start the transaction even before you create the EntityManager (or) b) call methods on entityManager including createQuery after you start the transaction (if this doesn't work, I think calling entityManager.joinTransaction() should work before you call methods on the entityManager)
 
Tim Holloway
Saloon Keeper
Posts: 18367
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This isn't JSF, it's ORM. Moving to the ORM forum.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic