Protected access to variable m_preparedCount via synchronized
authorJan Wieck <JanWieck@Yahoo.com>
Thu, 9 Oct 2003 01:17:07 +0000 (01:17 +0000)
committerJan Wieck <JanWieck@Yahoo.com>
Thu, 9 Oct 2003 01:17:07 +0000 (01:17 +0000)
function to prevent multiple threads using automatic cursors on
the same connection from stomping over each others cursor.

Jan

src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1Statement.java

index 88f7891d07bbc069f83a671155729b86e64a0f72..0a11f3a3b0c87c24f2fd52101a40f793700a901c 100644 (file)
@@ -26,7 +26,7 @@ import java.sql.Timestamp;
 import java.sql.Types;
 import java.util.Vector;
 
-/* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1Statement.java,v 1.39 2003/09/23 06:13:52 barry Exp $
+/* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1Statement.java,v 1.40 2003/10/09 01:17:07 wieck Exp $
  * This class defines methods of the jdbc1 specification.  This class is
  * extended by org.postgresql.jdbc2.AbstractJdbc2Statement which adds the jdbc2
  * methods.  The real Statement class (for jdbc1) is org.postgresql.jdbc1.Jdbc1Statement
@@ -73,7 +73,15 @@ public abstract class AbstractJdbc1Statement implements BaseStatement
         protected boolean m_statementIsCursor = false;
 
        private boolean m_useServerPrepare = false;
+
+    // m_preparedCount is used for naming of auto-cursors and must
+    // be synchronized so that multiple threads using the same
+    // connection don't stomp over each others cursors.
        private static int m_preparedCount = 1;
+    private synchronized static int next_preparedCount()
+    {
+        return m_preparedCount++;
+    }
 
        //Used by the callablestatement style methods
        private static final String JDBC_SYNTAX = "{[? =] call <some_function> ([? [,?]*]) }";
@@ -316,7 +324,7 @@ public abstract class AbstractJdbc1Statement implements BaseStatement
                {
                        if (m_statementName == null)
                        {
-                               m_statementName = "JDBC_STATEMENT_" + m_preparedCount++;
+                               m_statementName = "JDBC_STATEMENT_" + next_preparedCount();
                                m_origSqlFragments = new String[m_sqlFragments.length];
                                m_executeSqlFragments = new String[m_sqlFragments.length];
                                System.arraycopy(m_sqlFragments, 0, m_origSqlFragments, 0, m_sqlFragments.length);
@@ -375,7 +383,7 @@ public abstract class AbstractJdbc1Statement implements BaseStatement
                         // The first thing to do is transform the statement text into the cursor form.
                         String[] cursorBasedSql = new String[m_sqlFragments.length];
                         // Pinch the prepared count for our own nefarious purposes.
-                        String statementName = "JDBC_CURS_" + m_preparedCount++;
+                        String statementName = "JDBC_CURS_" + next_preparedCount();
                         // Setup the cursor decleration.
                         // Note that we don't need a BEGIN because we've already
                         // made sure we're executing inside a transaction.