@@ -35,6 +35,7 @@ pub struct MssqlConnection {
3535 stream: Option<MssqlWireStream>,
3636 transaction_depth: usize,
3737 transaction_descriptor: u64,
38+ pending_rollback_sql: Option<&'static str>,
3839}
3940
4041impl MssqlConnection {
@@ -78,6 +79,7 @@ impl MssqlConnection {
7879 stream: Some(stream),
7980 transaction_depth: 0,
8081 transaction_descriptor: 0,
82+ pending_rollback_sql: None,
8183 };
8284 conn.apply_env_changes(&env_changes);
8385 Ok(conn)
@@ -123,6 +125,11 @@ impl MssqlConnection {
123125 }
124126
125127 pub(crate) async fn run_sql_batch(&mut self, sql: &str) -> Result<QueryOutput, Error> {
128+ self.flush_pending_rollback().await?;
129+ self.run_sql_batch_direct(sql).await
130+ }
131+
132+ async fn run_sql_batch_direct(&mut self, sql: &str) -> Result<QueryOutput, Error> {
126133 let transaction_descriptor = self.transaction_descriptor;
127134 let stream = self.stream.as_mut().ok_or_else(wire_not_implemented)?;
128135 let packet = build_sql_batch_packet(sql, stream.packet_size, transaction_descriptor)
@@ -132,11 +139,38 @@ impl MssqlConnection {
132139 self.read_query_response().await
133140 }
134141
142+ pub(crate) fn queue_rollback(&mut self) {
143+ let sql = match self.transaction_depth {
144+ 0 => return,
145+ 1 => {
146+ self.transaction_depth = 0;
147+ "ROLLBACK TRANSACTION"
148+ }
149+ _ => {
150+ self.transaction_depth -= 1;
151+ "ROLLBACK TRANSACTION sqlx_savepoint"
152+ }
153+ };
154+
155+ self.pending_rollback_sql = Some(sql);
156+ }
157+
158+ async fn flush_pending_rollback(&mut self) -> Result<(), Error> {
159+ let Some(sql) = self.pending_rollback_sql.take() else {
160+ return Ok(());
161+ };
162+
163+ self.run_sql_batch_direct(sql).await?;
164+ Ok(())
165+ }
166+
135167 pub(crate) async fn run_execute_sql(
136168 &mut self,
137169 sql: &str,
138170 arguments: Option<&MssqlArguments>,
139171 ) -> Result<QueryOutput, Error> {
172+ self.flush_pending_rollback().await?;
173+
140174 match arguments {
141175 Some(arguments) if !arguments.is_empty() => {
142176 let transaction_descriptor = self.transaction_descriptor;
@@ -153,7 +187,7 @@ impl MssqlConnection {
153187 stream.write_all(&packet).await?;
154188 self.read_query_response().await
155189 }
156- _ => self.run_sql_batch (sql).await,
190+ _ => self.run_sql_batch_direct (sql).await,
157191 }
158192 }
159193
@@ -162,6 +196,8 @@ impl MssqlConnection {
162196 sql: &str,
163197 parameters: &[MssqlTypeInfo],
164198 ) -> Result<QueryOutput, Error> {
199+ self.flush_pending_rollback().await?;
200+
165201 let transaction_descriptor = self.transaction_descriptor;
166202 let stream = self.stream.as_mut().ok_or_else(wire_not_implemented)?;
167203 let packet =
@@ -211,6 +247,8 @@ impl Connection for MssqlConnection {
211247 type Options = MssqlConnectOptions;
212248
213249 async fn close(mut self) -> Result<(), Error> {
250+ self.flush_pending_rollback().await?;
251+
214252 if let Some(mut stream) = self.stream.take() {
215253 stream.shutdown().await?;
216254 }
@@ -227,6 +265,8 @@ impl Connection for MssqlConnection {
227265 }
228266
229267 async fn ping(&mut self) -> Result<(), Error> {
268+ self.flush_pending_rollback().await?;
269+
230270 if self.stream.is_some() {
231271 Ok(())
232272 } else {
0 commit comments