0

Hi I am using Hibernate to update the records in a table. And I'm inserting same records in another table. It is in a loop, but I am getting exception as lock wait timeout exception when I am updating records. Please could anybody resolve this problem? Thanks in advance!

try {
            SalesInventoryDAO dao = new SalesInventoryDAO();
            sess = HibernateUtil.getSessionFactory().openSession();
           Transaction tx = ses.beginTransaction();
            GoodsRecievedForm item = (GoodsRecievedForm) form;
            GoodsRecieved bk = new GoodsRecieved();
            bk.setGoodsId(item.getGoodsId());
            InventoryOrder order = (InventoryOrder) sess.get(InventoryOrder.class, item.getOrderNo());
            bk.setOrderNo(order);
//            if (order.getQuotation().getQuotationNo() != null) {
//                bk.setQuotation(order.getQuotation().getQuotationNo());
//            } else {
//                bk.setQuotation(null);
//            }

            java.util.Date temp = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH).parse(item.getRecievedDate());
            java.sql.Date temp1 = new java.sql.Date(temp.getTime());
            bk.setRecievedDate(temp1);
            bk.setOrderQty(order.getTotalqty());
            bk.setReceivedPersonName(item.getReceivedPersonName());
            bk.setReceivedQty(item.getReceivedQty());
            bk.setConditionOfMaterial(item.getConditionOfMaterial());
            UserEntity msg;
            HttpSession session = request.getSession(false);
            msg = (UserEntity) session.getAttribute("user");
            bk.setAddedBy(msg);
            bk.setAddedDate(new Date());
            int[] item1111 = item.getGoodsDetails();
            String[] productre = item.getGoodsDetailsName();
            float proqty[] = item.getGoodsDetailsQty();
            float price[] = item.getGoodsDetailsPrice();
            float receivedqty[] = item.getReceivedquantity();
            GoodsReceivedDetails mb;
            Set<GoodsReceivedDetails> purDetails = new HashSet();
            for (int i = 0; i < productre.length; i++) {
                mb = new GoodsReceivedDetails();
                mb.setGoodsDetailsName(productre[i]);
                mb.setGoodsDetailsQty(proqty[i]);
                mb.setGoodsDetailsPrice(price[i]);
                mb.setReceivedquantity(receivedqty[i]);
                //System.out.println("productre" + productre[i]);
                int id3 = item1111[i];
                //System.out.println("id3id3id3id3" + id3);
                // int id3 = Integer.parseInt(productre[i]);
                Item idf = (Item) sess.get(Item.class, id3);
                float qty = (idf.getItemStock() + mb.getReceivedquantity());
//                mb.setItemId(idf);
//                mb.setItemId(idf);
                dao.updateitem(qty, idf);
                //dao.updateitem(idf);
                mb.setGoodsId(bk);
                sess.save(mb);
                purDetails.add(mb);
            }

            bk.setGoodsDetails(purDetails);
            sess.save(bk);
          tx.commit;

            //System.out.println("comming");
//            List ls = gdao.getOrderItems(order.getOrderId());
//            for (Iterator it = ls.iterator(); it.hasNext();) {
//                InventoryOrderDetails inv = (InventoryOrderDetails) it.next();
//                gdao.updateitem(inv.getItemId().getItemStock() + bk.getReceivedQty(), inv.getItemId());
//            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sess.close();
        }

This is my dao code..

public void updateitem(float stock, Item itm) {
        Session ses = HibernateUtil.getSessionFactory().openSession();
        ////System.out.println("itmitmitm" + itm.getItemId());
        Transaction tx = ses.beginTransaction();
        Query qry = ses.createQuery("UPDATE Item set itemStock='" + stock + "' where itemId='" + itm.getItemId() + "'");
        qry.executeUpdate();
        ses.close();
        tx.commit();

    }
user2838630
  • 91
  • 1
  • 5
  • 15
  • You're creating a whole new session again in the DAO! Since you already have an existing session, why don't you use the same? Pass the same session to the DAO method and use that session in the DAO method. A session itself can have multiple transactions. So, create a session once, use multiple transactions within, if necessary. This would solve your issue. – Amith Koujalgi Oct 29 '13 at 07:27
  • Now it is giving me the other exception as object references an unsaved transient instance - save the transient instance before flushing: com.treamis.inventory.goodsReceived.GoodsRecieved thanks – user2838630 Oct 29 '13 at 09:35
  • First things first. It seems that you're not utilizing the Hibernate's main feature. In the DAO, you've written a query to update an entity called Item. Instead, if you have setup the persistance mapping to the Item entity with Hibernate, you actually don't have to write an update query. Hibernate does an update for you so easily that you just need to use sess.update(item). This will be converted to native SQL query internally and will be persisted. – Amith Koujalgi Oct 29 '13 at 10:46
  • 1
    And for the unsaved transient instance exception: You should include cascade="all" (if using xml) or cascade=CascadeType.ALL (if using annotations) on your collection mapping. This happens because you have a collection in your entity, and that collection has one or more items which are not present in the database. By specifying the above options you tell hibernate to save them to the database when saving their parent. – Amith Koujalgi Oct 29 '13 at 10:50

2 Answers2

0

You have initialized a the transaction by sess.beginTransaction(); in the beginning and before even committing the transaction, you've trying to re-initialize the transaction. This will lead to memory leaks as the previous transactions hasn't been committed. So, before you begin another transaction, commit the previous one.

And here are some suggestions:

  • ‘Lock wait timeout’ occurs typically when a transaction is waiting on row(s) of data to update which is already been locked by some other transaction.
  • Most of the times, the problem lies on the database side. The possible causes may be a inappropriate table design, large amount of data, constraints etc.

Please check out this for more details.

Community
  • 1
  • 1
Amith Koujalgi
  • 10,746
  • 2
  • 18
  • 22
0

Commit transaction before opening new one

Transaction currentTx = sess.beginTransaction();
..

currentTx.commit();
..
currentTx = sess.beginTransaction();

EDIT: In dao you opening new transaction instead of use previous one.. you should read some tutorials about transaction management in java/hibernate.

smajlo
  • 972
  • 10
  • 21