@@ -746,16 +746,18 @@ QueryInfo qi;
746
746
conn = SC_get_conn (self );
747
747
748
748
/* 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 )))) {
759
761
760
762
mylog (" about to begin a transaction on statement = %u\n" , self );
761
763
res = CC_send_query (conn , "BEGIN" , NULL );
@@ -802,7 +804,8 @@ QueryInfo qi;
802
804
/* send the declare/select */
803
805
self -> result = CC_send_query (conn , self -> stmt_with_params , NULL );
804
806
805
- if (globals .use_declarefetch && self -> result != NULL ) {
807
+ if (globals .use_declarefetch && self -> result != NULL &&
808
+ QR_command_successful (self -> result )) {
806
809
807
810
QR_Destructor (self -> result );
808
811
@@ -834,6 +837,15 @@ QueryInfo qi;
834
837
/* We shouldn't send COMMIT. Postgres backend does the
835
838
autocommit if neccessary. (Zoltan, 04/26/2000)
836
839
*/
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
+
837
849
}
838
850
839
851
conn -> status = oldstatus ;
@@ -867,7 +879,9 @@ QueryInfo qi;
867
879
return SQL_ERROR ;
868
880
}
869
881
}
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 );
871
885
} else { /* Bad Error -- The error message will be in the Connection */
872
886
873
887
if (self -> statement_type == STMT_TYPE_CREATE ) {
0 commit comments