-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Apply snapshot isolation to all SQL Server connection when it is enabled #11662
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
Merged
Merged
Changes from all commits
Commits
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
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
116 changes: 116 additions & 0 deletions
116
...n/trino-sqlserver/src/main/java/io/trino/plugin/sqlserver/SqlServerConnectionFactory.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,116 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.trino.plugin.sqlserver; | ||
|
|
||
| import com.google.common.cache.CacheBuilder; | ||
| import io.trino.collect.cache.NonEvictableCache; | ||
| import io.trino.plugin.jdbc.ConnectionFactory; | ||
| import io.trino.spi.TrinoException; | ||
| import io.trino.spi.connector.ConnectorSession; | ||
| import org.jdbi.v3.core.Handle; | ||
| import org.jdbi.v3.core.Jdbi; | ||
|
|
||
| import java.sql.Connection; | ||
| import java.sql.SQLException; | ||
| import java.util.concurrent.ExecutionException; | ||
|
|
||
| import static com.microsoft.sqlserver.jdbc.ISQLServerConnection.TRANSACTION_SNAPSHOT; | ||
| import static io.trino.collect.cache.SafeCaches.buildNonEvictableCache; | ||
| import static io.trino.spi.StandardErrorCode.GENERIC_INTERNAL_ERROR; | ||
| import static java.time.Duration.ofMinutes; | ||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| public class SqlServerConnectionFactory | ||
| implements ConnectionFactory | ||
| { | ||
| private final NonEvictableCache<SnapshotIsolationEnabledCacheKey, Boolean> snapshotIsolationEnabled = buildNonEvictableCache( | ||
| CacheBuilder.newBuilder() | ||
| .maximumSize(1) | ||
| .expireAfterWrite(ofMinutes(5))); | ||
|
|
||
| private final ConnectionFactory delegate; | ||
| private final boolean snapshotIsolationDisabled; | ||
|
|
||
| public SqlServerConnectionFactory(ConnectionFactory delegate, boolean snapshotIsolationDisabled) | ||
| { | ||
| this.delegate = requireNonNull(delegate, "delegate is null"); | ||
| this.snapshotIsolationDisabled = snapshotIsolationDisabled; | ||
| } | ||
|
|
||
| @Override | ||
| public Connection openConnection(ConnectorSession session) | ||
| throws SQLException | ||
| { | ||
| Connection connection = delegate.openConnection(session); | ||
| try { | ||
| prepare(connection); | ||
| } | ||
| catch (SQLException e) { | ||
| try (Connection ignored = connection) { | ||
| throw e; | ||
| } | ||
| } | ||
| return connection; | ||
| } | ||
|
|
||
| private void prepare(Connection connection) | ||
| throws SQLException | ||
| { | ||
| if (snapshotIsolationDisabled) { | ||
| return; | ||
| } | ||
| try { | ||
| if (hasSnapshotIsolationEnabled(connection)) { | ||
| // SQL Server's READ COMMITTED + SNAPSHOT ISOLATION is equivalent to ordinary READ COMMITTED in e.g. Oracle, PostgreSQL. | ||
| connection.setTransactionIsolation(TRANSACTION_SNAPSHOT); | ||
| } | ||
| } | ||
| catch (SQLException e) { | ||
| connection.close(); | ||
| throw e; | ||
| } | ||
| } | ||
|
|
||
| private boolean hasSnapshotIsolationEnabled(Connection connection) | ||
| throws SQLException | ||
| { | ||
| try { | ||
| return snapshotIsolationEnabled.get(SnapshotIsolationEnabledCacheKey.INSTANCE, () -> { | ||
| Handle handle = Jdbi.open(connection); | ||
| return handle.createQuery("SELECT snapshot_isolation_state FROM sys.databases WHERE name = :name") | ||
| .bind("name", connection.getCatalog()) | ||
| .mapTo(Boolean.class) | ||
| .findOne() | ||
| .orElse(false); | ||
| }); | ||
| } | ||
| catch (ExecutionException e) { | ||
| throw new TrinoException(GENERIC_INTERNAL_ERROR, e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void close() | ||
| throws SQLException | ||
| { | ||
| delegate.close(); | ||
| } | ||
|
|
||
| private enum SnapshotIsolationEnabledCacheKey | ||
| { | ||
| // The snapshot isolation can be enabled or disabled on database level. We connect to single | ||
| // database, so from our perspective, this is a global property. | ||
| INSTANCE | ||
| } | ||
| } |
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
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.