Index: src/main/java/org/apache/jackrabbit/core/SessionImpl.java =================================================================== --- src/main/java/org/apache/jackrabbit/core/SessionImpl.java (revision 1762532) +++ src/main/java/org/apache/jackrabbit/core/SessionImpl.java (working copy) @@ -219,7 +219,7 @@ * The stack trace knows who opened this session. It is logged * if the session is finalized, but Session.logout() was never called. */ - private Exception openStackTrace = new Exception("Stack Trace"); + private final Exception openStackTrace; /** * Protected constructor. @@ -275,6 +275,8 @@ createObservationManager(wspConfig.getName())); versionMgr = createVersionManager(); + // Avoid building the exception if warn level is not enabled as it is costly + this.openStackTrace = log.isWarnEnabled() ? new Exception("Stack Trace") : null; } /** @@ -394,7 +396,7 @@ // TODO: Q: shouldn't 'isSystem' rather be covered by instances of SystemSession only? return (subject != null && !subject.getPrincipals(SystemPrincipal.class).isEmpty()); } - + /** * Returns true if this session has been created for the * administrator. False otherwise. @@ -1363,7 +1365,12 @@ @Override public void finalize() { if (isLive()) { - log.warn("Unclosed session detected. The session was opened here: ", openStackTrace); + if (openStackTrace != null) { + // Log a warning if and only if openStackTrace is not null indicating that + // the warn level is enabled and the session has been fully created to prevent + // https://issues.apache.org/jira/browse/JCR-4033 + log.warn("Unclosed session detected. The session was opened here: ", openStackTrace); + } logout(); } }