(转)WebSphere的web工程中怎么获取数据源

时间:2023-03-08 16:22:08

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

https://blog.****.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.****.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。