NutzCN Logo
问答 关于daoUp 关闭问题
发布于 2558天前 作者 nutzJ 1828 次浏览 复制 上一个帖子 下一个帖子
标签:

连接方法

public class ConUtil {
	
	private static Map<String, DaoUp> daoUpMap = new HashMap<String, DaoUp>();

	public static DaoUp getNewConnect(String datasource) {
		DaoUp daoUp = null;
		if(daoUpMap.get(datasource) != null){
			return daoUpMap.get(datasource);
		}else{
			try {
				UrlDao urlDao = Mvcs.ctx().getDefaultIoc().get(UrlDao.class, "urlDao");
				if(eastUrlDao != null){
					Url url = urlDao.fetch(datasource);
					if(url != null){
						DruidDataSource druidDataSource = new DruidDataSource();
						druidDataSource.setDriverClassName(url.getDriver());
						druidDataSource.setUrl(url.getUrlStr());
						druidDataSource.setUsername(url.getUserName());
						druidDataSource.setPassword(uUrl.getPassword());
						druidDataSource.setMaxActive(250);
						druidDataSource.setMinIdle(5);
						
						druidDataSource.getConnection();
						daoUp = new DaoUp(datasource){
						};
						daoUp.setDataSource(druidDataSource);
						daoUpMap.put(datasource, daoUp);
						return daoUp;
						
					}else{
						return null;
					}
				}else{
					return null;
				}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		
		
		return daoUp;
		
	}
}

调用连接

	public Object getAllBm(@Param("token")String token,HttpServletRequest req){
		try{
			DaoUp daoUp = ConUtil.getNewConnect("YY");    //连接过后我还需要调用daoUp.close()吗?
			String sqlStr = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
			Sql sql = Sqls.create(sqlStr);
			sql.setCallback(new SqlCallback() {
				@Override
				public Object invoke(Connection conn, ResultSet rs, Sql sql)
						throws SQLException {
					List<Map<String, String>> branchList = new ArrayList<Map<String,String>>();
					while (rs.next()) {
						Map<String, String> branchMap = new HashMap<String, String>();
						branchMap.put("branchId", rs.getString("brnnbr"));
						branchMap.put("branchName", rs.getString("brnname"));
						branchMap.put("pId", rs.getString("upperbrn"));
						branchList.add(branchMap);
					}
					return branchList;
				}
			});
			daoUp.dao().execute(sql);
			List<Map> branchList = sql.getList(Map.class);
			return requestResult(true, branchList);
		}catch(Exception e){
			return requestResult(false, e.getMessage());
		}
	}

/连接过后我还需要调用daoUp.close()吗? daoUp.close() 关闭的又是什么?

6 回复

daoUp.close关闭的是DataSource实例, 程序关闭的时候才应该调用

也就是说在其他方法调用这个连接方法,其实不用关闭?那这个连接不就是一直存在了吗?

DruidDataSource是连接池啊... 如果你想用完就扔, 改成SimpleDataSource

好的,明白了谢谢!

那每个池子里的连接数,用完了不是要关闭的吗?

NutDao会自行处理连接的开启和关闭

添加回复
请先登陆
回到顶部