Skip to content
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

perf: EXPOSED-204 Performance problem with getConnection() #1943

Merged
merged 5 commits into from
Dec 19, 2023

Conversation

bog-walk
Copy link
Member

In spite of reused connections from a connection pool instance, the lazy property initializer of a new database connection attempts to reset the transaction isolation level. This occurs regardless of whether the connection pool's set level has been overriden by a by a new value setting in DatabaseConfig or on the transaction {} itself.

A flag in Database is now set if a DataSource is used to setup connections.
On the first connection, this flag ensures that the level set by the DataSource is retrieved and cached. This avoids redundant resets in future connections unless the DataSource value has been overriden by DatabaseConfig or per-transaction values.

@bog-walk bog-walk requested a review from e5l December 13, 2023 19:57
Copy link
Member

@e5l e5l left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

*
* This should only hold a non-null value if [connectsViaDataSource] has been set to `true`.
*/
internal var dataSourceIsolationLevel: Int? = null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would try to avoid nullable fields because of boxing, but it shouldn't be a problem here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@e5l I didn't consider the implications of Int? and boxing.
Since the goal of this PR is performance, it's an easy change to avoid the null entirely.
Please let me know if this is ok to go.

Override options include:
- Using a cache of the datasource values to compare against per-transaction values
- Getting the actual connection value to compare
In spite of reused connections from a connection pool instance, the lazy
property initializer of a new database connection attempts to reset the
transaction isolation level. This occurs regardless of whether the connection
pool's set level has been overriden by a new value setting in DatabaseConfig
or on the transaction itself.

A flag in Database is now set if a DataSource is used to setup connections.
On the first connection, this flag ensures that the level set by the DataSource
is retrieved and cached. This avoids redundant resets in future connections
unless the DataSource value has been overriden by DatabaseConfig or per-transaction
values.
Rename new properties.

Replace const integer value with a private flag that is passed from
ThreadLocalTransactionManager to ThreadLocalTransaction instance.

Edit tests to also check stored value in TransactionManager, in the event
users access this property directly, before the data source value is retrieved
and cached.
Switch unitialized Database.dataSourceIsolationLevel from nullable to -1.
@bog-walk bog-walk force-pushed the bog-walk/fix-connection-performance branch from 27f3a01 to 1057508 Compare December 14, 2023 19:16
@bog-walk bog-walk merged commit ec7fc56 into main Dec 19, 2023
5 checks passed
@bog-walk bog-walk deleted the bog-walk/fix-connection-performance branch December 19, 2023 13:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants