-
Notifications
You must be signed in to change notification settings - Fork 40
Support begin in read-only mode for JDBC transactions #2738
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
brfrn169
merged 3 commits into
support-begin-in-read-only-mode
from
support-begin-in-read-only-mode-for-jdbc-transaction
Jun 9, 2025
Merged
Changes from 1 commit
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
| import com.scalar.db.api.Upsert; | ||
| import com.scalar.db.common.AbstractDistributedTransactionManager; | ||
| import com.scalar.db.common.AbstractTransactionManagerCrudOperableScanner; | ||
| import com.scalar.db.common.ReadOnlyDistributedTransaction; | ||
| import com.scalar.db.common.TableMetadataManager; | ||
| import com.scalar.db.common.checker.OperationChecker; | ||
| import com.scalar.db.common.error.CoreError; | ||
|
|
@@ -36,6 +37,7 @@ | |
| import com.scalar.db.storage.jdbc.RdbEngineFactory; | ||
| import com.scalar.db.storage.jdbc.RdbEngineStrategy; | ||
| import com.scalar.db.util.ThrowableFunction; | ||
| import java.sql.Connection; | ||
| import java.sql.SQLException; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
@@ -90,14 +92,39 @@ public JdbcTransactionManager(DatabaseConfig databaseConfig) { | |
| @Override | ||
| public DistributedTransaction begin() throws TransactionException { | ||
| String txId = UUID.randomUUID().toString(); | ||
| return begin(txId); | ||
| return begin(txId, false); | ||
| } | ||
|
|
||
| @Override | ||
| public DistributedTransaction begin(String txId) throws TransactionException { | ||
| return begin(txId, false); | ||
| } | ||
|
|
||
| @Override | ||
| public DistributedTransaction beginReadOnly() throws TransactionException { | ||
| String txId = UUID.randomUUID().toString(); | ||
| return begin(txId, true); | ||
| } | ||
|
|
||
| @Override | ||
| public DistributedTransaction beginReadOnly(String txId) throws TransactionException { | ||
| return begin(txId, true); | ||
| } | ||
|
|
||
| private DistributedTransaction begin(String txId, boolean readOnly) throws TransactionException { | ||
| try { | ||
| JdbcTransaction transaction = | ||
| new JdbcTransaction(txId, jdbcService, dataSource.getConnection(), rdbEngine); | ||
| Connection connection = dataSource.getConnection(); | ||
|
|
||
| DistributedTransaction transaction; | ||
| if (readOnly) { | ||
| rdbEngine.setReadOnly(connection, true); | ||
| transaction = | ||
| new ReadOnlyDistributedTransaction( | ||
| new JdbcTransaction(txId, jdbcService, connection, rdbEngine)); | ||
|
Comment on lines
+121
to
+123
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wrap the transaction object with |
||
| } else { | ||
| transaction = new JdbcTransaction(txId, jdbcService, connection, rdbEngine); | ||
| } | ||
|
|
||
| getNamespace().ifPresent(transaction::withNamespace); | ||
| getTable().ifPresent(transaction::withTable); | ||
| return transaction; | ||
|
|
@@ -109,16 +136,6 @@ public DistributedTransaction begin(String txId) throws TransactionException { | |
| } | ||
| } | ||
|
|
||
| @Override | ||
| public DistributedTransaction beginReadOnly() { | ||
| throw new UnsupportedOperationException("implement later"); | ||
| } | ||
|
|
||
| @Override | ||
| public DistributedTransaction beginReadOnly(String txId) { | ||
| throw new UnsupportedOperationException("implement later"); | ||
| } | ||
|
|
||
| /** @deprecated As of release 2.4.0. Will be removed in release 4.0.0. */ | ||
| @SuppressWarnings("InlineMeSuggester") | ||
| @Deprecated | ||
|
|
@@ -173,19 +190,19 @@ public DistributedTransaction start( | |
|
|
||
| @Override | ||
| public Optional<Result> get(Get get) throws CrudException, UnknownTransactionStatusException { | ||
| return executeTransaction(t -> t.get(copyAndSetTargetToIfNot(get))); | ||
| return executeTransaction(t -> t.get(copyAndSetTargetToIfNot(get)), true); | ||
| } | ||
|
|
||
| @Override | ||
| public List<Result> scan(Scan scan) throws CrudException, UnknownTransactionStatusException { | ||
| return executeTransaction(t -> t.scan(copyAndSetTargetToIfNot(scan))); | ||
| return executeTransaction(t -> t.scan(copyAndSetTargetToIfNot(scan)), true); | ||
| } | ||
|
|
||
| @Override | ||
| public Scanner getScanner(Scan scan) throws CrudException { | ||
| DistributedTransaction transaction; | ||
| try { | ||
| transaction = begin(); | ||
| transaction = beginReadOnly(); | ||
| } catch (TransactionNotFoundException e) { | ||
| throw new CrudConflictException(e.getMessage(), e, e.getTransactionId().orElse(null)); | ||
| } catch (TransactionException e) { | ||
|
|
@@ -277,7 +294,8 @@ public void put(Put put) throws CrudException, UnknownTransactionStatusException | |
| t -> { | ||
| t.put(copyAndSetTargetToIfNot(put)); | ||
| return null; | ||
| }); | ||
| }, | ||
| false); | ||
| } | ||
|
|
||
| /** @deprecated As of release 3.13.0. Will be removed in release 5.0.0. */ | ||
|
|
@@ -288,7 +306,8 @@ public void put(List<Put> puts) throws CrudException, UnknownTransactionStatusEx | |
| t -> { | ||
| t.put(copyAndSetTargetToIfNot(puts)); | ||
| return null; | ||
| }); | ||
| }, | ||
| false); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -297,7 +316,8 @@ public void insert(Insert insert) throws CrudException, UnknownTransactionStatus | |
| t -> { | ||
| t.insert(copyAndSetTargetToIfNot(insert)); | ||
| return null; | ||
| }); | ||
| }, | ||
| false); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -306,7 +326,8 @@ public void upsert(Upsert upsert) throws CrudException, UnknownTransactionStatus | |
| t -> { | ||
| t.upsert(copyAndSetTargetToIfNot(upsert)); | ||
| return null; | ||
| }); | ||
| }, | ||
| false); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -315,7 +336,8 @@ public void update(Update update) throws CrudException, UnknownTransactionStatus | |
| t -> { | ||
| t.update(copyAndSetTargetToIfNot(update)); | ||
| return null; | ||
| }); | ||
| }, | ||
| false); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -324,7 +346,8 @@ public void delete(Delete delete) throws CrudException, UnknownTransactionStatus | |
| t -> { | ||
| t.delete(copyAndSetTargetToIfNot(delete)); | ||
| return null; | ||
| }); | ||
| }, | ||
| false); | ||
| } | ||
|
|
||
| /** @deprecated As of release 3.13.0. Will be removed in release 5.0.0. */ | ||
|
|
@@ -335,7 +358,8 @@ public void delete(List<Delete> deletes) throws CrudException, UnknownTransactio | |
| t -> { | ||
| t.delete(copyAndSetTargetToIfNot(deletes)); | ||
| return null; | ||
| }); | ||
| }, | ||
| false); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -345,15 +369,21 @@ public void mutate(List<? extends Mutation> mutations) | |
| t -> { | ||
| t.mutate(copyAndSetTargetToIfNot(mutations)); | ||
| return null; | ||
| }); | ||
| }, | ||
| false); | ||
| } | ||
|
|
||
| private <R> R executeTransaction( | ||
| ThrowableFunction<DistributedTransaction, R, TransactionException> throwableFunction) | ||
| ThrowableFunction<DistributedTransaction, R, TransactionException> throwableFunction, | ||
| boolean readOnly) | ||
| throws CrudException, UnknownTransactionStatusException { | ||
| DistributedTransaction transaction; | ||
| try { | ||
| transaction = begin(); | ||
| if (readOnly) { | ||
| transaction = beginReadOnly(); | ||
| } else { | ||
| transaction = begin(); | ||
| } | ||
| } catch (TransactionNotFoundException e) { | ||
| throw new CrudConflictException(e.getMessage(), e, e.getTransactionId().orElse(null)); | ||
| } catch (TransactionException e) { | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Call
Connection.setReadOnly()for read-only transactions.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed the method name
setReadOnly()seems to change therdbEngineto read-only mode.setConnectionToReadOnly()might be less confusing. What do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 60c000f. Thanks!