|
19 | 19 | import com.google.cloud.spanner.AbortedException; |
20 | 20 | import com.google.cloud.spanner.Mutation; |
21 | 21 | import com.google.cloud.spanner.ResultSet; |
| 22 | +import com.google.cloud.spanner.TimestampBound; |
| 23 | +import com.google.cloud.spanner.connection.AutocommitDmlMode; |
| 24 | +import com.google.cloud.spanner.connection.TransactionMode; |
22 | 25 | import java.sql.Connection; |
23 | 26 | import java.sql.SQLException; |
24 | 27 | import java.sql.Timestamp; |
|
34 | 37 | */ |
35 | 38 | public interface CloudSpannerJdbcConnection extends Connection { |
36 | 39 |
|
| 40 | + /** |
| 41 | + * Sets the transaction mode to use for current transaction. This method may only be called when |
| 42 | + * in a transaction, and before the transaction is actually started, i.e. before any statements |
| 43 | + * have been executed in the transaction. |
| 44 | + * |
| 45 | + * @param transactionMode The transaction mode to use for the current transaction. |
| 46 | + * <ul> |
| 47 | + * <li>{@link TransactionMode#READ_ONLY_TRANSACTION} will create a read-only transaction and |
| 48 | + * prevent any changes to written to the database through this transaction. The read |
| 49 | + * timestamp to be used will be determined based on the current readOnlyStaleness |
| 50 | + * setting of this connection. It is recommended to use {@link |
| 51 | + * TransactionMode#READ_ONLY_TRANSACTION} instead of {@link |
| 52 | + * TransactionMode#READ_WRITE_TRANSACTION} when possible, as read-only transactions do |
| 53 | + * not acquire locks on Cloud Spanner, and read-only transactions never abort. |
| 54 | + * <li>{@link TransactionMode#READ_WRITE_TRANSACTION} this value is only allowed when the |
| 55 | + * connection is not in read-only mode and will create a read-write transaction. If |
| 56 | + * {@link Connection#isRetryAbortsInternally()} is <code>true</code>, each read/write |
| 57 | + * transaction will keep track of a running SHA256 checksum for each {@link ResultSet} |
| 58 | + * that is returned in order to be able to retry the transaction in case the transaction |
| 59 | + * is aborted by Spanner. |
| 60 | + * </ul> |
| 61 | + */ |
| 62 | + void setTransactionMode(TransactionMode transactionMode) throws SQLException; |
| 63 | + |
| 64 | + /** |
| 65 | + * @return the transaction mode of the current transaction. This method may only be called when |
| 66 | + * the connection is in a transaction. |
| 67 | + */ |
| 68 | + TransactionMode getTransactionMode() throws SQLException; |
| 69 | + |
| 70 | + /** |
| 71 | + * Sets the mode for executing DML statements in autocommit mode for this connection. This setting |
| 72 | + * is only used when the connection is in autocommit mode, and may only be set while the |
| 73 | + * transaction is in autocommit mode and not in a temporary transaction. The autocommit |
| 74 | + * transaction mode is reset to its default value of {@link AutocommitDmlMode#TRANSACTIONAL} when |
| 75 | + * autocommit mode is changed on the connection. |
| 76 | + * |
| 77 | + * @param mode The DML autocommit mode to use |
| 78 | + * <ul> |
| 79 | + * <li>{@link AutocommitDmlMode#TRANSACTIONAL} DML statements are executed as single |
| 80 | + * read-write transaction. After successful execution, the DML statement is guaranteed |
| 81 | + * to have been applied exactly once to the database |
| 82 | + * <li>{@link AutocommitDmlMode#PARTITIONED_NON_ATOMIC} DML statements are executed as |
| 83 | + * partitioned DML transactions. If an error occurs during the execution of the DML |
| 84 | + * statement, it is possible that the statement has been applied to some but not all of |
| 85 | + * the rows specified in the statement. |
| 86 | + * </ul> |
| 87 | + */ |
| 88 | + void setAutocommitDmlMode(AutocommitDmlMode mode) throws SQLException; |
| 89 | + |
| 90 | + /** |
| 91 | + * @return the current {@link AutocommitDmlMode} setting for this connection. This method may only |
| 92 | + * be called on a connection that is in autocommit mode and not while in a temporary |
| 93 | + * transaction. |
| 94 | + */ |
| 95 | + AutocommitDmlMode getAutocommitDmlMode() throws SQLException; |
| 96 | + |
| 97 | + /** |
| 98 | + * Sets the staleness to use for the current read-only transaction. This method may only be called |
| 99 | + * when the transaction mode of the current transaction is {@link |
| 100 | + * TransactionMode#READ_ONLY_TRANSACTION} and there is no transaction that has started, or when |
| 101 | + * the connection is in read-only and autocommit mode. |
| 102 | + * |
| 103 | + * @param staleness The staleness to use for the current but not yet started read-only transaction |
| 104 | + */ |
| 105 | + void setReadOnlyStaleness(TimestampBound staleness) throws SQLException; |
| 106 | + |
| 107 | + /** |
| 108 | + * @return the read-only staleness setting for the current read-only transaction. This method may |
| 109 | + * only be called when the current transaction is a read-only transaction, or when the |
| 110 | + * connection is in read-only and autocommit mode. |
| 111 | + */ |
| 112 | + TimestampBound getReadOnlyStaleness() throws SQLException; |
| 113 | + |
| 114 | + /** |
| 115 | + * Sets the query optimizer version to use for this connection. |
| 116 | + * |
| 117 | + * @param optimizerVersion The query optimizer version to use. Must be a valid optimizer version |
| 118 | + * number, the string <code>LATEST</code> or an empty string. The empty string will instruct |
| 119 | + * the connection to use the optimizer version that is defined in the environment variable |
| 120 | + * <code>SPANNER_OPTIMIZER_VERSION</code>. If no value is specified in the environment |
| 121 | + * variable, the default query optimizer of Cloud Spanner is used. |
| 122 | + */ |
| 123 | + void setOptimizerVersion(String optimizerVersion) throws SQLException; |
| 124 | + |
| 125 | + /** |
| 126 | + * Gets the current query optimizer version of this connection. |
| 127 | + * |
| 128 | + * @return The query optimizer version that is currently used by this connection. |
| 129 | + */ |
| 130 | + String getOptimizerVersion() throws SQLException; |
| 131 | + |
| 132 | + /** |
| 133 | + * @return <code>true</code> if this connection has a transaction (that has not necessarily |
| 134 | + * started). This method will only return false when the {@link Connection} is in autocommit |
| 135 | + * mode and no explicit transaction has been started by calling {@link |
| 136 | + * Connection#beginTransaction()}. If the {@link Connection} is not in autocommit mode, there |
| 137 | + * will always be a transaction. |
| 138 | + */ |
| 139 | + boolean isInTransaction() throws SQLException; |
| 140 | + |
| 141 | + /** |
| 142 | + * @return <code>true</code> if this connection has a transaction that has started. A transaction |
| 143 | + * is automatically started by the first statement that is executed in the transaction. |
| 144 | + */ |
| 145 | + boolean isTransactionStarted() throws SQLException; |
| 146 | + |
37 | 147 | /** |
38 | 148 | * @return the commit {@link Timestamp} of the last read/write transaction. If the last |
39 | 149 | * transaction was not a read/write transaction, or a read/write transaction that did not |
|
0 commit comments