On 29-Mar-2010 17:48, Scott Ferguson wrote:
Wesley Wu wrote:
Not a version issue.

An entitymanager should not get transaction by itself. Transaction in
a modern java appserver should be XA or JTA transaciton.
An entitymanager will detect if there is a JTA transaction existing
and will join it if there is an open one.

You need to use an injected UserTransaction instance to do the
transaction stuff and leave the entitymanager do db stuff and the
entitymanager will participate in the transaction.

Also, if you absolutely need to use the EntityTransaction, you'd need to
grab the EntityManagerFactory, not the EntityManager. The EntityManager
is tied into the container's transaction manager.

The UserTransaction is registered with CanDI, by the way, so it's easy
to grab:

public class MyBean {
   @Inject UserTransaction _ut;

   ...
}

-- Scott
Ok thanks, I'm now sure the problem was me working from old Hibernate docs. But I'm still struggling. Referring back to the example: http://wiki.caucho.com/Hibernate <http://wiki.caucho.com/Hibernate>It works perfectly as given, with resin 4.0.5. I added the UserTransaction and tried to make a change and commit but saw no difference in the db. Since the courses were listed I'm assuming everyting else is ok. What have I missed please? Heres the servlet in full:

package example;

import java.io.IOException;
import java.io.PrintWriter;

import javax.inject.Inject;
import javax.persistence.EntityManager;
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")
  private EntityManager _manager;

  @Inject
  private UserTransaction ut;

public void service(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    PrintWriter out = response.getWriter();
    response.setContentType("text/html");

    out.println("EntityManager = " + _manager + "<br/>");

    CourseBean []course = new CourseBean[2];

    course[0] = _manager.find(CourseBean.class, new Integer(1));
    course[1] = _manager.find(CourseBean.class, new Integer(2));

    out.println("Course Details<br/><br/>");

    for (int i = 0; i < course.length; i++) {
      out.println("course: " + course[i].getCourse() + "<br/>");
      out.println("teacher: " + course[i].getTeacher() + "<br/>");
    }

CourseBean updateCourse = _manager.find(CourseBean.class, new Integer(1));
    try {
        ut.begin();
        updateCourse.setCourse("Magic");
        ut.commit();
    } catch (Exception e) {
        e.printStackTrace();
    }
  }
}

2010/3/29 Stargazer<starga...@blueyonder.co.uk>

On 27-Mar-2010 01:10, Stargazer wrote:

Resin 4.0.5 - following http://wiki.caucho.com/Hibernate works fine, but
I'd like to take it to the next level and persist something.
Adding
           EntityTransaction tx = _manager.getTransaction();
           tx.begin();
           ...
to the end of the CourseServlet.java file throws up

java.lang.IllegalStateException: Container-manager @PersistenceContext
may not use getTransaction.

What have I missed please?



_______________________________________________
resin-interest mailing list
resin-interest@caucho.com
http://maillist.caucho.com/mailman/listinfo/resin-interest



Could anyone tell me if this worked in an earlier vrsion of resin please?
Its the first time I've tried hibernate with resin, and I can't tell if
its something I'm doing (or not doing!) here or related to an issue in
4.0.5.



_______________________________________________
resin-interest mailing list
resin-interest@caucho.com
http://maillist.caucho.com/mailman/listinfo/resin-interest


_______________________________________________
resin-interest mailing list
resin-interest@caucho.com
http://maillist.caucho.com/mailman/listinfo/resin-interest




_______________________________________________
resin-interest mailing list
resin-interest@caucho.com
http://maillist.caucho.com/mailman/listinfo/resin-interest


_______________________________________________
resin-interest mailing list
resin-interest@caucho.com
http://maillist.caucho.com/mailman/listinfo/resin-interest

Reply via email to