> For the complete documentation index, see [llms.txt](https://boxlang.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://boxlang.ortusbooks.com/boxlang-language/reference/components/jdbc/transaction.md).

# Transaction

Demarcate or manage a JDBC transaction.

## Component Signature

```
<bx:Transaction action=[string]
isolation=[string]
savepoint=[string]
datasource=[string] />
```

### Attributes

| Atrribute    | Type     | Required | Description                                                                                                                                                                 | Default |
| ------------ | -------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------- |
| `action`     | `string` | `false`  | When used inside a transaction block, perform some action upon an existing transaction. One of: `begin`, `commit`, `rollback`, or `setsavepoint`.                           | `begin` |
| `isolation`  | `string` | `false`  | The isolation level to use for the transaction. Can only be set upon transaction begin. One of: `read_uncommitted`, `read_committed`, `repeatable_read`, or `serializable`. |         |
| `savepoint`  | `string` | `false`  | The name of the savepoint to set or rollback to. Used with `savepoint` or `rollback` actions.                                                                               |         |
| `datasource` | `string` | `false`  | The name of the datasource to use for the transaction. If not provided, the first query execution inside the transaction will set the datasource.                           |         |

## Examples

### Simple Transaction

A "simple" transaction might look like this:

```js
transaction{
    queryExecute( "UPDATE users SET name=:name WHERE id=:id", { id: variables.id, name: variables.name } );
    queryExecute(
        "INSERT INTO userLog( action, id, changes ) VALUES ( 'UPDATE', :id, :change)",
        { id: variables.id, change: "changed name to #variables.name#" }
    );
}
```

Note that you can specify a custom isolation level using the `isolation` attribute on the component:

```js
transaction isolation="serializable"{
    // ...
}
```

### Nested Transaction

BoxLang supports nested transactions, using savepoints to control transaction state between the parent and child transactions.

```js
transaction{
    queryExecute( "UPDATE users SET name=:name WHERE id=:id", { id: variables.id, name: variables.name } );

    transaction{
        try{
            queryExecute(
                "INSERT INTO userLog( action, id, changes ) VALUES ( 'UPDATE', :id, :change)",
                { id: variables.id, change: "changed name to #variables.name#" }
            );
        } catch( any e ){
            // user log errored; rollback!
            transactionRollback();
            // log error here
        }
    }
}
```

Here, we have a nested transaction that may (or may not) error; if this happens we want to roll back the logging query but NOT roll back the user query. This functions the same as if we were rolling back to a named savepoint created just after the `UPDATE users` query.

### Script Syntax

[Run Example](https://try.boxlang.io/?code=eJxLqrAqKUrMK05MLsnMz1Oo5uIsKaoEUZz6%2BgrJ%2BSmpCiX5CkWleUCBJFSlEMpWKTk%2FNzezRMmai7NWITmxJDlDQSMxr1IhVRNsCg5NRfk5OUmJydlgbVy1XADL1Cvw)

```java
bx:transaction {
	try {
		// code to run
		bx:transaction action="commit";
	} catch (any e) {
		bx:transaction action="rollback";
	}
}

```

### Tag Syntax

```java
<bx:transaction> 
 <bx:try> 
 <!--- code to run ---> 
   <bx:transaction action="commit"/> 
  
 <bx:catch type="any"> 
 <bx:transaction action="rollback"/> 
 </bx:catch></bx:try> 
 </bx:transaction>
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://boxlang.ortusbooks.com/boxlang-language/reference/components/jdbc/transaction.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
