-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract transaction manager from JdbcConnector
Preparatory commit for supporting table functions in JDBC connectors
- Loading branch information
Showing
3 changed files
with
75 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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 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
66 changes: 66 additions & 0 deletions
66
plugin/trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/JdbcTransactionManager.java
This file contains 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,66 @@ | ||
/* | ||
* 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.jdbc; | ||
|
||
import io.trino.spi.connector.ConnectorTransactionHandle; | ||
import io.trino.spi.transaction.IsolationLevel; | ||
|
||
import javax.inject.Inject; | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
import static io.trino.spi.transaction.IsolationLevel.READ_COMMITTED; | ||
import static io.trino.spi.transaction.IsolationLevel.checkConnectorSupports; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
public class JdbcTransactionManager | ||
{ | ||
private final ConcurrentMap<ConnectorTransactionHandle, JdbcMetadata> transactions = new ConcurrentHashMap<>(); | ||
private final JdbcMetadataFactory metadataFactory; | ||
|
||
@Inject | ||
public JdbcTransactionManager(JdbcMetadataFactory metadataFactory) | ||
{ | ||
this.metadataFactory = requireNonNull(metadataFactory, "metadataFactory is null"); | ||
} | ||
|
||
public ConnectorTransactionHandle beginTransaction(IsolationLevel isolationLevel, boolean readOnly, boolean autoCommit) | ||
{ | ||
checkConnectorSupports(READ_COMMITTED, isolationLevel); | ||
JdbcTransactionHandle transaction = new JdbcTransactionHandle(); | ||
transactions.put(transaction, metadataFactory.create(transaction)); | ||
return transaction; | ||
} | ||
|
||
public JdbcMetadata getMetadata(ConnectorTransactionHandle transaction) | ||
{ | ||
JdbcMetadata metadata = transactions.get(transaction); | ||
checkArgument(metadata != null, "no such transaction: %s", transaction); | ||
return metadata; | ||
} | ||
|
||
public void commit(ConnectorTransactionHandle transaction) | ||
{ | ||
checkArgument(transactions.remove(transaction) != null, "no such transaction: %s", transaction); | ||
} | ||
|
||
public void rollback(ConnectorTransactionHandle transaction) | ||
{ | ||
JdbcMetadata metadata = transactions.remove(transaction); | ||
checkArgument(metadata != null, "no such transaction: %s", transaction); | ||
metadata.rollback(); | ||
} | ||
} |