spring定時器時batch批量操作出現找不到session的解決辦法

<pre name="code" class="java">Session session = sessionFactory.openSession();
獲取session 解決了找不到session的問題。

/**
	 * @Title: batchUpdateOrder
	 * @Description: 未支付超過2小時,訂單關閉
	 * @param: @return  設定文件
	 * @return: Integer  返回類型
	 * @throws
	 */
	public Integer batchUpdateOrder(){
		Integer result=0;
		
		Connection conn = null; 
		PreparedStatement pstmt = null; 
		ResultSet rs=null;
		//未支付訂單集合
		
		//超過2個小時未支付,訂單自動關閉
		String sql="update order_details d,order_master_info m set d.order_status=4,m.order_status=2 "+
                " where d.order_master_id=m.id and d.id=0 ";
        //遍歷分銷商,保存到分期賬單表中		
		try{	
			Session session = sessionFactory.openSession();
			conn = SessionFactoryUtils.getDataSource(sessionFactory).getConnection(); 
			conn.setAutoCommit(false); 
			pstmt = conn.prepareStatement(sql); 
			
			String sqls="select * from order_details where order_status=0";
			List<OrderDetails> detailList=session.createSQLQuery(sqls).addEntity(OrderDetails.class).list();
			
			for(OrderDetails order:detailList){
				//現在時間 - 訂單創建時間>=2
				Date currentDate=new Date();
				Long time1=currentDate.getTime()-order.getCreateDate().getTime();
				long nh = 1000*60*60;//一小時的毫秒數
				long hour = time1/nh;//計算差多少小時
				
				conn.setAutoCommit(false); // 開始事務    
				//系統配置支付間隔數
				Integer payInterval=SystemConfigUtil.getSystemConfig().getOrderPayInterval();
				if(payInterval.equals(null) || payInterval==0){
					payInterval=2;
				}
				if(hour>=payInterval){
					pstmt.setInt(1, order.getId());    
				}
				pstmt.addBatch();  
			} 
			pstmt.executeBatch(); 
			conn.commit(); 
			conn.setAutoCommit(true); 
		} catch (SQLException e) { 
			if (conn != null) { 
				try { 
				conn.rollback(); 
				} catch (SQLException e1) { 
				e1.printStackTrace(); 
				} 
			} 
		} finally { 
			if (pstmt != null) { 
				try { 
				pstmt.close(); 
				} catch (SQLException e) { 
				e.printStackTrace(); 
				} 
			} 
			if (conn != null) { 
				try { 
				conn.close(); 
				} catch (SQLException e) { 
				e.printStackTrace(); 
				} 
			} 
		} 
		return result;
	}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章