Skip to content

matlab/matlab-agentic-toolkit / skills-catalog/reporting-and-database-access/matlab-use-database

matlab-use-database

Verified

Reads from, writes to, and manages relational databases using MATLAB Database Toolbox. Use when connecting to databases, reading data with sqlread or fetch, filtering with rowfilter, writing with sqlwrite, updating with sqlupdate, executing SQL statements, managing transactions with commit and rollback, mapping MATLAB classes to tables with ORM (Mappable, ormread, ormwrite, ormupdate), or performing any database operation from MATLAB. Triggers on: database, SQL, sqlread, sqlwrite, sqlupdate, fetch, execute, rowfilter, RowFilter, ORM, Mappable, ormread, ormwrite, ormupdate, orm2sql, transaction, commit, rollback, Database Toolbox, PostgreSQL, MySQL, SQLite, SQL Server, Oracle, database connection, database table, query database, insert data, update rows, delete rows, stored procedure, prepared statement, odbc, databaseConnectionOptions, datasource, data source, DSN, connection string, multithreaded, parallel.

references/transactions.md

View on GitHub
# Transaction Management — commit & rollback

## Basic Transaction Pattern

```matlab
conn.AutoCommit = 'off';

try
    sqlwrite(conn, "orders", orderData);
    sqlwrite(conn, "orderItems", itemData);
    commit(conn);
catch e
    rollback(conn);
    conn.AutoCommit = 'on';
    rethrow(e);
end

conn.AutoCommit = 'on';
```

## Critical Rules

- **ALWAYS** set `AutoCommit` to `'off'` before using `commit`/`rollback`.
- **ALWAYS** use `rollback(conn)` in `catch` blocks to undo partial changes.
- **ALWAYS** restore `AutoCommit` to `'on'` after the transaction block.
- **NEVER** use raw SQL `BEGIN`/`COMMIT`/`ROLLBACK` via `execute` — use MATLAB methods instead.
- If `AutoCommit` is `'on'` (default), each SQL statement auto-commits immediately — `commit`/`rollback` have no effect.

## Complete Example: Atomic Multi-Table Update

```matlab
conn = database("myDataSource", getSecret("dbUser"), getSecret("dbPass"));
conn.AutoCommit = 'off';

try
    % Reduce inventory
    execute(conn, "UPDATE inventory SET Quantity = Quantity - 10 WHERE ProductID = 42");

    % Record the shipment
    shipment = table(42, datetime("now"), 10, ...
        VariableNames=["ProductID", "ShipDate", "Quantity"]);
    sqlwrite(conn, "shipments", shipment);

    commit(conn);
catch e
    rollback(conn);
    conn.AutoCommit = 'on';
    close(conn);
    error("Shipment failed: %s", e.message);
end

conn.AutoCommit = 'on';
close(conn);
```

## Gotchas

- Transaction changes are invisible to other connections until `commit` is called.
- If the MATLAB session crashes before `commit`, all changes are lost (rolled back by the database).
- Some databases (e.g., SQLite) have limited concurrent transaction support.
- `AutoCommit` is a property of the connection object, not a function call.
- Place verification reads (e.g., `sqlread` to confirm) OUTSIDE the try/catch block, after `commit` — a read error inside the catch would trigger an unnecessary `rollback`.

----

Copyright 2026 The MathWorks, Inc.

----
deprecation-and-migrationaddyosmani/agent-skillsManages deprecation and migration. Use when removing old systems, APIs, or features. Use when migrating users from one implementation to another. Use when migrating a database schema in production, such as renaming or dropping a column without downtime (expand/contract). Use when deciding whether to maintain or sunset existing code.Stars: 119kUpdated Claude CodeCodexVerifiedInstall
performance-optimizationaddyosmani/agent-skillsOptimizes application performance across frontend, backend, queries, and databases. Use when performance requirements exist, when you suspect performance regressions, when Core Web Vitals or load times need improvement, when N+1 query patterns need fixing, or when profiling reveals bottlenecks.Stars: 119kUpdated Claude CodeCodexVerifiedInstall
agent-cost-reportthedotmack/claude-memBelievable agent cost report for any period, default the last 7 full days PT, not counting today. Measured tokens from Claude Code transcripts priced at OpenRouter list prices (ESTIMATED), measured provider spend when a sanctioned source exists, note-taker cost separate, Timing-style HTML/PDF plus report.json, line-items.csv, evidence.json.Stars: 118kUpdated Claude CodeCodexVerifiedInstall
agent-cost-reportthedotmack/claude-memBelievable agent cost report for any period, default the last 7 full days PT, not counting today. Measured tokens from Claude Code transcripts priced at OpenRouter list prices (ESTIMATED), measured provider spend when a sanctioned source exists, note-taker cost separate, Timing-style HTML/PDF plus report.json, line-items.csv, evidence.json.Stars: 118kUpdated Claude CodeCodexVerifiedInstall