Yes. One minor problem:

@PersistentContext should be @PersistentUnit.


That's exactly what I used to do in the past three years.

I controlled the entitymanager and transaction by my own code in a JPA
wrapper class,
and gained great performance advantage over automatic transaction handling
like @TransactionAttribute.

I never inject a @PersistentContext or never use a container provided
EntityManager.
I use ThreadLocal to maintain every EntityManager instance.

-Wesley

2010/3/30 Stargazer <[email protected]>

> On 30-Mar-2010 09:34, Wesley Wu wrote:
> > To make set method auto translated into a UPDATE clause, the
> > entitymanager should be opened after a transaction begins.
> >
> >
> Sincere thanks again, hopefully this will all help others coming across
> it in the future.
>
> If I understood you correctly I made those changes and now get
> example.CourseServlet.emf : @PersistenceContext field must be assignable
> from EntityManager.
>
> Heres the new full servlet:
>
> package example;
>
> import java.io.IOException;
> import java.io.PrintWriter;
>
> import javax.inject.Inject;
> import javax.persistence.EntityManager;
> import javax.persistence.EntityManagerFactory;
> import javax.persistence.PersistenceContext;
> import javax.servlet.ServletException;
> import javax.servlet.http.HttpServlet;
> import javax.servlet.http.HttpServletRequest;
> import javax.servlet.http.HttpServletResponse;
> import javax.transaction.UserTransaction;
>
> public class CourseServlet extends HttpServlet
> {
>   // Resin IoC will inject this
>   @PersistenceContext(unitName="example")
>    EntityManagerFactory emf;
>
>   @Inject
>   private UserTransaction ut;
>
>   public void service(HttpServletRequest request, HttpServletResponse
> response)
>     throws IOException, ServletException
>   {
>     PrintWriter out = response.getWriter();
>     response.setContentType("text/html");
>
>      EntityManager em = null;
>     try {
>         ut.begin();
>          em = emf.createEntityManager();
>         CourseBean updateCourse = em.find(CourseBean.class, new
> Integer(1));
>         updateCourse.setCourse("Magic");
>         ut.commit();
>     } catch (Exception e) {
>         e.printStackTrace();
>     } finally {
>         if (em != null && em.isOpen()) {
>             em.close();
>         }
>     }
>   }
> }
>
>
>
>
> _______________________________________________
> resin-interest mailing list
> [email protected]
> http://maillist.caucho.com/mailman/listinfo/resin-interest
>
_______________________________________________
resin-interest mailing list
[email protected]
http://maillist.caucho.com/mailman/listinfo/resin-interest

Reply via email to