技术开发 频道

WebSphere使用初探之数据连接池

    【IT168 技术文章】

    IBM WebSphere Application Server (WebSphere应用服务器)是一个Web应用服务器(内含IBM Http Server),它本质上是适合于servlet的Web服务器插件,提供了增强的Servlet API和Servlets管理工具,并集成了JSP技术和数据库连接技术。

    由于项目的需要,需要将某一部分的web应用移植到WebSphere Application Server上。这几天我作为项目经理对这部分的工作进行了初步的探索。

    我先在www.ibm.com中下载了WebSphere Application Server6.0的试用版。在安装了WebSphere以后,打开Jbuilder X发现这个版本的Jbuilder不支持WebSphere Application Server6.0,因此及其郁闷的安装了Jbuilder2006。

    现在可以确认的开发环境如下:

    系统平台:Windows2000 Server

    数据库:Oracle9I

    应用服务器:WebSphere Application Server6.0 for windows

    编程环境:Jbuilder2006

    因为如果进行Web项目的移植,最先需要确认其对数据库访问的正确性。因此需要在安装的WebSphere Application Server先设置一个数据库连接池,以保证Web应用程序对数据库访问的可行性。需要注意的是在6.0版本中需要在数据源中为该数据源设置一个J2EE 连接器体系结构(J2C)认证。在设置了连接数据源的用户名和密码以后,在数据源的组件管理的认证别名中选择刚才设置的认证。在以上配置工作完成以后就可以对该数据连接池进行连接测试了。

    在数据连接池测试成功以后,就需要考虑在Web应用程序如何取调用该数据连接池进行数据库连接了。访问应用服务器的数据连接池肯定是通过JNDI进行访问。

    主要程序如下:

    Connection conn;

    Statement stmt = null;

    DataSource ds = null;

    ResultSet rs = null;

    try {

    ht.put(Context.INITIAL_CONTEXT_FACTORY,

    "com.ibm.websphere.naming.WsnInitialContextFactory");

    ht.put(Context.PROVIDER_URL, "iiop://192.168.1.224:2809/");

    Context ctx = new InitialContext(ht);

    Object obj = ctx.lookup("Pubtest");

    ds = (DataSource) obj;

    conn = ds.getConnection();

    stmt = conn.createStatement();

    rs = stmt.executeQuery("SELECT * FROM EPUB.SYS_LOGIN_USER");

    while (rs.next()) {

    out.println(rs.getString("USER_CODE"));

    out.println(rs.getString("PWD"));

    out.println(rs.getDate("UP_DATE"));

    out.println(rs.getString("OPR_CODE"));

    out.println(rs.getString("NOTE"));

    }

    if (stmt != null) {

    stmt.close();

    }

    if (conn != null) {

    if (!conn.isClosed()) {

    conn.close();

    }

    }

    }

    catch (Exception ex) {

    // System.err.println("建立数据库连接错误:" + ex.getMessage());

    out.print("建立数据库连接错误:" + ex.getMessage()); //输出到客户端

    ex.printStackTrace();

    }

    在程序编译成功以后,执行出现如下错误:

    javax.naming.NamingException: Failed to initialize the ORB [Root exception is java.lang.ClassCastException: com.sun.corba.se.impl.orb.ORBImpl]

    at com.ibm.ws.naming.util.Helpers.getOrb(Helpers.java:294)

    at com.ibm.ws.naming.util.WsnInitCtxFactory.getInitialContextInternal(WsnInitCtxFactory.java:373)建立数据库连接错误:Failed to initialize the ORB

    at com.ibm.ws.naming.util.WsnInitCtx.getContext(WsnInitCtx.java:112)

    at com.ibm.ws.naming.util.WsnInitCtx.getContextIfNull(WsnInitCtx.java:422)

    at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:143)

    at javax.naming.InitialContext.lookup(InitialContext.java:351)

    at testwebsphere.test.main(test.java:88)

    ……………

   

在查询了众多的资料以后发现只有使用IBM WebSphere6.0自身带的JDK才可以避免以上的错误。

    在更改了JDK以后,结果又出现了新的错误

    java.lang.ClassCastException: javax.naming.Reference

    at com.ibm.rmi.javax.rmi.PortableRemoteObject.getObjectImpl(PortableRemoteObject.java:614)

    at com.ibm.rmi.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:339)

    at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:157)建立数据库连接错误:javax.naming.Reference

    at testwebsphere.test.main(test.java:89)

    这个问题可头疼了,我查询了网络,就是没有找到解决办法。最后在经过2天的反复测试和试验后,在找到了原因。

    上面的代码需要做如下的修改

    ht.put(Context.INITIAL_CONTEXT_FACTORY,

    "com.ibm.websphere.naming.WsnInitialContextFactory");

    ht.put(Context.PROVIDER_URL, "iiop://192.168.1.224:2809/");

    Context ctx = new InitialContext(ht);

    以上语句删除,添加一条语句

    Context ctx = new InitialContext();

    然后在工程生成WAR文件以后发布在WebSphere上,通过网页进行调用,就成功了。

    总结:WebLogic 的Web应用数据连接池调用机制和WebSphere不同,WebLogic是通过T3协议可以直接访问应用服务器上的JNDI资源,而WebSphere需要将资源调用的bean已JAR应用的方式发布到WebSphere上,因为它是在本地所以不需要设置,只需要读取本地的InitialContext()然后Lookup相应的JNDI接口就可以。
 

0
相关文章