Re: Function Issue

Поиск
Список
Период
Сортировка
От ALMA TAHIR
Тема Re: Function Issue
Дата
Msg-id 1393846322.42246.YahooMailNeo@web192703.mail.sg3.yahoo.com
обсуждение исходный текст
Ответ на Re: Function Issue  (ALMA TAHIR <almaheena2003@yahoo.co.in>)
Ответы Re: Function Issue  (Adrian Klaver <adrian.klaver@aklaver.com>)
Список pgsql-sql
Hi,

I am using below code in multi threaded environment, but when multiple threads are accessing then i get : "org.postgresql.util.PSQLException: ERROR: tuple concurrently updated" exception. But my concern is I need to use it in multi threaded env, for the same reason I am using FOR UPDATE with cursor. Then where is the issue??? Am I missing something????? Please help me with the same.....
        Statement stmt = c.createStatement();
          // Setup function to call.
          stmt.execute("CREATE OR REPLACE FUNCTION refcursorfunc() RETURNS refcursor AS '"
                                          + " DECLARE "
                                          + "    call_log_rec call_log % rowtype; "
                                          + "             call_log_cursor refcursor; "
                                          + " final_cursor refcursor; "
                                          + " idInt int[]; "
                                          + " BEGIN "
                                          + "    OPEN call_log_cursor FOR SELECT * FROM call_log WHERE aht_read_status = 0 ORDER BY record_sequence_number ASC limit 20 FOR UPDATE; "
                                          + " LOOP "
                                          + " FETCH NEXT FROM call_log_cursor INTO call_log_rec; "
                                          + " EXIT WHEN call_log_rec IS NULL; "
                                          + " UPDATE call_log SET aht_read_status = 1 WHERE CURRENT OF call_log_cursor; "
                                          + " idInt := idInt || ARRAY [call_log_rec.record_sequence_number]; "
                                          + " END LOOP;"
                                          + " OPEN final_cursor FOR SELECT record_sequence_number FROM call_log WHERE record_sequence_number  = ANY(idInt); "
                                          + "    RETURN final_cursor; "
                                          + " END;' language plpgsql");
          stmt.close();
          // We must be inside a transaction for cursors to work.
          c.setAutoCommit(false);

          // Procedure call.
          CallableStatement proc = c.prepareCall("{ ? = call refcursorfunc() }");
          proc.registerOutParameter(1, Types.OTHER);
          System.out.println("BEFORE::: Thread name::: " + Thread.currentThread().getName());
          proc.execute();
         
          ResultSet results = (ResultSet) proc.getObject(1);
          while (results.next()) {
              // do something with the results...
                          System.out.println("Hurrey got the results from SP........");
                          System.out.println("AFTER::::Thread name::: " + Thread.currentThread().getName()+ " record_sequence_number:::: "+results.getString(1));
          }
          c.commit();
          results.close();
          proc.close();



On Friday, 28 February 2014 6:47 PM, ALMA TAHIR <almaheena2003@yahoo.co.in> wrote:
hi,
thankyou for suggestions, its working now. I ma retrieving the ids in an int[] and then using one more cursor to read and return using the int[].



On Thursday, 27 February 2014 8:04 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
ALMA TAHIR <almaheena2003@yahoo.co.in> writes:
> I want to open a ref cursor with select for update and then update
> the records and get the ref cursor in response back in java.

Your function has already sucked all the rows out of the cursor before
it returns it, so it's not surprising that further reads from the cursor
produce nothing.

You could try rewinding the cursor (see MOVE) but I'm not sure that will
help in this case, since the function has carefully ensured that none of
the rows pass the cursor query's WHERE condition anymore.  I think that
since the cursor used SELECT FOR UPDATE, it will not return the updated
rows even after rewinding.  (I could be wrong though, so it's worth

trying.)


I think you need to rethink what you're doing.  This seems like a fairly
silly application design: why not do all the processing you need on these
rows in one place?  Or at the very least, don't use one cursor to serve
two masters.  Possibly you could have the function return the rows itself
instead of passing back a refcursor.

            regards, tom lane


--
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql





В списке pgsql-sql по дате отправления:

Предыдущее
От: David Johnston
Дата:
Сообщение: Re: how to effectively SELECT new "customers"
Следующее
От: Seb
Дата:
Сообщение: creating a new aggregate function