pgsql-sql-owner

From: ALMA TAHIR <almaheena2003(at)yahoo(dot)co(dot)in>
To: "pgsql-sql-owner(at)postgresql(dot)org" <pgsql-sql-owner(at)postgresql(dot)org>, "pgsql-sql(at)postgresql(dot)org" <pgsql-sql(at)postgresql(dot)org>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, "majordomo(at)postgresql(dot)org" <majordomo(at)postgresql(dot)org>
Subject: pgsql-sql-owner
Date: 2014-03-04 05:38:10
Message-ID: 1393911490.4808.YahooMailNeo@web192701.mail.sg3.yahoo.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: 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();

Thanks in advance Alma

Browse pgsql-sql by date

  From Date Subject
Next Message Adrian Klaver 2014-03-04 15:08:21 Re: pgsql-sq-owner
Previous Message ALMA TAHIR 2014-03-04 05:35:21 Re: pgsql-sq-owner