runInTransaction
The runInTransaction
attribute specifies whether a changeset can be run as a single transaction (if possible). The default value is true
.
Uses
When you make a database deployment with the update command, Liquibase attempts to create an atomic "transaction block" around each changeset to ensure that it is either fully deployed to the database or not deployed at all. This protects your database from incomplete deployments with unpredictable behavior.
However, your database may not allow certain SQL statements to run in a transaction block, such as CREATE DATABASE
in PostgreSQL or ALTER TABLE
on an external table in AWS Redshift. If you need to deploy a changeset where this is the case, set runInTransaction
to false
on that changeset.
Warning: If runInTransaction
is set to false
and an error occurs part way through running a changeset that contains multiple statements, the Liquibase DATABASECHANGELOG table will be left in an invalid state.
Tip: In Liquibase Pro 4.27.0 and later, the CheckRunInTransactionValue
policy check is enabled by default so that you can easily confirm whether your changesets have the correct configuration.
Syntax
Note: All changelog attributes use the camelCase
format.
--liquibase formatted sql
--changeset your.name:1 runInTransaction:false
CREATE DATABASE myDatabase;
{
"databaseChangeLog": [
{
"changeSet": {
"id": "1",
"author": "your.name",
"runInTransaction": "false",
"changes": [
{
"sql": {
"sql": "CREATE DATABASE myDatabase;"
}
}
]
}
}
]
}
databaseChangeLog:
- changeSet:
id: 1
author: your.name
runInTransaction: false
changes:
- sql:
sql: CREATE DATABASE myDatabase;
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd
http://www.liquibase.org/xml/ns/pro
http://www.liquibase.org/xml/ns/pro/liquibase-pro-latest.xsd">
<changeSet id="1" author="your.name" runInTransaction="false">
<sql>CREATE DATABASE myDatabase;</sql>
</changeSet>
</databaseChangeLog>
Troubleshooting
If you specify a native executor other than JDBC using runWith
, you cannot also set runInTransaction
to true
. If you do, Liquibase throws an UnexpectedLiquibaseException
error:
Unexpected error running Liquibase: Migration failed for changeset runwith-tr.sql::1::runSQLTableWithPsql::Liquibase Pro User:
Reason: liquibase.exception. DatabaseException: liquibase.exception.LiquibaseException: liquibase.exception. UnexpectedLiquibaseException: psql returned a code of 3