-
Notifications
You must be signed in to change notification settings - Fork 21
Moving LRTS handling into a Connection Preparer #1596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rma-rripken
wants to merge
8
commits into
develop
Choose a base branch
from
bugfix/lrts_left_on
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
18bb109
Trying to resolve failing test
rma-rripken 2ea9e48
Changing how the lrts flag is handled.
rma-rripken 747ed9d
Merge branch 'develop' into bugfix/lrts_left_on
rma-rripken 085b32d
Removed two very old controller tests that mocked requests and databa…
rma-rripken b0d7028
Making LRTS behavior binary.
rma-rripken f3ba856
Tired of seeing these info messages in the IT log. They'd get printe…
rma-rripken 3ea7213
Merge branch 'develop' into bugfix/lrts_left_on
rma-rripken 8608dd5
Merge branch 'develop' into bugfix/lrts_left_on
rma-rripken File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
cwms-data-api/src/main/java/cwms/cda/datasource/LrtsSessionPreparer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| package cwms.cda.datasource; | ||
|
|
||
| import static cwms.cda.data.dao.Dao.formatBool; | ||
| import static org.jooq.SQLDialect.ORACLE18C; | ||
|
|
||
| import com.google.common.flogger.FluentLogger; | ||
| import cwms.cda.data.dao.JooqDao; | ||
| import java.lang.reflect.InvocationHandler; | ||
| import java.lang.reflect.Method; | ||
| import java.lang.reflect.Proxy; | ||
| import java.sql.Connection; | ||
| import org.jooq.DSLContext; | ||
| import org.jooq.impl.DSL; | ||
| import usace.cwms.db.jooq.codegen.packages.CWMS_UTIL_PACKAGE; | ||
|
|
||
|
|
||
| /** | ||
| * Prepares a connection by setting the LRTS session flag. | ||
| */ | ||
| public class LrtsSessionPreparer implements ConnectionPreparer { | ||
|
|
||
| private static final FluentLogger logger = FluentLogger.forEnclosingClass(); | ||
|
|
||
| private final Boolean isNewLrts; | ||
| private final boolean clearOnClose; | ||
|
|
||
| public LrtsSessionPreparer(Boolean isNewLrts) { | ||
| this(isNewLrts, false); | ||
| } | ||
|
|
||
| public LrtsSessionPreparer(Boolean isNewLrts, boolean clear) { | ||
| this.isNewLrts = isNewLrts; | ||
| clearOnClose = clear; | ||
| } | ||
|
|
||
| @Override | ||
| public Connection prepare(Connection connection) { | ||
| if (isNewLrts == null) { | ||
| return connection; | ||
| } | ||
|
|
||
| DSLContext dsl = DSL.using(connection, ORACLE18C); | ||
|
|
||
| // Can also get current value and remember it and then reset to that in the close | ||
| // if setting with null,null doesn't work. | ||
| // GET_SESSION_INFO sessionInfo = CWMS_UTIL_PACKAGE.call_GET_SESSION_INFO(dslContext.configuration() | ||
| // , SESSION_USE_LRTS_ID_FORMAT); | ||
|
|
||
| String requireBool = formatBool(isNewLrts); | ||
| int requireIntValue = isNewLrts ? JooqDao.REQUIRE_NEW_LRTS_ID_FORMAT | ||
| : JooqDao.REQUIRE_OLD_LRTS_ID_FORMAT; | ||
| CWMS_UTIL_PACKAGE.call_SET_SESSION_INFO(dsl.configuration(), | ||
| JooqDao.SESSION_USE_LRTS_ID_FORMAT, requireBool, requireIntValue); | ||
| logger.atFine().log("Set LRTS session flag to %s (%d) for connection %s", | ||
| requireBool, requireIntValue, connection); | ||
|
|
||
| if (clearOnClose) { | ||
| // Return a proxy that will unset the flag on close() | ||
| return (Connection) Proxy.newProxyInstance( | ||
| connection.getClass().getClassLoader(), | ||
| new Class<?>[]{Connection.class}, | ||
| new CloseUnsettingHandler(connection)); | ||
| } else { | ||
| return connection; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| private static class CloseUnsettingHandler implements InvocationHandler { | ||
| private final Connection delegate; | ||
| private volatile boolean closed = false; | ||
|
|
||
| CloseUnsettingHandler(Connection delegate) { | ||
| this.delegate = delegate; | ||
| } | ||
|
|
||
| @Override | ||
| public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { | ||
| String name = method.getName(); | ||
| if ("close".equals(name)) { | ||
| if (!closed) { | ||
| try { | ||
| DSLContext dsl = DSL.using(delegate, ORACLE18C); | ||
| // Passing null values to clear the session setting | ||
| CWMS_UTIL_PACKAGE.call_SET_SESSION_INFO(dsl.configuration(), | ||
| JooqDao.SESSION_USE_LRTS_ID_FORMAT, null, null); | ||
| logger.atFine().log("Cleared LRTS session flag for connection %s", delegate); | ||
| } catch (RuntimeException ex) { | ||
| logger.atWarning().withCause(ex) | ||
| .log("Failed to clear LRTS session flag on connection close"); | ||
| } finally { | ||
| closed = true; | ||
| } | ||
| } | ||
| return method.invoke(delegate, args); | ||
| } | ||
| return method.invoke(delegate, args); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.