fix(rds-data): reject concurrent transaction requests (#5575)

Use a nonblocking per-transaction lock so overlapping Data API calls fail instead of waiting. Skip active transactions during expiry cleanup and refresh their idle timeout after statements finish.
This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-10-13 11:22:27 -04:00
1 parent 890b8eca51
commit 2a04cd78c7
3 files changed
+277 -11

No files matched your search

@@ -40,6 +40,7 @@ import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
@ApplicationScoped
public class RdsDataService implements Resettable {
@@ -121,22 +122,28 @@ public class RdsDataService implements Resettable {
void shutdown() {
transactionCleanupExecutor.shutdownNow();
transactions.forEach((id, tx) -> {
synchronized (tx) {
tx.lock.lock();
try {
if (transactions.remove(id, tx)) {
rollbackQuietly(tx.connection);
tx.close();
}
} finally {
tx.lock.unlock();
}
});
}
public void clear() {
transactions.forEach((id, tx) -> {
synchronized (tx) {
tx.lock.lock();
try {
if (transactions.remove(id, tx)) {
rollbackQuietly(tx.connection);
tx.close();
}
} finally {
tx.lock.unlock();
}
});
transactions.clear();
@@ -211,9 +218,10 @@ public class RdsDataService implements Resettable {
String resourceArn = requiredText(request, "resourceArn");
requiredText(request, "secretArn");
TransactionContext tx = transaction(transactionId);
synchronized (tx) {
validateTransactionResource(tx, resourceArn, region);
acquireTransaction(tx);
try {
requireActiveTransaction(transactionId, tx);
validateTransactionResource(tx, resourceArn, region);
if (!transactions.remove(transactionId, tx)) {
throw transactionNotFound(transactionId);
}
@@ -224,6 +232,8 @@ public class RdsDataService implements Resettable {
} finally {
tx.close();
}
} finally {
tx.lock.unlock();
}
ObjectNode response = objectMapper.createObjectNode();
response.put("transactionStatus", "Transaction Committed");
@@ -235,9 +245,10 @@ public class RdsDataService implements Resettable {
String resourceArn = requiredText(request, "resourceArn");
requiredText(request, "secretArn");
TransactionContext tx = transaction(transactionId);
synchronized (tx) {
validateTransactionResource(tx, resourceArn, region);
acquireTransaction(tx);
try {
requireActiveTransaction(transactionId, tx);
validateTransactionResource(tx, resourceArn, region);
if (!transactions.remove(transactionId, tx)) {
throw transactionNotFound(transactionId);
}
@@ -248,6 +259,8 @@ public class RdsDataService implements Resettable {
} finally {
tx.close();
}
} finally {
tx.lock.unlock();
}
ObjectNode response = objectMapper.createObjectNode();
response.put("transactionStatus", "Rollback Complete");
@@ -262,11 +275,14 @@ public class RdsDataService implements Resettable {
if (transactionId != null && !transactionId.isBlank()) {
TransactionContext tx = transaction(transactionId);
synchronized (tx) {
validateTransactionIdentity(tx, request, region);
acquireTransaction(tx);
try {
requireActiveTransaction(transactionId, tx);
validateTransactionIdentity(tx, request, region);
tx.refresh(transactionTtl);
return work.execute(tx.connection, tx.engine);
} finally {
tx.refresh(transactionTtl);
tx.lock.unlock();
}
}
@@ -654,15 +670,24 @@ public class RdsDataService implements Resettable {
}
}
private void acquireTransaction(TransactionContext tx) {
if (!tx.lock.tryLock()) {
requireActiveTransaction(tx.id, tx);
throw new AwsException("DatabaseErrorException", "Transaction is still running a query", 400);
}
}
private void cleanupExpiredTransactions() {
Instant now = Instant.now();
transactions.forEach((id, tx) -> {
if (tx.expiresAt.isBefore(now)) {
synchronized (tx) {
if (tx.expiresAt.isBefore(now) && tx.lock.tryLock()) {
try {
if (tx.expiresAt.isBefore(now) && transactions.remove(id, tx)) {
rollbackQuietly(tx.connection);
tx.close();
}
} finally {
tx.lock.unlock();
}
}
});
@@ -816,6 +841,7 @@ public class RdsDataService implements Resettable {
private final String database;
private final String region;
private final RdsBackendGate.Lease lease;
private final ReentrantLock lock = new ReentrantLock();
private volatile Instant expiresAt;
private TransactionContext(