Sunday, November 2, 2014

Define and use a datasource under the Websphere Application Server Liberty

Bellow how to define and use a datasource on your workspace.

  1. Create a directory be used as a shared lib for the Oracle JDBC JAR. For example C:\temp_jars and add the ojdbc14.jar and class12.jar to it.
  2. Open server.xml under the Websphere Application Server Liberty Profile and add the bellow two tags:
  3. <dataSource id="oracle" jndiName="jdbc/oracle">
        <jdbcDriver libraryRef="OracleLib"/>
        <properties.oracle URL="jdbc:oracle:thin:@//localhost:1521/test" password="{xor}KzosKw==" user="test"/>
    </dataSource><library description="Oracle JDBC Driver" id="OracleLib" name="Oracle JDBC">
        <fileset dir="C:\temp_jars" includes="ojdbc14.jar class12.jar"/>
    </library>
    
  4. Notice that the password attribute is encoded. You can encode the password through a security utility under the Websphere Application Server Liberty Profile bin directory.

    To use this utility to encode the "test" password, follow the bellow commands.
    cd <WLP_PATH>/wlp/bin
    securityUtility encode test
    
  5. Now inject the datasource at your DAO class as bellow:
  6. import javax.annotation.Resource;
    import javax.ejb.Stateless;
    import javax.sql.DataSource;
    
    @Stateless
    public class TestDAO implements ITestDAO {
    
        @Resource(lookup = "jdbc/oracle")
        private DataSource ds;
    
        // ... 
    }
    


No comments:

Post a Comment