Uploaded image for project: 'Commons Pool'
  1. Commons Pool
  2. POOL-280

Code cleanups for GenericObjectPool.borrowObject(...)

    XMLWordPrintableJSON

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 2.2
    • Fix Version/s: 2.4.3
    • Labels:
      None

      Description

      In the attached patch you will find 2 minor code cleanups (no functional changes) in order to slightly simplify the code:

      1. removed an if block that was unnecessary from DefaultPooledObject.deallocate()
      2. removed some duplicate code from the two blocks of an if-else statement in GenericObjectPool.borrowObject(...); the original code was:
                while (p == null) {
                    create = false;
                    if (blockWhenExhausted) {
                        p = idleObjects.pollFirst();
                        if (p == null) {
                            p = create();
                            if (p != null) {
                                create = true;
                            }
                        }
                        if (p == null) {
                            if (borrowMaxWaitMillis < 0) {
                                p = idleObjects.takeFirst();
                            } else {
                                p = idleObjects.pollFirst(borrowMaxWaitMillis,
                                        TimeUnit.MILLISECONDS);
                            }
                        }
                        if (p == null) {
                            throw new NoSuchElementException(
                                    "Timeout waiting for idle object");
                        }
                        if (!p.allocate()) {
                            p = null;
                        }
                    } else {
                        p = idleObjects.pollFirst();
                        if (p == null) {
                            p = create();
                            if (p != null) {
                                create = true;
                            }
                        }
                        if (p == null) {
                            throw new NoSuchElementException("Pool exhausted");
                        }
                        if (!p.allocate()) {
                            p = null;
                        }
                    }
                    ...
                }
        

        and the new code is:

                while (p == null) {
                    create = false;
                    p = idleObjects.pollFirst();
                    if (p == null) {
                        p = create();
                        if (p != null) {
                            create = true;
                        }
                    }
                    if (p == null) {
                        if (blockWhenExhausted) {
                            if (borrowMaxWaitMillis < 0) {
                                p = idleObjects.takeFirst();
                            } else {
                                p = idleObjects.pollFirst(borrowMaxWaitMillis,
                                        TimeUnit.MILLISECONDS);
                            }
                            if (p == null) {
                                throw new NoSuchElementException(
                                        "Timeout waiting for idle object");
                            }
                        } else {
                            throw new NoSuchElementException("Pool exhausted");
                        }
                    }
                    if (!p.allocate()) {
                        p = null;
                    }
                    ...
                }
        

        Attachments

        1. POOL-280.patch
          2 kB
          Jacopo Cappellato

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              jacopoc Jacopo Cappellato
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: