WebSphere使用初探——資料連線池
來源:互聯網
上載者:User
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應用程式對資料庫訪問的可行性。具體的JNDI設定請參看下面網頁http://www.matrix.org.cn/thread.shtml?topicId=30665&forumId=40。需要注意的是在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介面就可以。