Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
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

Win a copy of Head First Android this week in the Android forum!
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

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

Volodymyr Lysenko
Ranch Hand

Joined: Mar 29, 2012
Posts: 511


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 = :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(
at com.javahelp.session.TopicFacade.doUpdateQuery(
at com.javahelp.session.TopicFacade.incrementViewQuantity(
at com.javahelp.backingbeans.TopicBacking.<init>(
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(
at java.lang.reflect.Constructor.newInstance(
at java.lang.Class.newInstance0(
at java.lang.Class.newInstance(
at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(

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

Joined: Oct 16, 2003
Posts: 1100

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

Joined: Jun 25, 2001
Posts: 16897

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.
subject: How to start container-managed transaction before calling executeUpdate()
jQuery in Action, 3rd edition