begin_transaction
Start a database transaction to group multiple operations, ensuring data consistency and atomic execution in CockroachDB.
Instructions
Begin a database transaction.
Returns:
Transaction status.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/cockroachdb_mcp/server.py:378-388 (handler)Primary MCP tool handler for 'begin_transaction'. Decorated with @mcp.tool() which registers the tool. Thin wrapper that calls connection_manager.begin_transaction() with error handling.@mcp.tool() async def begin_transaction() -> dict[str, Any]: """Begin a database transaction. Returns: Transaction status. """ try: return await connection_manager.begin_transaction() except Exception as e: return {"status": "error", "error": str(e)}
- Core implementation logic in ConnectionManager class. Checks if transaction active, disables autocommit, sets transaction state.async def begin_transaction(self) -> dict[str, Any]: """Begin a database transaction. Returns: Transaction status. """ conn = await self.ensure_connected() if self._state.in_transaction: return {"status": "error", "error": "Transaction already in progress"} try: # Disable autocommit for transaction await conn.set_autocommit(False) self._state.in_transaction = True return {"status": "started", "message": "Transaction started"} except Exception as e: return {"status": "error", "error": str(e)}
- src/cockroachdb_mcp/server.py:378-388 (registration)Tool registration via @mcp.tool() decorator on the handler function.@mcp.tool() async def begin_transaction() -> dict[str, Any]: """Begin a database transaction. Returns: Transaction status. """ try: return await connection_manager.begin_transaction() except Exception as e: return {"status": "error", "error": str(e)}