Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id CF823633404 for ; Sat, 2 Jan 2010 16:32:48 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 00858-05 for ; Sat, 2 Jan 2010 20:32:32 +0000 (UTC) X-Greylist: delayed 00:10:09.070727 by SQLgrey-1.7.6 Received: from web2.nidhog.com (web2.nidhog.com [66.207.132.3]) by mail.postgresql.org (Postfix) with ESMTP id 64F4D633367 for ; Sat, 2 Jan 2010 16:32:36 -0400 (AST) Received: from XP2 (pool-141-158-96-177.pitt.east.verizon.net [141.158.96.177]) (authenticated bits=0) by web2.nidhog.com (8.14.3/8.13.8) with ESMTP id o02KMNO0028233 for ; Sat, 2 Jan 2010 15:22:24 -0500 (EST) (envelope-from john@johntdow.com) Message-Id: <201001022022.o02KMNO0028233@web2.nidhog.com> From: "John T. Dow" To: "pgsql-jdbc@postgresql.org" Date: Sat, 02 Jan 2010 15:22:07 -0500 X-Mailer: PMMail 2000 Professional (2.20.2717) For Windows 2000 (5.1.2600;3) MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Subject: Optimistic locking with multiple rows X-Nidhog-MailScanner-Information: Please contact the ISP for more information X-Nidhog-MailScanner: Found to be clean X-Nidhog-MailScanner-From: john@johntdow.com X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.589 tagged_above=-10 required=5 tests=BAYES_00=-2.599, FH_DATE_PAST_20XX=3.188 X-Spam-Level: X-Archive-Number: 201001/1 X-Sequence-Number: 20197 I have code that works, I'm just not sure I have the "best" solution. I have applications in which the user can open multiple rows at a time. The user might scroll through the rows and not update some of the rows but might update other rows. Until a row is to be updated, nothing should be locked. At the time of the update, the user should be informed if another user has change the row in question; he can then decide to accept the changes he has made or to leave in place the changes made by the other user. I create a result set for viewing the multiple rows like this: createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); viewResultSet = jdbcStmt.executeQuery("SELECT ... FROM ... WHERE select multiple rows"); Scroll through the resultset to view rows as desired. When positioned at a row, can update that row. See below. To update one of those rows, it is necessary to update that row, then refresh that row in the original multiple row resultset so that if the user scrolls away from the row and then back to it, it shows the updated information. This is the logic I use for updating a single row. jdbcConn.setAutoCommit(false); createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE) updateResultSet = jdbcStmt.executeQuery("SELECT ... FROM ... WHERE select same row FOR UPDATE"); Can compare viewResultSet to updateResetSet to see if changes have been made by another user. resultSet.updateString(colname,colvalue); // one or more column updates currentDatabaseSRS.resultSet.updateRow(); jdbcConn.commit(); jdbcConn.setAutoCommit(true); viewResultSet.refreshRow(); // Refresh the original resultset. This can be very slow. This all seems to work well. The biggest question I have is the very last statement, which refreshes the multi-row read-only resultset. Sometimes this operation is very slow. Specific question: why is refreshRow slow, can I make it faster or should I perhaps execute the original query again. General question: any problems evident with this approach? John