users@glassfish.java.net

Problem in simultaneous insert in same table using JPA

From: <glassfish_at_javadesktop.org>
Date: Wed, 15 Sep 2010 06:42:07 PDT

Description: Here is a for loop. that means if 'for loop' is running 5 times - there will be 5 times insert/update in a same table. Parameters of the function are array of string.
But i bring 3 sets of data in array of string - that means for loop will run 3 times - and it should 'insert' 3 times in same table simultaneously. but interestingly it is inserting just for first time and ignoring others. But i have checked that value is changing for every time.
 
 
        public String updateInsertInv(int productNo, int timekey, String caller_pid[], String caller_amt[], String callerUpIn[])
                throws ClassNotFoundException, SQLException
        {
            EntityTransaction et = em.getTransaction();
        try {
            et.begin();
            int len = productNo;
            int b=0;
           // Daily_inventory c[] = new Daily_inventory[100];
            Daily_inventory c = new Daily_inventory();
            
            int tkey = 0, productID = 0 , k =0;
            float begin_inv = 0, purchase_amt=0, end_inv=0,the_amt=0,waste =0,missing=0;
            String tt = "suv";
            for(int a=0; a<len;a++)
            {
                tkey = timekey;
                productID = Integer.parseInt(caller_pid[b]);
                the_amt = Integer.parseInt(caller_amt[b]);
                
                if(callerUpIn[b].equals("update"))
                {
                    //update
                }
                else if(callerUpIn[b].equals("insert"))
                {
                    //insert
                     c.setTime_id(tkey);
                     c.setProduct_id(productID);
                     c.setBegin_inv(begin_inv);
                     c.setPurchase(purchase_amt);
                     c.setEnd_inv(end_inv);
                     c.setTheoratical_amt(the_amt);
                     c.setWaste(waste);
                     c.setMissing(missing);
                    
                    em.persist(c);
                    }
                    
                     tt = tt + tkey + productID + the_amt + callerUpIn[b]+" ";
                }
                b=b+1;
            }
            et.commit();
            return tt;
        }

        catch (Exception e) {
                        e.printStackTrace();
                        return e.getMessage();
        }
            
        }
[Message sent by forum member 'suvsuv']

http://forums.java.net/jive/thread.jspa?messageID=482832