这个是连接的方法
public static DaoUp getNewConnect(String dataSource) {
try {
SysUrlDao sysUrlDao = Mvcs.ctx().getDefaultIoc().get(SysUrlDao.class,"sysUrlDao");
SysUrl sysUrl = sysUrlDao.dao().fetch(SysUrl.class,dataSource);
if(sysUrl != null){
DruidDataSource ds = new DruidDataSource();
ds.setDriverClassName(sysUrl.getDriver());
ds.setUrl(sysUrl.getUrlStr());
ds.setUsername(sysUrl.getUserName());
ds.setPassword(sysUrl.getPassword());
ds.getConnection();
DaoUp daoUp = new DaoUp("") {
};
daoUp.setDataSource(ds);
return daoUp;
}else {
return null;
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return null;
}
}
调用的方法(类似的使用)
public List<Record> getFXXX(@Param("customid") String customid){
DaoUp daoUp = null;
try {
if(daoUp == null || daoUp.getDataSource()==null || daoUp.dao() == null){
daoUp = SysConUtil.getNewConnect("FXSK"); //获取数据库远程连接
}
List<Record> fxxxLists = new ArrayList<Record>();
String sqlStr = "XXXXXXXXXXXXXXXXXXXXXXXXXx";
Sql sql = Sqls.create(sqlStr);
sql.setCallback(new SqlCallback() {
@Override
public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
// TODO Auto-generated method stub
ResultSetMetaData rsmd = rs.getMetaData();
int column = rsmd.getColumnCount();
List<Record> list = new LinkedList<>();
Record record = null;
while (rs.next()) {
record = new Record();
for (int i = 1; i <= column; i++) {
record.set(rsmd.getColumnName(i).toLowerCase(), rs.getString(i));
}
list.add(record);
}
return list;
}
});
daoUp.dao().execute(sql);
fxxxLists = sql.getList(Record.class);
return fxxxLists;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return null;
}finally {
daoUp.close();
}
}
因为需要连接多个数据库的原因(差不多4个),每次用完的话我都有close(),但是不知道什么原因,对数据的连接数一直不断,不停的增加增加,然后就会导致数据库没法用了。