# Transaction

## Definition

After creating the database connection, we use the database connection to create a transaction manager.

Transaction management in GoooQo is accomplished through the cooperation of TransactionManager and TransactionContext (TC for short).

```go
package core

import (
	"context"
	"database/sql/driver"
)

type TransactionManager interface {
	GetClient() any
	StartTransaction(ctx context.Context) (TransactionContext, error)
	SubmitTransaction(ctx context.Context, callback func(tc TransactionContext) error) error
}

type TransactionContext interface {
	context.Context
	driver.Tx
	Parent() context.Context
	SavePoint(name string) error
	RollbackTo(name string) error
}
```

The method `TransactionManager#StartTransaction` is responsible for starting a transaction and returning TC; TC combines `driver.Tx` and is responsible for transaction commit and rollback.

The `TxDataAccess` interface combines `TransactionManager` and `DataAccess`, which can conveniently manage transactions while providing database operations.

```go
type TxDataAccess[E Entity] struct {
	TransactionManager
	DataAccess[E]
}
```

## Usages

Use the database connection `db` to create a TransactionManager `tm`:

<pre class="language-go"><code class="lang-go"><strong>db := rdb.Connect("app.properties")
</strong>defer rdb.Disconnect(db)
tm := rdb.NewTransactionManager(db)
</code></pre>

Use `StartTransaction` to start a transaction and manually commit or rollback the transaction:

```go
tc, err := tm.StartTransaction(ctx)
userQuery := UserQuery{ScoreLt: P(80)}
cnt, err := userDataAccess.DeleteByQuery(tc, userQuery)
if err != nil {
	err = RollbackFor(tc, err)
	return 0
}
err = tc.Commit()
return cnt
```

Or use `SubmitTransaction` to submit the transaction via callback:

```go
err := tm.SubmitTransaction(ctx, func(tc TransactionContext) (err error) {
    // transaction body
    return
})
```


---

# 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://goooqo.docs.doyto.win/api/transaction.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.
