李振海的gravatar头像
李振海 2017-12-15 22:42:54

mybatis多表查询问题

		WechatUser currentWechatUser = wechatUserDao.selectByPrimaryKey(wechatUserId);
		Business currentBusiness = businessDao.selectByPrimaryKey(businessId);
		Weishangqiang currentWeishangqiang = weishangqiangDao.selectByPrimaryKey(businessWeishangqiangDao.selectByBusinessId(currentBusiness.getId()).getWeishangqiangId());
		
		List<WeishangqiangContent> currentWeishangqiangContentList = weishangqiangContentDao.selectByWeishangqiangIdInOrderCreateTimeAsc(currentWeishangqiang.getId(), lastTime);
		List<WeishangqiangContentResult> currentWeishangqiangContentResultList = new ArrayList<WeishangqiangContentResult>();
		int maxId = 0;
		int length = currentWeishangqiangContentList.size();
		int start = 0;
		if (length > 10) {
			start = length - 10;
		}
		for (int i = start; i < length; i++) {
//		for (WeishangqiangContent weishangqiangContent : currentWeishangqiangContentList) {
			WeishangqiangContent weishangqiangContent = currentWeishangqiangContentList.get(i);

			if (maxId < weishangqiangContent.getId()) {
				maxId = weishangqiangContent.getId();
			}
			WeishangqiangContentResult weishangqiangContentResult = new WeishangqiangContentResult();
			weishangqiangContentResult.setId(weishangqiangContent.getId());
			weishangqiangContentResult.setWechatUser(wechatUserDao.selectByPrimaryKey(weishangqiangContent.getWechatUserId()));
			weishangqiangContentResult.setWeishangqiangId(weishangqiangContent.getWeishangqiangId());
			weishangqiangContentResult.setType(weishangqiangContent.getType());
			if (weishangqiangContent.getType() == 1) {
				weishangqiangContentResult.setOwnScreen(ownScreenDao.selectByPrimaryKey(weishangqiangContent.getOwnScreenId()));
				if (weishangqiangContent.getImgUrl() != null && !"".equals(weishangqiangContent.getImgUrl())){
					weishangqiangContentResult.setImgUrl(weishangqiangContent.getImgUrl().split(","));
				}
				weishangqiangContentResult.setVideoUrl(weishangqiangContent.getVideoUrl());
			} else if (weishangqiangContent.getType() == 2) {
				Dashang dashang = dashangDao.selectByPrimaryKey(weishangqiangContent.getDashangId());
				DashangTar dashangTar = dashangTarDao.selectByPrimaryKey(weishangqiangContent.getDashangTarId());
				
				weishangqiangContentResult.setDashang(dashang);
				weishangqiangContentResult.setDashangTar(dashangTar);
			} else if (weishangqiangContent.getType() == 3) {
				weishangqiangContentResult.setGift(giftMapper.selectByPrimaryKey(weishangqiangContent.getOwnScreenId()));
				weishangqiangContentResult.setToWechatUser(wechatUserDao.selectByPrimaryKey(weishangqiangContent.getToWechatUserId()));
			} else if (weishangqiangContent.getType() == ActionConsts.RED_PACKAGE_TYPE) {
				weishangqiangContentResult.setRedPackage(redPackagesMapper.selectByPrimaryKey(weishangqiangContent.getContent()));
			} else if (weishangqiangContent.getType() == ActionConsts.EXPRESS_LOVE_TYPE) {
				weishangqiangContentResult.setImgUrl(weishangqiangContent.getImgUrl().split(","));
				weishangqiangContentResult.setVideoUrl(weishangqiangContent.getVideoUrl());
				ExpressLove el = expressLoveMapper.selectByPrimaryKey(weishangqiangContent.getOwnScreenId());
				String name = ElThemeConsts.THEMEMAP.get(weishangqiangContent.getThemeId() + "");
				el.setName(name);
				weishangqiangContentResult.setExpressLove(el);
				weishangqiangContentResult.setToWechatUser(wechatUserDao.selectByPrimaryKey(weishangqiangContent.getToWechatUserId()));
			} 
			weishangqiangContentResult.setContent(weishangqiangContent.getContent());
			weishangqiangContentResult.setCreateTime(new SimpleDateFormat("MM-dd HH:mm").format(weishangqiangContent.getCreateTime()));
			
			currentWeishangqiangContentResultList.add(weishangqiangContentResult);
		}
		BusinessWechatUser businessWechatUser = new BusinessWechatUser();
		businessWechatUser.setBusinessId(businessId);
		businessWechatUser.setWechatUserId(wechatUserId);
		businessWechatUser = businessWechatUserDao.selectByBusinessId(businessWechatUser);
		
		List<Dashang> dashangList = dashangDao.selectListByWeishangqiangId(currentWeishangqiang.getId());
		List<DashangTar> dashangTarList = dashangTarDao.selectList();
		List<Gift> giftList = giftMapper.selectListByWeishangqiangId(currentWeishangqiang.getId());
		
		
		int currentWeishangqiangAccessAccount = Integer.valueOf(currentWeishangqiang.getAccessAccount());		
		currentWeishangqiang.setAccessAccount(String.valueOf(currentWeishangqiangAccessAccount + 1));
		weishangqiangDao.updateByPrimaryKeySelective(currentWeishangqiang);
		
		List<String> listWechatId = orderDao.getPayMostTopUser(10, businessId);
		if (listWechatId.contains(wechatUserId)) {
			int haobang = listWechatId.indexOf(wechatUserId + "") + 1;
			model.addAttribute("haobang", haobang);
		} else {
			model.addAttribute("haobang", 11);
		}
		
		if (currentWeishangqiangContentList.size() == 0) {
			Integer value = weishangqiangContentDao.selectMaxId(currentWeishangqiang.getId());
			maxId = value == null ? 0 : value;
		}
		
		model.addAttribute("maxId", maxId);
		
		model.addAttribute("timestamp", DateUtil.getCurrentTime());
		model.addAttribute("currentWechatUser", currentWechatUser);
		model.addAttribute("currentBusiness", currentBusiness);
		model.addAttribute("businessWechatUser", businessWechatUser);
		model.addAttribute("currentWeishangqiang", currentWeishangqiang);
		model.addAttribute("currentWeishangqiangContentResultList", JSON.toJSONString(currentWeishangqiangContentResultList));
		
		model.addAttribute("dashangList", JSON.toJSONString(dashangList));
		model.addAttribute("dashangTarList", JSON.toJSONString(dashangTarList));
		model.addAttribute("giftList", JSON.toJSONString(giftList));
		model.addAttribute("expressLoveList", JSON.toJSONString(expressLoveMapper.getExpressListByWeishangqiangId(currentWeishangqiang.getId())));
		
		model.addAttribute("shortUrl", shortUrlMapper.selectByPrimaryKey(businessId).getShorturl());
		
		List<Map<String,Object>> list = orderDao.payMostWechatUser(businessId);
		if (list.size() > 0) {
			model.addAttribute("payMost", list.get(0));
		} else {
			model.addAttribute("payMost", null);
		}
		
		return "/customer/weishangqiang";
	}

这是公司以前人写的代码,昨天晚上项目报

Could not get JDBC Connection; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection,  message from server: "Too many connections"] with root cause
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection,  message from server: "Too many connections"

请大神看看和这块代码关系大吗

所有回答列表(2)
码农_老王的gravatar头像
码农_老王  LV10 2017年12月16日

产生这种问题的原因是:

连接数超过了 MySQL 设置的值,与 max_connections 和 wait_timeout  都有关系。wait_timeout 的值越大,连接的空闲等待就越长,这样就会造成当前连接数越大。

解决方法:

修改MySQL配置文件/etc/my.cnf,设置成max_connections=1000,wait_timeout=5。如果没有此项设置可以自行添加,修改后重启MySQL服务即可。要不经常性报此错误,则要对服务器作整体性能优化。

总结,解决问题的最终方法:

1.修改配置文件/etc/my.cnf,调整连接参数

2.检查程序代码,对于没有关闭的链接及时进行关闭

评论(1) 最佳答案
liuhaitie的gravatar头像
liuhaitie  LV14 2018年2月3日

数据库连接有个连接数设置,,,你这个是因为连接满了,,修改下连接数

顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友