WebSphere Application Server & Liberty

 View Only
  • 1.  @Resource annotation does not work on Liberty

    IBM Champion
    Posted Wed January 25, 2023 08:58 AM
    Hey I'm testing a dataSource configuration on Liberty 22.0.0.12 with a servlet and facing the problem that the "@Resorce" annotation is not finding the dataSoure while looking up the dataSource via the initialContext works.
    ​This is my dataSource definition in liberty (with feature `jakartaee-9.1` loaded:
        <dataSource id="db2DB" jndiName="jdbc/db2DS" jdbcDriverRef="db2XADriver" containerAuthDataRef="db2AuthData" statementCacheSize="10" connectionManagerRef="db2CM" recoveryAuthDataRef="db2AuthData" type="javax.sql.XADataSource" >
            <properties databaseName="HHUE" serverName="localhost" portNumber="60000" driverType="4"  />
        </dataSource>​


    Using the dataSource definition above the following code returns null for `ds1' (but dies not use the authData (which is afaik working as designed)):

    public class HelloServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
        @Resource(lookup = "jdbc/db2DS")
        DataSource ds1;
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            Statement stmt = null;
            Connection con = null;
            try {
                // ds1 = (DataSource) new InitialContext().lookup("jdbc/db2DS");

    However the following code returns a dataSource object for `ds1`:

    public class HelloServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
        // @Resource(lookup = "jdbc/db2DS")
        DataSource ds1;
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            Statement stmt = null;
            Connection con = null;
            try {
                ds1 = (DataSource) new InitialContext().lookup("jdbc/db2DS");


    Any help ob why the "@Resource" annotation does not work abd getting it working is much appreciated. Thanks!



    ------------------------------
    Hermann Huebler
    2innovate IT Consulting GmbH
    Vienna
    Austria

    #IBMChampion
    ------------------------------


  • 2.  RE: @Resource annotation does not work on Liberty

    Posted Wed January 25, 2023 01:34 PM

    I'd recommend checking the following 3 items:

    1. The servlet is importing `jakarta.annotation.Resource` and not `javax.annotation.Resource`.  Jakarta EE 9.1 uses the `jakarta` version of the annotation class.
    2. The WAR module does not contain a web.xml file with `metadata-complete` set to "true".  This disables annotation processing.
    3. The application does not include a copy of the `jakarta.annotation.Resource` annotation.  If the application packages a copy of the annotation class, then it could be loaded with the application classloader and not recognized by the liberty server runtime.


    ------------------------------
    Tracy Burroughs
    ------------------------------



  • 3.  RE: @Resource annotation does not work on Liberty

    IBM Champion
    Posted Thu January 26, 2023 03:59 AM
    Thanks a lot @Tracy Burroughs! Indeed, replacing
    import javax.annotation.Resource;​

    with
    import jakarta.annotation.Resource;​


    did the trick and made the annotation working!



    ------------------------------
    Hermann Huebler
    2innovate IT Consulting GmbH
    Vienna
    Austria

    #IBMChampion
    ------------------------------