在使用 WAS(Websphere Application Server)部署应用时,如果想获取 WAS 中配置的数据源,通过 JNDI 来获取数据库连接,需要在部署应用的目录(应用安装目录 => ear 包 => war 包 => WEB-INF 目录)下的 ibm-web-bnd.xml 和 web.xml 文件中配置如下内容:

  • 在 web.xml 中配置资源引用

    <!-- JNDI 资源 -->
    <resource-ref>
    <res-ref-name>jndiRefName</res-ref-name>
    <res-type>javax.sql.Datasource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>
    

    jndiRefName 是自定义的资源引用名称,在代码中使用的就是该名称

  • 在 ibm-web-bnd.xml 中绑定真正的连接池资源

    <?xml version="1.0" encoding="UTF-8"?> 
    <web-bnd     xmlns="http://websphere.ibm.com/xml/ns/javaee"    
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
    xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee    
    http://websphere.ibm.com/xml/ns/javaee/ibm-web-bnd_1_0.xsd"   
     version="1.0">
     <virtual-host name="default_host" /> 
     <!-- 绑定数据源  --> 
     <resource-ref name="jndiRefName" binding-name="JNDIResourceName">
     </resource-ref>
     </web-bnd>
    

    这里的 jndiRefName 需要和 web.xml 的索引名称一致,而 JNDIResourceName 则需要和 WAS 中配置的 JDBC 资源中的 JDNI 名称保持一致。

如此,只要在配置文件中配置 spring.datasource.jndi-name 的值为 jndiRefName 即可,此时,spring boot 中的 JndiDataSourceAutoConfiguration 类便会自动加载,通过 JndiDataSourceLookup 类去获取一个数据源。

WAS 中配置的数据源类型是 com.ibm.ws.rsadapter.jdbc.WSJdbcDataSource,其是 com.ibm.ws.runtime.jar 包中的类,该 jar 包 通常在 WAS 服务器中的 /washome/IBM/WebSphere/AppServer/plugins/ 目录下。

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐