use super::*;
impl ArcadeDbClient {
pub async fn begin_transaction(
&self,
database: &str,
isolation: TransactionIsolation,
) -> Result<String> {
let req = tonic::Request::new(BeginTransactionRequest {
database: database.into(),
credentials: Some(self.auth.creds.clone()),
isolation: isolation as i32,
});
let resp = self
.data
.clone()
.begin_transaction(self.add_auth_headers(req))
.await
.map_err(|e| map_rpc_error("begin transaction", "", e))?
.into_inner();
tracing::info!(tx = %resp.transaction_id, "transaction begun");
Ok(resp.transaction_id)
}
pub async fn execute_in_transaction(
&self,
database: &str,
language: &str,
command: &str,
transaction_id: &str,
) -> Result<ExecuteCommandResponse> {
let req = tonic::Request::new(ExecuteCommandRequest {
database: database.into(),
command: command.into(),
credentials: Some(self.auth.creds.clone()),
language: language.into(),
return_rows: true,
transaction: Some(TransactionContext {
transaction_id: transaction_id.into(),
..Default::default()
}),
..Default::default()
});
let resp = self
.data
.clone()
.execute_command(self.add_auth_headers(req))
.await
.map_err(|e| map_rpc_error("execute command (in tx)", command, e))?
.into_inner();
if !resp.success {
return Err(command_failed(
"execute command (in tx)",
resp.message,
resp.affected_records,
));
}
Ok(resp)
}
pub async fn query_in_transaction(
&self,
database: &str,
query: &str,
transaction_id: &str,
) -> Result<QueryResult> {
let req = tonic::Request::new(ExecuteQueryRequest {
database: database.into(),
query: query.into(),
credentials: Some(self.auth.creds.clone()),
language: "sql".into(),
transaction: Some(TransactionContext {
transaction_id: transaction_id.into(),
..Default::default()
}),
..Default::default()
});
let resp = self
.data
.clone()
.execute_query(self.add_auth_headers(req))
.await
.map_err(|e| map_rpc_error("query (in tx)", query, e))?
.into_inner();
let mut records = Vec::with_capacity(resp.results.iter().map(|qr| qr.records.len()).sum());
records.extend(resp.results.into_iter().flat_map(|qr| qr.records));
Ok(QueryResult {
records,
execution_time_ms: resp.execution_time_ms,
})
}
pub async fn commit_transaction(&self, database: &str, transaction_id: &str) -> Result<()> {
let req = tonic::Request::new(CommitTransactionRequest {
transaction: Some(TransactionContext {
transaction_id: transaction_id.into(),
database: database.into(),
..Default::default()
}),
credentials: Some(self.auth.creds.clone()),
});
let resp = self
.data
.clone()
.commit_transaction(self.add_auth_headers(req))
.await
.map_err(|e| map_rpc_error("commit transaction", transaction_id, e))?
.into_inner();
tracing::info!(tx = %transaction_id, ok = resp.success, committed = resp.committed, "transaction committed");
if !resp.success {
return Err(ArcadeDbError::CommandFailed {
operation: "commit transaction".into(),
message: resp.message,
affected: None,
});
}
Ok(())
}
pub async fn rollback_transaction(&self, database: &str, transaction_id: &str) -> Result<()> {
let req = tonic::Request::new(RollbackTransactionRequest {
transaction: Some(TransactionContext {
transaction_id: transaction_id.into(),
database: database.into(),
..Default::default()
}),
credentials: Some(self.auth.creds.clone()),
});
let resp = self
.data
.clone()
.rollback_transaction(self.add_auth_headers(req))
.await
.map_err(|e| map_rpc_error("rollback transaction", transaction_id, e))?
.into_inner();
tracing::info!(tx = %transaction_id, ok = resp.success, rolled_back = resp.rolled_back, "transaction rolled back");
if !resp.success {
return Err(ArcadeDbError::CommandFailed {
operation: "rollback transaction".into(),
message: resp.message,
affected: None,
});
}
Ok(())
}
pub async fn transaction(
&self,
database: &str,
isolation: TransactionIsolation,
) -> Result<Transaction<'_>> {
let id = self.begin_transaction(database, isolation).await?;
Ok(Transaction {
client: self,
database: database.into(),
id,
finished: false,
})
}
pub async fn run_transaction<T, F, Fut>(
&self,
database: &str,
isolation: TransactionIsolation,
retries: u32,
body: F,
) -> Result<T>
where
F: Fn(TxCommands) -> Fut,
Fut: std::future::Future<Output = Result<T>>,
{
let attempts = retries.saturating_add(1);
let mut last_err: Option<ArcadeDbError> = None;
for attempt in 0..attempts {
let tx_id = self.begin_transaction(database, isolation).await?;
let commands = TxCommands::new(self, database, &tx_id);
match body(commands).await {
Ok(value) => match self.commit_transaction(database, &tx_id).await {
Ok(()) => return Ok(value),
Err(e) => {
let _ = self.rollback_transaction(database, &tx_id).await;
if is_retryable_conflict(&e) && attempt + 1 < attempts {
tracing::warn!(attempt, "transaction commit conflict — retrying");
last_err = Some(e);
continue;
}
return Err(e);
}
},
Err(e) => {
let _ = self.rollback_transaction(database, &tx_id).await;
if is_retryable_conflict(&e) && attempt + 1 < attempts {
tracing::warn!(attempt, "transaction body conflict — retrying");
last_err = Some(e);
continue;
}
return Err(e);
}
}
}
Err(last_err.unwrap_or_else(|| ArcadeDbError::InvalidInput {
message: "run_transaction: retries exhausted without an error".into(),
}))
}
}
pub struct TxCommands {
client: ArcadeDbClient,
database: String,
tx_id: String,
}
impl TxCommands {
pub(crate) fn new(client: &ArcadeDbClient, database: &str, tx_id: &str) -> Self {
Self {
client: client.clone(),
database: database.to_string(),
tx_id: tx_id.to_string(),
}
}
pub async fn execute(&self, language: &str, command: &str) -> Result<ExecuteCommandResponse> {
self.client
.execute_in_transaction(&self.database, language, command, &self.tx_id)
.await
}
pub async fn execute_with_values(
&self,
command: &str,
parameters: impl Into<crate::encode::Params>,
) -> Result<ExecuteCommandResponse> {
let parameters = parameters.into();
let req = tonic::Request::new(ExecuteCommandRequest {
database: self.database.clone(),
command: command.into(),
parameters: parameters.0,
credentials: Some(self.client.creds()),
language: "sql".into(),
return_rows: true,
transaction: Some(TransactionContext {
transaction_id: self.tx_id.clone(),
..Default::default()
}),
..Default::default()
});
let resp = self
.client
.data
.clone()
.execute_command(self.client.add_auth_headers(req))
.await
.map_err(|e| map_rpc_error("execute command (in tx)", command, e))?
.into_inner();
if !resp.success {
return Err(command_failed(
"execute command (in tx)",
resp.message,
resp.affected_records,
));
}
Ok(resp)
}
pub async fn query(&self, query: &str) -> Result<QueryResult> {
self.client
.query_in_transaction(&self.database, query, &self.tx_id)
.await
}
}
pub(crate) fn is_retryable_conflict(err: &ArcadeDbError) -> bool {
err.server_message()
.map(|m| {
let m = m.to_ascii_lowercase();
m.contains("concurrent") || m.contains("mvcc") || m.contains("needretry")
})
.unwrap_or(false)
}
pub struct Transaction<'a> {
client: &'a ArcadeDbClient,
database: String,
id: String,
finished: bool,
}
impl Transaction<'_> {
pub fn id(&self) -> &str {
&self.id
}
pub async fn execute(&self, language: &str, command: &str) -> Result<ExecuteCommandResponse> {
self.client
.execute_in_transaction(&self.database, language, command, &self.id)
.await
}
pub async fn query(&self, query: &str) -> Result<QueryResult> {
self.client
.query_in_transaction(&self.database, query, &self.id)
.await
}
pub async fn commit(mut self) -> Result<()> {
self.finished = true;
self.client
.commit_transaction(&self.database, &self.id)
.await
}
pub async fn rollback(mut self) -> Result<()> {
self.finished = true;
self.client
.rollback_transaction(&self.database, &self.id)
.await
}
}
impl Drop for Transaction<'_> {
fn drop(&mut self) {
if self.finished {
return;
}
tracing::warn!(tx = %self.id, db = %self.database, "transaction dropped unfinished — rolling back");
let client = self.client.clone();
let database = std::mem::take(&mut self.database);
let id = std::mem::take(&mut self.id);
let _ = tokio::runtime::Handle::try_current().map(|rt| {
rt.spawn(async move {
if let Err(e) = client.rollback_transaction(&database, &id).await {
tracing::error!(tx = %id, "drop rollback failed: {e}");
}
});
});
}
}