关于请求并发的问题
我有个action方法是取消订单用的
在取消之前先判断订单状态是不是已取消
不是的话就改成已取消并且把订单的钱加到账户
但是测试的时候 连续快速的点两次取消
钱加了两次
有可能是什么原因引起的
是两次请求同时执行了?
action
先查询订单状态 判断是否已取消
然后
boolean res = this.ordersManager.cancelOrder(uporders);
dao层
public boolean cancelOrder(Orders orders) throws DataAccessException { Session session =null; Transaction tx = null; Balance bal = null; try{ session = hibernateTemplate.getSessionFactory().openSession(); tx = session.beginTransaction(); ClientLoginInfo clientLoginInfo = orders.getClientLoginInfo(); String manualName=orders.getClientLoginInfo().getClientUserName(); double money=orders.getSumMoney(); Company c = orders.getClientLoginInfo().getCompany(); Integer companyId = c.getCompanyId(); List list = this.hibernateTemplate.find( "from Balance as g where g.company.companyId=?", companyId); if (list.isEmpty()) { bal = new Balance(); bal.setCompany(c); bal.setBalanceMoney(money); session.save(bal); } else { bal= (Balance) list.get(0); bal.setBalanceMoney(bal.getBalanceMoney() + money); session.merge(bal); } session.merge(orders); tx.commit(); return true; }catch (DataAccessException e) { tx.rollback(); e.printStackTrace(); throw e; }finally{ if(session!=null) session.close(); } }
public static void main(String[] args) { synchronized (Test.class) { //you code here } }