[libpostgresql-jdbc-java] 09/12: Adjust the deadlock avoidance code for the V3 protocol to be concerned with many statements in a single execute call in addition to the existing worry about many statements from an executeBatch call. This doesn't prevent all possible deadlocks as the deadlock avoidance calculation was written for batch execution which should not be returning ResultSets. If many long queries that return significant results are issued with a single execute we will still deadlock.

Emmanuel Bourg ebourg-guest at moszumanska.debian.org
Mon Jan 9 10:20:44 UTC 2017


This is an automated email from the git hooks/post-receive script.

ebourg-guest pushed a commit to tag REL8_3_604
in repository libpostgresql-jdbc-java.

commit a1999daa58beb3f3e04c064651808c8fccc11ea7
Author: Kris Jurka <books at ejurka.com>
Date:   Sat Oct 18 13:40:40 2008 +0000

    Adjust the deadlock avoidance code for the V3 protocol to be concerned
    with many statements in a single execute call in addition to the
    existing worry about many statements from an executeBatch call.  This
    doesn't prevent all possible deadlocks as the deadlock avoidance
    calculation was written for batch execution which should not be
    returning ResultSets.  If many long queries that return significant
    results are issued with a single execute we will still deadlock.
---
 org/postgresql/core/v3/QueryExecutorImpl.java | 62 ++++++++++++++++++---------
 1 file changed, 42 insertions(+), 20 deletions(-)

diff --git a/org/postgresql/core/v3/QueryExecutorImpl.java b/org/postgresql/core/v3/QueryExecutorImpl.java
index f1f6dc0..5e6ffee 100644
--- a/org/postgresql/core/v3/QueryExecutorImpl.java
+++ b/org/postgresql/core/v3/QueryExecutorImpl.java
@@ -4,7 +4,7 @@
 * Copyright (c) 2004, Open Cloud Limited.
 *
 * IDENTIFICATION
-*   $PostgreSQL: pgjdbc/org/postgresql/core/v3/QueryExecutorImpl.java,v 1.39 2008/01/28 10:08:57 jurka Exp $
+*   $PostgreSQL: pgjdbc/org/postgresql/core/v3/QueryExecutorImpl.java,v 1.39.2.1 2008/05/20 00:04:24 jurka Exp $
 *
 *-------------------------------------------------------------------------
 */
@@ -187,7 +187,9 @@ public class QueryExecutorImpl implements QueryExecutor {
             try
             {
                 handler = sendQueryPreamble(handler, flags);
-                sendQuery((V3Query)query, (V3ParameterList)parameters, maxRows, fetchSize, flags);
+                ErrorTrackingResultHandler trackingHandler = new ErrorTrackingResultHandler(handler);
+                queryCount = 0;
+                sendQuery((V3Query)query, (V3ParameterList)parameters, maxRows, fetchSize, flags, trackingHandler);
                 sendSync();
                 processResults(handler, flags);
             }
@@ -318,31 +320,21 @@ public class QueryExecutorImpl implements QueryExecutor {
 
         try
         {
-            int queryCount = 0;
-
             handler = sendQueryPreamble(handler, flags);
             ErrorTrackingResultHandler trackingHandler = new ErrorTrackingResultHandler(handler);
+            queryCount = 0;
 
             for (int i = 0; i < queries.length; ++i)
             {
-                ++queryCount;
-                if (queryCount >= MAX_BUFFERED_QUERIES)
-                {
-                    sendSync();
-                    processResults(trackingHandler, flags);
-
-                    // If we saw errors, don't send anything more.
-                    if (trackingHandler.hasErrors())
-                        break;
-
-                    queryCount = 0;
-                }
-
                 V3Query query = (V3Query)queries[i];
                 V3ParameterList parameters = (V3ParameterList)parameterLists[i];
                 if (parameters == null)
                     parameters = SimpleQuery.NO_PARAMETERS;
-                sendQuery(query, parameters, maxRows, fetchSize, flags);
+
+                sendQuery(query, parameters, maxRows, fetchSize, flags, trackingHandler);
+
+                if (trackingHandler.hasErrors())
+                    break;
             }
 
             if (!trackingHandler.hasErrors())
@@ -634,19 +626,43 @@ public class QueryExecutorImpl implements QueryExecutor {
     /*
      * Send a query to the backend.
      */
-    private void sendQuery(V3Query query, V3ParameterList parameters, int maxRows, int fetchSize, int flags) throws IOException, SQLException {
+    private void sendQuery(V3Query query, V3ParameterList parameters, int maxRows, int fetchSize, int flags, ErrorTrackingResultHandler trackingHandler) throws IOException, SQLException {
         // Now the query itself.
         SimpleQuery[] subqueries = query.getSubqueries();
         SimpleParameterList[] subparams = parameters.getSubparams();
 
         if (subqueries == null)
         {
-            sendOneQuery((SimpleQuery)query, (SimpleParameterList)parameters, maxRows, fetchSize, flags);
+            ++queryCount;
+            if (queryCount >= MAX_BUFFERED_QUERIES)
+            {
+                sendSync();
+                processResults(trackingHandler, flags);
+
+                queryCount = 0;
+            }
+
+             // If we saw errors, don't send anything more.
+            if (!trackingHandler.hasErrors())
+                sendOneQuery((SimpleQuery)query, (SimpleParameterList)parameters, maxRows, fetchSize, flags);
         }
         else
         {
             for (int i = 0; i < subqueries.length; ++i)
             {
+                ++queryCount;
+                if (queryCount >= MAX_BUFFERED_QUERIES)
+                {
+                    sendSync();
+                    processResults(trackingHandler, flags);
+
+                    // If we saw errors, don't send anything more.
+                    if (trackingHandler.hasErrors())
+                        break;
+
+                    queryCount = 0;
+                }
+
                 // In the situation where parameters is already
                 // NO_PARAMETERS it cannot know the correct
                 // number of array elements to return in the
@@ -1675,6 +1691,12 @@ public class QueryExecutorImpl implements QueryExecutor {
     private final Logger logger;
     private final boolean allowEncodingChanges;
 
+    /**
+     * The number of queries executed so far without processing any results.
+     * Used to avoid deadlocks, see MAX_BUFFERED_QUERIES.
+     */
+    private int queryCount;
+
     private final SimpleQuery beginTransactionQuery = new SimpleQuery(new String[] { "BEGIN" });
 
     private final static SimpleQuery EMPTY_QUERY = new SimpleQuery(new String[] { "" });

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/libpostgresql-jdbc-java.git



More information about the pkg-java-commits mailing list