(轉)WebSphere的web工程中怎么獲取數據源

原文:http://aguu125.iteye.com/blog/1694313?

?https://blog.csdn.net/bigtree_3721/article/details/44900325-------JNDI之java:comp/env

was配置數據源和tomcat是不同的。tomcat只需要配置tomcat 的service.xml或者content.xml,然后 WEB程序就不需要配置了。但是was不同.was 除了在控制臺配置數據源后,還需要在web.xml 和WEB-IBN.XML中配置

?

websphere 下獲取jndi,有兩種方式:java:comp/env/cas與jdbc/cas。?A.lookup("java:comp/env/cas")與lockup("jdbc/cas")在websphere中都可以使用。兩者的差別在于,java:comp/env/cas是websphere建議使用的方式 。

??? 如果你當前的線程屬于websphere的線程,建議使用java:comp/env/cas的方式,否則was的控制臺將報出警告。?
? ? 在web程序中,要實現通過java:comp/env/cas的方式來獲得jndi必須在web.xm和ibm-web-bnd.xmi文件里分別添加?

?web.xml:?

?

  1. <resource-ref id="ResourceRef_1129470735234">
  2. <res-ref-name>cas_ase</res-ref-name>
  3. <res-type>java.sql.DataSource</res-type>
  4. <res-auth>Container</res-auth>
  5. <res-sharing-scope>Shareable</res-sharing-scope>
  6. </resource-ref>


ibm-web-bnd.xmi:?

?

?

  1. <resRefBindings xmi:id="ResourceRefBinding_1129470735234"
  2. jndiName="jdbc/cas_ase">
  3. <bindingResourceRef href="WEB-INF/web.xml#ResourceRef_1129470735234" />
  4. </resRefBindings>


?以上兩段配置的意思是告訴web容器的上下文環境,將應用映射到的jndi資源。然后就可以通過lookup(“java:comp/env/cas/jdbc/cas_ase”)名來獲得數據源。?
???? 然而,如果你當前執行的線程不在was的容器內,比如說你通過web容器的線程新起了一個子線程,那么該線程將不在容器的上下文內,通過lookup(“java:comp/env/cas/jdbc/cas_ase”)名來獲得數據源?
???? 將報錯,這個時候你只能使用通用的獲取jndi資源的方式,就是通過lookup(“jdbc/cas”)來實現。“jdbc/cas”為你在was的資源設定的jndi名

?

?

java代碼:?

?

  1. package com;
  2. import java.sql.Connection;
  3. import java.util.Hashtable;
  4. import javax.naming.Context;
  5. import javax.naming.InitialContext;
  6. import javax.naming.NamingException;
  7. import javax.sql.DataSource;
  8. public class conn {
  9. public static void main(String[] args) throws Exception {
  10. InitialContext initialContext = getInitialContext();
  11. javax.sql.DataSource ds = (DataSource) initialContext.lookup("jdbc/cas_ase");
  12. Connection cn = ds.getConnection();
  13. if (cn != null){
  14. System.out.println("Connection ok");
  15. }
  16. }
  17. /* 因為此類不在Websphere服務器內部運行,所以需要配置環境變量,否則是可以省略的 */
  18. public static InitialContext getInitialContext() throws NamingException {
  19. Hashtable env = new Hashtable();
  20. env.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");
  21. env.put(Context.PROVIDER_URL, "iiop://localhost:2809"); // iiop是協議
  22. InitialContext context = new InitialContext(env);
  23. return context;
  24. }
  25. }


關于JNDI可以看:http://blog.csdn.net/lan861698789/article/details/26402935?

?

?

關于web.xml中參數的解釋:

?

 resource-ref元素用于指定對外部資源的servlet引用的聲明。<!ELEMENT resource-ref (description?, res-ref-name,
  1. res-type, res-auth, res-sharing-scope?)>
  2. <!ELEMENT description (#PCDATA)>
  3. <!ELEMENT res-ref-name (#PCDATA)>
  4. <!ELEMENT res-type (#PCDATA)>
  5. <!ELEMENT res-auth (#PCDATA)>
  6. <!ELEMENT res-sharing-scope (#PCDATA)>

resource-ref子元素的描述如下:?

 ● res-ref-name是資源工廠引用名的名稱。該名稱是一個與java:comp/env上下文相對應的JNDI名稱,并且在整個Web應用中必須是惟一的。 ?
● res-auth表明:servlet代碼通過編程注冊到資源管理器,或者是容器將代表servlet注冊到資源管理器。該元素的值必須為Application或Container。?
 ● res-sharing-scope表明:是否可以共享通過給定資源管理器連接工廠引用獲得的連接。該元素的值必須為Shareable(默認值)或Unshareable。

轉載于:https://www.cnblogs.com/liujiacai/p/10299408.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/278437.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/278437.shtml
英文地址,請注明出處:http://en.pswp.cn/news/278437.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

阿里數據庫內核月報:2017年04月

摘要&#xff1a;阿里數據庫內核月報&#xff1a;2017年04月# 01 MySQL 源碼分析 MySQL 半同步復制數據一致性分析# 02 MYSQL 新特性 MySQL 8.0對Parser所做的改進# 03 MySQL 引擎介紹 Sphinx源碼剖析&#xff08;二&#xff09;# 04 PgSQL 特性分析 checkpoint機制淺析…

SQL Server2008導入導出數據庫

一、導出數據庫 1.新建一個.bak的文本 右擊數據庫--》Tasks--》BackUp--》Remove原來的數據庫--》Add后選擇之前建立的.bak檔 二、導入數據庫 1.右擊數據庫--》Tasks--》Restore--》Database--》From device--》勾選數據庫之后--》點擊左上角Options--》點擊 --》OK覆蓋 轉載于…

如何使YouTube視頻連續循環播放

Should you need a YouTube video on a continuous loop, a few methods can help you keep repeating a video without having to start it over manually. Here’s how to do it. 如果您需要連續循環播放YouTube視頻&#xff0c;可以采用以下幾種方法來繼續播放視頻&#xff…

Android ABI

2019獨角獸企業重金招聘Python工程師標準>>> 獲取當前ABI var supportsABIs:Array<String>? null if(Build.VERSION.SDK_INT > 21) {supportsABIs Build.SUPPORTED_ABIS } var currentABI Build.CPU_ABI 通過Build可以獲取當前手機支持的abi集以及cpu的…

nginx限流健康檢查

Nginx原生限流模塊:ngx_http_limit_conn_module模塊根據前端請求域名或ip生成一個key&#xff0c;對于每個key對應的網絡連接數進行限制。配置如下:http模塊server模塊#http模塊內 http {include mime.types;default_type application/octet-stream;log_format main [$t…

如何在Ubuntu上創建桌面快捷方式

Desktop icons should be simple, but they’re not on Ubuntu 18.04 LTS and newer releases like Ubuntu 19.10. Follow these easy steps to get desktop shortcuts for your favorite applications, just like on other operating systems and other Linux desktops. 桌面圖…

阿里再破記錄!代表中國企業首次在這項國際比賽中摘得銀牌!

2月9日在洛杉磯舉行的第11屆網絡搜索與數據挖掘國際會議&#xff08;WSDM 2018&#xff09;上&#xff0c;公布了今年的WSDM Cup競賽成績&#xff0c;來自阿里巴巴的AliOS團隊憑借優秀的算法能力&#xff0c;摘得榜眼。這是該賽事舉辦11屆以來&#xff0c;中國企業在該賽事上首…

關于IE 對 $.get 緩存的記錄

最近在IE9中碰到一個問題是&#xff0c; 當我對某個角色進行修改的時候&#xff0c;再點擊查詢還是修改之前的內容&#xff0c;但是實際數據庫已經修改成功&#xff0c;糾結了好一會兒之后&#xff0c;才發現是 $.get請求的問題。 因為 IE對get請求&#xff0c;如果請求url 相…

閃存驅動器_將閃存驅動器變成便攜式Web服務器

閃存驅動器Portable applications are very useful for getting work done on the go, but how about portable servers? Here’s how you can turn your flash drive into a portable web server. 便攜式應用程序對于在旅途中完成工作非常有用&#xff0c;但是便攜式服務器呢…

Android中文API-ViewStub

ViewStub控件是一個不可見&#xff0c;0尺寸得惰性控件。當ViewStub控件設置可見&#xff0c;或者調用inflate()&#xff0c;并運行完畢之后&#xff0c;ViewStub所指定的layout資源就會被載入。這個ViewStub就會被新載入的layout文件取代。ViewStub也會從其父控件中移除。因此…

如何播放梅西百貨的感恩節大游行2019

Macy’s梅西百貨As we draw ever closer to the Thanksgiving holiday, multiple things come to mind: turkey, Black Friday, and the Macy’s Thanksgiving Day Parade. With that in mind, you might want to find a way to stream it for your family. 隨著我們越來越接近…

thinkpaidE480office安裝文件夾

C:\Program Files (x86)\Microsoft Office\root\Office16需要嵌入office的小伙伴自行百度吧教程吧&#xff0c;網上有很多的轉載于:https://www.cnblogs.com/hgyzm/p/10303449.html

AJAX入門這一篇就夠了

什么是Ajax Ajax(Asynchronous JavaScript and XML) 異步JavaScript和XML Ajax實際上是下面這幾種技術的融合&#xff1a; (1)XHTML和CSS的基于標準的表示技術(2)DOM進行動態顯示和交互(3)XML和XSLT進行數據交換和處理(4)XMLHttpRequest進行異步數據檢索(5)Javascript將以上技術…

如何在iPhone和iPad上允許“不受信任的快捷方式”

Khamosh PathakKhamosh PathakShortcuts is now a stock app in iOS 13, iPadOS 13, and beyond. Thanks to Apple’s stricter rules, any shortcut you download from the internet is blocked. Here’s how you can allow untrusted shortcuts on your iPhone or iPad. 現在…

程序員技術進階手冊(一)

AI正在迅速改變世界&#xff0c;對于程序員來說&#xff0c;這絕對是一個千載難逢的轉型機會。機器學習是計算機科學的一個子領域&#xff0c;在人工智能領域&#xff0c;機器學習逐漸發展成模式識別和計算科學理論的研究。從2016年起&#xff0c;機器學習到達了不合理的火熱巔…

SpringBoot入門 (一) HelloWorld

一 什么是springboot springboot是一個全新的框架&#xff0c;它設計的目的簡化spring項目的初始環境的搭建和開發&#xff0c;主要有以下幾個特點&#xff1a; 1、簡化初始配置 &#xff0c;可與主流框架集成&#xff1b; 2、內置Servlet容器&#xff0c;無需在打War包&#x…

gmail附件調用_如何將Gmail附件保存到Google云端硬盤

gmail附件調用While you can access Gmail attachments by opening the related message deep within Google’s client, it’s not very convenient. You need a central location to access saved documents and images. This guide shows you how to save Gmail attachments…

spring boot攔截器中獲取request post請求中的參數(轉)

文章轉自 https://www.jianshu.com/p/69c6fba08c92 轉載于:https://www.cnblogs.com/shuaiandjun/p/10306242.html

絕地求生大逃殺,改配置

提取效果設置配置文件 通過Procmon工具分析&#xff0c;絕地求生大逃殺效果設置的配置文件為 “C:\Users\Administrator\AppData\Local\TslGame\Saved\Config\WindowsNoEditor\GameUserSettings.ini”&#xff0c;設置好網吧需要的游戲效果后將“TslGame”文件夾提取出來即可&a…

如何使用VLOOKUP在Google表格中查找數據

VLOOKUP is one of the most misunderstood functions in Google Sheets. It allows you to search through and link together two sets of data in your spreadsheet with a single search value. Here’s how to use it. VLOOKUP是Google表格中最容易被誤解的功能之一。 它使…