How do I connect to a Websphere Datasource with a given JNDI name?

前端 未结 6 1483
温柔的废话
温柔的废话 2020-12-07 23:15

I\'m using Websphere Portal 7.0 and creating a portlet with RAD 8.0. My portlet is trying to make a db2 connection to a remote server. I wrote a java program locally to do a

6条回答
  •  抹茶落季
    2020-12-07 23:47

    You need to define a resource reference in your application and then map that logical resource reference to the physical resource (data source) during deployment.

    In your web.xml, add the following configuration (modifying the names and properties as appropriate):

    
        Resource reference to my database
        jdbc/MyDB
        javax.sql.DataSource
        Container
        Shareable
    
    

    Then, during application deployment, WAS will prompt you to map this resource reference (jdbc/MyDB) to the data source you created in WAS.

    In your code, you can obtain the DataSource similar to how you've shown it in your example; however, the JNDI name you'll use to look it up should actually be the resource reference's name you defined (res-ref-name), rather than the JNDI name of the physical data source. Also, you'll need to prefix the res-ref-name with the application naming context (java:comp/env/).

    Context ctx = new InitialContext();
    DataSource dataSource = (DataSource) ctx.lookup("java:comp/env/jdbc/MyDB");
    

提交回复
热议问题