dev@glassfish.java.net

Re: TransactionRequiredException for UPDATE query ?

From: Marina Vatkina <Marina.Vatkina_at_Sun.COM>
Date: Thu, 25 Oct 2007 22:19:38 -0700

It's in the javadoc of the Query API:

* @throws TransactionRequiredException if there is
* no transaction
*/
public int executeUpdate();

Regards,
-marina

Arun Gupta wrote:
> I've the following code fragment in a JSP:
>
> -- cut here --
> EntityManagerFactory emf =
> Persistence.createEntityManagerFactory("WebApplication1PU");
> EntityManager em = emf.createEntityManager();
>
> String update = request.getParameter("update");
> if (Boolean.parseBoolean(update)) {
> String isbn = request.getParameter("isbn");
> String description = request.getParameter("description");
>
> int result = em.createNamedQuery("Books.updateDescription").
> setParameter("isbn", isbn).
> setParameter("description", description).
> executeUpdate();
> out.println(result + " entities updated.");
> }
> -- cut here --
>
> Books.updateDescription is:
>
> @NamedQuery(name = "Books.updateDescription", query = "UPDATE Books b
> SET b.description= :description WHERE b.isbn = :isbn")
>
> If I try to invoke the page as:
>
> http://localhost:8080/WebApplication1/data.jsp?update=true&isbn=123456&description=How%20to%20train%20for%20a%20marathon%20%3FThu%20Oct%2025%202007%2021%3A28%3A26%20GMT-0700%20(Pacific%20Daylight%20Time)
>
>
> then I get the exception:
>
> javax.persistence.TransactionRequiredException:
> Exception Description: No transaction is currently active
>
> This is not an issue if I replace updateDescription with another query
> that uses only SELECT. Do I need special transactions when UPDATE query
> is invoked ?
>
> I could not find any reference in the JavaEE5 tutorial
> (http://java.sun.com/javaee/5/docs/tutorial/doc/bnbtl.html#bnbud) on
> this topic.
>
> -Arun