日期:2014-05-19  浏览次数:20703 次

求高手指点这是啥意思
private static DataSource initDataSource(String dsId) {

DataSource ds = null;
try {
if (webserverType == null || webserverType.equals(""))
webserverType = "2";
if (webserverType.equals("1")) // tomcat resin
{
Context ctx = (Context) (new InitialContext())
.lookup("java:comp/env");
ds = (DataSource) ctx.lookup(dsId);
System.out.println("Datasource(tocmat resin) is ok....." + ds);

} else

{
System.out.println(dsId);

ds = (DataSource) (new InitialContext()).lookup(dsId);
System.out.println("Datasource(weblogic websphere) is ok....."
+ ds);

}
} catch (Exception e) {
System.out.println("取得数据源名称发生错误!");
e.printStackTrace();
}
return ds;
}

------解决方案--------------------
DataSource ds = null; //定义数据源变量
try {
if (webserverType == null || webserverType.equals(""))//webserverType服务器类型,估计是自定义变量,如果为空,赋值为2
webserverType = "2";
if (webserverType.equals("1")) // tomcat resin //如果为1,表示为TomCat服务器
{
Context ctx = (Context) (new InitialContext()) //初始化上下文,java:comp/env是JDNI的环境变量
.lookup("java:comp/env");
ds = (DataSource) ctx.lookup(dsId); //用ctx得到数据源
System.out.println("Datasource(tocmat resin) is ok....." + ds);//输出数据源状态

} else //else同上,这里有个BUG,应该剔除webserverType = "2";

{
System.out.println(dsId);

ds = (DataSource) (new InitialContext()).lookup(dsId);
System.out.println("Datasource(weblogic websphere) is ok....."
+ ds);

}
} catch (Exception e) { //扑捉异常
System.out.println("取得数据源名称发生错误!");
e.printStackTrace();
}
return ds;
}