aspose file tools*
The moose likes Object Relational Mapping and the fly likes How to start container-managed transaction before calling executeUpdate() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "How to start container-managed transaction before calling executeUpdate()" Watch "How to start container-managed transaction before calling executeUpdate()" New topic
Author

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

Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

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?


True person is moral, false is right!
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1083
    
  10

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)


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16065
    
  21

This isn't JSF, it's ORM. Moving to the ORM forum.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to start container-managed transaction before calling executeUpdate()