Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5a03b0c

Browse files
author
Hiroshi Inoue
committed
Change SELECT to not trigger "BEGIN" in not autocommit mode.
1 parent dfbd5d6 commit 5a03b0c

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

src/interfaces/odbc/statement.c

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -746,16 +746,18 @@ QueryInfo qi;
746746
conn = SC_get_conn(self);
747747

748748
/* Begin a transaction if one is not already in progress */
749-
/* The reason is because we can't use declare/fetch cursors without
750-
starting a transaction first.
751-
752-
A transaction should be begun if and only if
753-
we use declare/fetch and the statement is SELECT
754-
or we are not in autocommit state
755-
We assume that the Postgres backend has an autocommit
756-
feature as default. (Zoltan Kovacs, 04/26/2000)
757-
*/
758-
if ( ! self->internal && ! CC_is_in_trans(conn) && ((globals.use_declarefetch && self->statement_type == STMT_TYPE_SELECT) || ! CC_is_in_autocommit(conn))) {
749+
/*
750+
Basically we don't have to begin a transaction in
751+
autocommit mode because Postgres backend runs in
752+
autocomit mode.
753+
We issue "BEGIN" in the following cases.
754+
1) we use declare/fetch and the statement is SELECT
755+
(because declare/fetch must be called in a transaction).
756+
2) we are not in autocommit state and the statement
757+
is of type UPDATE.
758+
*/
759+
if ( ! self->internal && ! CC_is_in_trans(conn) &&
760+
((globals.use_declarefetch && self->statement_type == STMT_TYPE_SELECT) || (! CC_is_in_autocommit(conn) && STMT_UPDATE(self)))) {
759761

760762
mylog(" about to begin a transaction on statement = %u\n", self);
761763
res = CC_send_query(conn, "BEGIN", NULL);
@@ -802,7 +804,8 @@ QueryInfo qi;
802804
/* send the declare/select */
803805
self->result = CC_send_query(conn, self->stmt_with_params, NULL);
804806

805-
if (globals.use_declarefetch && self->result != NULL) {
807+
if (globals.use_declarefetch && self->result != NULL &&
808+
QR_command_successful(self->result)) {
806809

807810
QR_Destructor(self->result);
808811

@@ -834,6 +837,15 @@ QueryInfo qi;
834837
/* We shouldn't send COMMIT. Postgres backend does the
835838
autocommit if neccessary. (Zoltan, 04/26/2000)
836839
*/
840+
/* Even in case of autocommit, started transactions
841+
must be committed. (Hiroshi, 09/02/2001)
842+
*/
843+
if ( ! self->internal && CC_is_in_autocommit(conn) && CC_is_in_trans(conn) && STMT_UPDATE(self)) {
844+
res = CC_send_query(conn, "COMMIT", NULL);
845+
QR_Destructor(res);
846+
CC_set_no_trans(conn);
847+
}
848+
837849
}
838850

839851
conn->status = oldstatus;
@@ -867,7 +879,9 @@ QueryInfo qi;
867879
return SQL_ERROR;
868880
}
869881
}
870-
882+
/* in autocommit mode declare/fetch error must be aborted */
883+
if ( ! was_ok && ! self->internal && CC_is_in_autocommit(conn) && CC_is_in_trans(conn))
884+
CC_abort(conn);
871885
} else { /* Bad Error -- The error message will be in the Connection */
872886

873887
if (self->statement_type == STMT_TYPE_CREATE) {

0 commit comments

Comments
 (0)