# TransactionRollback

Rollback the current transaction and discard all unpersisted queries.

Only the changes made since the last `transactionSetSavepoint()` or `transactionCommit()` call will be discarded.

If no transaction is found in the current context, this method will throw an exception.

## Method Signature

```
TransactionRollback(savepoint=[string])
```

### Arguments

| Argument    | Type     | Required | Description                                                                                               | Default |
| ----------- | -------- | -------- | --------------------------------------------------------------------------------------------------------- | ------- |
| `savepoint` | `string` | `false`  | String name of the savepoint to rollback to. If not provided, the entire transaction will be rolled back. |         |

## Examples

### Using TransactionRollback with TransactionCommit

This simple example shows two JDBC queries executed inside a transaction. The first query, an INSERT, is committed to the database while the second is rolled back.

```java
transaction {
    queryExecute( "INSERT INTO vehicles (id,make) VALUES (8, 'Ford' )", {}, { datasource : "carDB" } );
    transactionCommit();

    queryExecute( "UPDATE developers SET name = 'Chevrolet' WHERE id=8", {}, { datasource : "carDB" } );
    transactionRollback();
}
```

Note that the rollback cannot affect the INSERT statement, since it has already been committed ("persisted") to the database.

#### Using TransactionRollback with TransactionSetSavepoint

Many times, you may wish to roll back only a portion of a transaction. This is possible by setting a "savepoint" upon the transaction:

```java
transaction {
    queryExecute( "INSERT INTO vehicles (id,make) VALUES (8, 'Ford' )", {}, { datasource : "carDB" } );
    transactionSetSavepoint( 'insert' );

    queryExecute( "UPDATE developers SET name = 'Chevrolet' WHERE id=8", {}, { datasource : "carDB" } );
    transactionRollback( 'insert' );
}
```

Multiple savepoints can be set or referenced.

### Script Syntax

In this code, any error triggered will cause transactionRollback() to run and roll back the pending transaction

[Run Example](https://try.boxlang.io/?code=eJxLqrAqKUrMK05MLsnMz1Oo5uIsKaoEUZz6%2BgrJ%2BSmpCiX5CkWleUABJHXO%2Bbm5mSUamtZcnLUKyYklyRkKGol5lQqpmmCtSCqD8nNykhKTsyFquWq5AC39I%2Bw%3D)

```java
bx:transaction {
	try {
		// code to run
		transactionCommit();
	} catch (any e) {
		transactionRollback();
	}
}

```

## Related

* [IsInTransaction](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/jdbc/isintransaction)
* [IsWithinTransaction](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/jdbc/iswithintransaction)
* [PreserveSingleQuotes](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/jdbc/preservesinglequotes)
* [QueryExecute](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/jdbc/queryexecute)
* [TransactionCommit](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/jdbc/transactioncommit)
* [TransactionSetSavepoint](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/jdbc/transactionsetsavepoint)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/jdbc/transactionrollback.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
