您好我正在使用Hibernate更新表中的记录。我在另一个表中插入相同的记录。它在一个循环中,但是当我更新记录时,我得到异常作为锁等待超时异常。请有人能解决这个问题吗?提前致谢!
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();
}
这是我的代码..
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();
}
答案 0 :(得分:0)
您已在开始时sess.beginTransaction();
初始化了事务,在提交事务之前,您尝试重新初始化事务。这将导致内存泄漏,因为之前的事务尚未提交。因此,在开始另一个事务之前,请提交前一个事务。
以下是一些建议:
请查看this了解详情。
答案 1 :(得分:0)
在开启新交易之前提交交易
Transaction currentTx = sess.beginTransaction();
..
currentTx.commit();
..
currentTx = sess.beginTransaction();
编辑: 在dao中你打开新事务而不是使用前一个事务..你应该阅读一些关于java / hibernate中事务管理的教程。