Uploaded image for project: 'OpenJPA'
  1. OpenJPA
  2. OPENJPA-2557

FinderCache contains incorrectly cached query with a NULL for a Primary Key.

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Critical
    • Resolution: Fixed
    • Affects Version/s: 2.1.2, 2.2.2, 2.2.1.1, 2.3.0, 2.4.0
    • Fix Version/s: 2.1.2, 2.2.1.1, 2.2.3, 2.4.0
    • Component/s: query
    • Labels:
      None
    • Patch Info:
      Patch Available

      Description

      Take the following SQL from a finder:

      SELECT t1.code, t1.EXTDISCR, t0.field1, t0.field2 FROM TB1 t0 LEFT OUTER JOIN AbstractExtValue t1 ON t0.EXT_USR = t1.code WHERE (t1.EXTDISCR IS NULL OR t1.EXTDISCR IN ) AND t0.field1 = ? AND t0.field2 IS NULL

      Notice the 't0.field2 IS NULL' part. Field2 is part of a compound PK of table 'TB1', where the compound PK consists of two fields (field1 and field2). This will become more apparent when looking at the attached test. Because the PK field 'IS NULL', this finder query should NOT be added to the FinderQuery cache (i.e. we can't cache something which is "hard coded" to 'IS NULL'....in the case where field2 is non-null, the query will never account for that). However, this query is in fact incorrectly added to the cache. To understand why this is incorrectly added we must look at two things. First, if we look at the attached test, we can see that the test uses inheritance and a discriminator value. In the above SQL the 't1.EXTDISCR' is the discriminator value. As you can see we are selecting field1 and field2, where field2 IS NULL. Second, we have to look at the OpenJPA code where we determine if the finder query can be cached. That is, look here in FinderQueryImpl:

      static FinderQueryImpl newFinder(ClassMapping mapping,
      SelectExecutor select) {
      SelectImpl impl = extractImplementation(select);
      if (impl == null)
      return null;
      SQLBuffer buffer = impl.getSQL();
      Column[] pkCols = mapping.getPrimaryKeyColumns();
      boolean canCache = pkCols.length == buffer.getParameters().size();

      As you can see, the last line determines if the query can be cached (i.e. 'canCache'). In this case, OpenJPA compares the pkCols (PK columns) to the parameters. In most cases, these two should be equal for a finder query. However, at least in the case where a discriminator is used, the finder will have one extra more parameter in the finder query. In those cases, the 'canCache' will be false and the query not cached. However, take the case where one of the compound PKs contains 'IS NULL' (see above SQL at the start of this text). In this case, 'pkCols.length' will be two, AND the params will be two. So by the 'canCache' test, it will be true. However, in this case we should not cache the above query (i.e. in so doing field2 will always be "NULL").
      To resolve this issue, I feel that we need to make sure the the columns in the 'buffer' exactly match that of pkCols. See the attached test/patch for proposed fix.

      Thanks,

      Heath Thomann

        Activity

        Hide
        curtisr7 Rick Curtis added a comment -

        +1

        The patch looks good to me.

        Show
        curtisr7 Rick Curtis added a comment - +1 The patch looks good to me.
        Hide
        jira-bot ASF subversion and git services added a comment -

        Commit 1662610 from Heath Thomann in branch 'openjpa/branches/2.1.x'
        [ https://svn.apache.org/r1662610 ]

        OPENJPA-2557: FinderCache contains incorrectly cached query with a NULL for a Primary Key.

        Show
        jira-bot ASF subversion and git services added a comment - Commit 1662610 from Heath Thomann in branch 'openjpa/branches/2.1.x' [ https://svn.apache.org/r1662610 ] OPENJPA-2557 : FinderCache contains incorrectly cached query with a NULL for a Primary Key.
        Hide
        jira-bot ASF subversion and git services added a comment -

        Commit 1662611 from Heath Thomann in branch 'openjpa/branches/2.2.1.x'
        [ https://svn.apache.org/r1662611 ]

        OPENJPA-2557: FinderCache contains incorrectly cached query with a NULL for a Primary Key.

        Show
        jira-bot ASF subversion and git services added a comment - Commit 1662611 from Heath Thomann in branch 'openjpa/branches/2.2.1.x' [ https://svn.apache.org/r1662611 ] OPENJPA-2557 : FinderCache contains incorrectly cached query with a NULL for a Primary Key.
        Hide
        jira-bot ASF subversion and git services added a comment -

        Commit 1662613 from Heath Thomann in branch 'openjpa/branches/2.2.x'
        [ https://svn.apache.org/r1662613 ]

        OPENJPA-2557: FinderCache contains incorrectly cached query with a NULL for a Primary Key.

        Show
        jira-bot ASF subversion and git services added a comment - Commit 1662613 from Heath Thomann in branch 'openjpa/branches/2.2.x' [ https://svn.apache.org/r1662613 ] OPENJPA-2557 : FinderCache contains incorrectly cached query with a NULL for a Primary Key.
        Hide
        jira-bot ASF subversion and git services added a comment -

        Commit 1662615 from Heath Thomann in branch 'openjpa/trunk'
        [ https://svn.apache.org/r1662615 ]

        OPENJPA-2557: FinderCache contains incorrectly cached query with a NULL for a Primary Key.

        Show
        jira-bot ASF subversion and git services added a comment - Commit 1662615 from Heath Thomann in branch 'openjpa/trunk' [ https://svn.apache.org/r1662615 ] OPENJPA-2557 : FinderCache contains incorrectly cached query with a NULL for a Primary Key.

          People

          • Assignee:
            Unassigned
            Reporter:
            jpaheath Heath Thomann
          • Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development