> ## Documentation Index
> Fetch the complete documentation index at: https://docs.arc.cdata.com/llms.txt
> Use this file to discover all available pages before exploring further.

# dbBeginTransaction

> BEGIN TRANSACTION SQL コマンドを実行して、明示的なSQL トランザクションの開始を示します。

export const siteNameShort = "Arc";

`BEGIN TRANSACTION` SQL コマンドを実行します。明示的なSQL トランザクションの開始点を示します。このオペレーションは[dbEndTransaction](./op-db-end-transaction)と組み合わせて使用し、dbBeginTransaction とdbEndTransaction の呼び出しの間で他のデータベースオペレーションを実行します。詳細については、[例](#例)を参照してください。

## 必要なパラメータ

* **driver**: クロスプラットフォーム版ではJDBC ドライバーのクラス名、.NET 版ではADO.NET プロバイダー名。
* **conn**: 接続文字列、またはデータベースURL。

## オプションのパラメータ

* **transactiontimeout**: トランザクションのタイムアウト（秒）。ゼロ（`0`）はタイムアウトなしを意味します。デフォルトは`60` です。
* **transactionid**: トランザクションのId。設定されていない場合、{siteNameShort} はトランザクションId として使用するGUID を生成します。

## アウトプットパラメータ

* **transactionid**: トランザクションのId。

## 例

この例では、dbBeginTransaction とdbEndTransaction の間でデータベースクエリを実行し、SQL トランザクションを作成します。トランザクションのステータスを判断するために、各クエリのステータスがチェックされます。すべてのクエリが成功した場合はトランザクションをコミットし、そうでない場合はトランザクションをロールバックします。

```xml theme={null}
<!-- Creating the input item and defining the SQL queries -->
<arc:set attr="db.driver" value="cdata.jdbc.mysql.MySQLDriver" />
<arc:set attr="db.conn" value="jdbc:cdata:mysql:server=localhost;port=3306;database=sakila;user=root;password=Password123;"/>
<arc:set attr="db.transactionaction" value="COMMIT" />
<arc:set attr="sql.queries#" value="ALTER TABLE Colors ADD Example varchar(255);"/>
<arc:set attr="sql.queries#" value="INSERT INTO Colors VALUES (27, Blue, Sky);" />
<arc:set attr="sql.queries#" value="INSERT INTO Colors VALUES (28, Red, Apple);" />
<arc:set attr="sql.queries#" value="INSERT INTO Colors VALUES (29, Yellow, Banana);" />

<arc:call op="dbBeginTransaction" in="db" out="dbbeginout">
  <arc:try>
    <!-- Enumerate and execute all queries and set the transactionaction based on the result -->
    <arc:enum attr="sql.queries">
      <arc:set attr="db.query" value="[_value]"/>
      <arc:call op="dbNonQuery" in="db" out="results" >
        <arc:set attr="db.transactionaction" value="[results.db:result | equals('success', 'COMMIT', 'ROLLBACK')]" />
      </arc:call>
      <!-- If any queries were flagged as rollback, stop running the queries -->
      <arc:if exp="[db.transactionaction | equals('ROLLBACK')]">
        <arc:break />
      </arc:if>
    </arc:enum>
    <!-- If any exception is raised, catch it and set the transactionaction to rollback -->
    <arc:catch>
      <arc:set attr="db.transactionaction" value="ROLLBACK" />
    </arc:catch>
    <!-- Finally, call dbEndTransaction to either commit or roll back the SQL transaction -->
    <arc:finally>
      <arc:set attr="db.transactionid" value="[dbbeginout.transactionid]" />
      <arc:call op="dbEndTransaction" in="db" />
    </arc:finally>
  </arc:try>
</arc:call>
```

{siteNameShort} がインストールおよび実行されているマシンにドライバーがインストールされている場合に限り、上記の例を変更して別のデータベースを対象にすることができます。例を.NET 用に調整するには、`driver` と`conn` のインプットを次のように変更する必要があります：

```xml theme={null}
<arc:set attr="db.driver" value="System.Data.CData.MySql" /> 
<arc:set attr="db.conn" value="Server=localhost;Database=sakila;UID=root;Password=Password123;"/>
```
