在websphere上一个J2EE应用中引用多个数据源JNDI的方法

来源:互联网 发布:versions for mac闪退 编辑:程序博客网 时间:2024/06/15 20:57

 

1.WEB-INF下新建文件ibm-web-bnd.xmi中建立两个绑定JNDI

<?xml version="1.0" encoding="UTF-8"?>

<com.ibm.ejs.models.base.bindings.webappbnd:WebAppBindingxmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI"xmlns:com.ibm.ejs.models.base.bindings.webappbnd="webappbnd.xmi"xmi:id="WebAppBinding_1216699055547"virtualHostName="default_host">
  <webapp href="WEB-INF/web.xml#WebApp_1216699055562"/>
  <resRefBindings xmi:id="ResourceRefBinding_1"jndiName="jdbc/ias">
    <bindingResourceRef href="WEB-INF/web.xml#ResourceRef_1"/>
  </resRefBindings>
  <resRefBindings xmi:id="ResourceRefBinding_1216699055547"jndiName="jdbc/host">
    <bindingResourceRefhref="WEB-INF/web.xml#ResourceRef_1216699055547"/>
  </resRefBindings>
</com.ibm.ejs.models.base.bindings.webappbnd:WebAppBinding>

2.WEB.xml文件中两个引用指向(id分别跟上面的web.xml#后面的内容相同)
<resource-ref id="ResourceRef_1">
    <description></description>
    <res-ref-name>ias</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
   </resource-ref>
  

   <resource-ref id="ResourceRef_1216699055547">
    <description></description>
    <res-ref-name>host</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
   </resource-ref>

3.在代码中可以引用两个JNDI(不同的数据源)
 Context ctx = new InitialContext();
    Object obj = ctx.lookup("java:comp/env/host");
      ds = (DataSource) obj;
      conn = ds.getConnection();
      stmt = conn.createStatement();
      rs = stmt.executeQuery("select * from c_jgbwhere rownum<10");

 Contextctx = new InitialContext();
    Object obj = ctx.lookup("java:comp/env/ias");
      ds = (DataSource) obj;
      conn = ds.getConnection();
      stmt = conn.createStatement();
      rs = stmt.executeQuery("select * fromTBUSERSUBPOS where rownum<10");

 

原创粉丝点击