Page 1 of 1

Create Database error - cannot run inside a transaction block

Posted: 16.10.2023, 13:50
by gustavototta
Hi everyone!

Can anyone help with this question?

Zeos version: 7.2.14-release
Delphi Version: 10 - Seattle
Postgresql Version: 9.3

I'm trying create database with Zeos on runtime with ZSQLProcessor, but i receive this error:
CREATE DATABASE cannot run inside a transaction block

My script create database:

Code: Select all

    ZSQLProcessor := TZSQLProcessor.Create(Self);
    ZSQLProcessor.Connection := ConexaoLog;

    script := 'CREATE DATABASE imagem '+
              'WITH OWNER = postgres '+
              'TABLESPACE = pg_default '+
              'LC_COLLATE = ''Portuguese_Brazil.1252'' '+
              'LC_CTYPE = ''Portuguese_Brazil.1252'' '+
              'CONNECTION LIMIT = -1;';

    ZSQLProcessor.Script.Text := script;
    ZSQLProcessor.Execute;
    ZSQLProcessor.free;
Observation: In another topic, an user say to change the property "TransactIsolationLevel" to "tiNone", but doesn't work for me.
(because see in the image that I'm already passing it as itNone)
Image

Thank you so much! Hugs

Re: Create Database error - cannot run inside a transaction block

Posted: 17.10.2023, 08:51
by marsupilami
I am not sure if TZSQLProcessor starts a transaction automatically. Is AutoCommit set to true on your connection?
Since the create database statement really is only one statement, you don't need to use TZSQLProcessor. What happens, if you do something like the follwing code block?

Code: Select all

    script := 'CREATE DATABASE imagem '+
              'WITH OWNER = postgres '+
              'TABLESPACE = pg_default '+
              'LC_COLLATE = ''Portuguese_Brazil.1252'' '+
              'LC_CTYPE = ''Portuguese_Brazil.1252'' '+
              'CONNECTION LIMIT = -1;';
    ConexaoLog.ExecuteDirect(script);

Re: Create Database error - cannot run inside a transaction block

Posted: 19.10.2023, 19:26
by gustavototta
Hello, how are you?

The resolution for the problem:
ConexaoLog.AutoCommit := true;

its works! :)

Thanks so much