persistence@glassfish.java.net

threads and transactions

From: Allan Juul <gf_at_muly.dk>
Date: Tue, 07 Jul 2009 17:16:24 +0200

hi,

from a servlet I start a transaction much like the code below.
this code works beuatifully in the way that i get data inserted/updated
into my database via my entities.

my problem is that my updates will happen from 2 threads, so i am afraid
  that my transaction will not commit. for instance i will get this kind
of errors.

        "Nested transaction not supported"


so my question really is, how do i make sure this scenario is thread and
  transaction safe, ensuring that all transactions commit ok ?


./allan

************************* pseudo code **************************


@PersistenceContext( name = "GFPU", unitName = "GFPU")
public class myServlet extends HttpServlet {
     @Resource
     UserTransaction _utx;
     ...


protected void doProcess(HttpServletRequest request, HttpServletResponse
  response) throws ServletException, IOException {

     String pc_jndi = "my_jndi_name";
     InitialContext ic = new InitialContext();
     Object ot = ic.lookup(pc_jndi);
     _em = (EntityManager) ot;
     ...

     ...

     MyThread instance1 = new AggregatorThread(_utx, _em, 1);
     Thread thread1 = new Thread( instance1 );
     thread1.start();

     MyThread instance2 = new AggregatorThread( _utx, _em, 2);
     Thread thread2 = new Thread( instance2 );
     thread2.start();