threads and transactions

View: New views
2 Messages — Rating Filter:   Alert me  

threads and transactions

by Allan Juul-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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();





Re: threads and transactions

by Marina Vatkina :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

You need to create/lookup a separate EM in each thread - EMs are not thread safe.

Regards,
-marina

Allan Juul wrote:

> 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();
>
>
>
>