Re: [Resin-interest] Hibernate transactions

2010-03-30 Thread Stargazer
On 30-Mar-2010 06:42, Wesley Wu wrote:
  try {
  ut.begin();
  CourseBean updateCourse = _manager.find(CourseBean.class, new
 Integer(1));
  updateCourse.setCourse(Magic);
  ut.commit();
  } catch (Exception e) {
  e.printStackTrace();
  }

 will work.

 -Wesley



Thanks, but theres clearly something basic wrong here as that change 
made no difference. If I add

property name=hibernate.show_sql value=true/

to persistence.xml I see in the console

Hibernate: select coursebean0_.id as id0_0_, coursebean0_.course as 
course0_0_, coursebean0_.teacher as teacher0_0_ from basic_courses 
coursebean0_ where coursebean0_.id=?
Hibernate: select coursebean0_.id as id0_0_, coursebean0_.course as 
course0_0_, coursebean0_.teacher as teacher0_0_ from basic_courses 
coursebean0_ where coursebean0_.id=?

But no attempt at a write. Single stepping the code proves the 
transaction is being executed.

 ___
 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


Re: [Resin-interest] Hibernate transactions

2010-03-30 Thread Wesley Wu
To make set method auto translated into a UPDATE clause, the
entitymanager should be opened after a transaction begins.

@PersistenceUnit(unitName=example)
EntityManagerFactory emf;

EntityManager em;
try {
   ut.begin();
   EntityManager 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
resin-interest@caucho.com
http://maillist.caucho.com/mailman/listinfo/resin-interest


Re: [Resin-interest] Hibernate transactions

2010-03-30 Thread Stargazer
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
resin-interest@caucho.com
http://maillist.caucho.com/mailman/listinfo/resin-interest


Re: [Resin-interest] Hibernate transactions

2010-03-30 Thread Wesley Wu
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 starga...@blueyonder.co.uk

 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
 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


Re: [Resin-interest] Hibernate transactions

2010-03-30 Thread Scott Ferguson
Wesley Wu wrote:
 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.
Why would calling UserTransaction in your code be faster? Essentially, 
all @TransactionAttribute does is call UserTransaction.begin() and 
commit(). (Any extra overhead should be minimal, especially compared to 
the actual transaction.)

-- Scott

 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 starga...@blueyonder.co.uk 
 mailto:starga...@blueyonder.co.uk

 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
 resin-interest@caucho.com mailto: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


Re: [Resin-interest] Hibernate transactions

2010-03-30 Thread Stargazer
On 30-Mar-2010 17:54, Wesley Wu wrote:
 Yes. One minor problem:

 @PersistentContext should be @PersistentUnit.

Doh!
Great, works now. Thanks for your help!




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


Re: [Resin-interest] Hibernate transactions

2010-03-30 Thread Stargazer
On 30-Mar-2010 18:03, Scott Ferguson wrote:
 Wesley Wu wrote:

 Yes. One minor problem:

 @PersistentContext should be @PersistentUnit.
  
   would calling UserTransaction in your code be faster? Essentially,

snip

I don't know about the finer details, but looking back on this now it's 
fixed it seems the example is really only half done. A fuller one 
showing not just how to read the db but how to update it would be more 
real-world and less frustrating for those making the switch.




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


Re: [Resin-interest] Hibernate transactions

2010-03-30 Thread Wesley Wu
I will not call UserTransaction.begin() when all db operations are SELECT.
I create a EntityManager from emf to do SELECT stuff with no transaction at
all.

When I detected there're some update/delete operations, I'll do this:
1. close the former created EntityManager if there is one (for precedent
SELECT op)
2. call UserTransaction.begin()
3. create a new EntityManager
4. do left jobs
5. commint() on success or rollback() on failure.
*. finally (always) close the em (bounded to current thread) and set current
ThreadLocalEntityManger to null.

This worked for me in the last three years in several heavy loaded websites.

-Wesley

2010/3/31 Scott Ferguson f...@caucho.com

 Why would calling UserTransaction in your code be faster? Essentially,
 all @TransactionAttribute does is call UserTransaction.begin() and
 commit(). (Any extra overhead should be minimal, especially compared to
 the actual transaction.)

 -- Scott


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


Re: [Resin-interest] Hibernate transactions

2010-03-30 Thread Wesley Wu
More to mention about MDBs (Message Driven Beans):

1. Always use @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
on public void onMessage(Message message).
because the impl of Resin MDBs has severe reenter synchronization problem.
If I start a transaction before onMessage() being called there would be
several transactions tried to crossing commit in unexpected manner.

2. Inject a ExecutorService to do the real stuff.

public class MyMessageDrivenBean implements MessageListener {
@Inject
ExecutorService executorService;
private InjectManager _webBeans = InjectManager.create();

@Override
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public void onMessage(Message message) {
 RealStuffRunnable runnable = webBeans.getReference(RealStuffRunnable
.class);
 // set some properties of runnable
 ...
 // run it in a new thread
 executorService.execute(runnable);
}
}

3. Use @TransactionAttribute on RealStuffRunnable.run()

Thus my real stuff will not suffer from the reenter and crossing commit.

Any better solutions?

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


Re: [Resin-interest] Hibernate transactions

2010-03-29 Thread Stargazer
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


Re: [Resin-interest] Hibernate transactions

2010-03-29 Thread Wesley Wu
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.

-Wesley

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


Re: [Resin-interest] Hibernate transactions

2010-03-29 Thread Scott Ferguson
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
 -Wesley

 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


Re: [Resin-interest] Hibernate transactions

2010-03-29 Thread Stargazer

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/HibernateIt 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 Detailsbr/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 Stargazerstarga...@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


[Resin-interest] Hibernate transactions

2010-03-26 Thread Stargazer
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


Re: [Resin-interest] Hibernate transactions

2010-03-26 Thread Rick Mann
Also, the link at the very bottom of that page gets a 500 error:

http://www.caucho.com/resin/examples/amber-basic/index.xtp

 500 Servlet Exception
 
 'ejb3_basic_courses' is not a valid database table.  Either the table needs
 to be created or the create-database-tables attribute must be set.
 
 com.caucho.db.sql.SQLParseException: 'ejb3_basic_courses' is an unknown
 table.  'FROM table' requires an existing table.
 select 1 from ejb3_basic_courses o where 1=0
 
 Resin/4.0.s100325 Server: ''



On Mar 26, 2010, at 18:10:01, 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



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