Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 54bc3b6

Browse files
author
Barry Lind
committed
Fixed bug reported by Marko Strukelj and Keith Wannamaker. Using executeBatch
on a preparedStatement would reset the prepared statment causing subsequent uses of the preparedStatement to fail (i.e. the following series of calls would fail: addBatch() executeBatch() addBatch() executBatch()). This is a regression from 7.2 where this worked correctly. The regression test has also been modified to explicitly test for this case. Modified Files: jdbc/org/postgresql/jdbc1/AbstractJdbc1Statement.java jdbc/org/postgresql/jdbc2/AbstractJdbc2Statement.java jdbc/org/postgresql/test/jdbc2/BatchExecuteTest.java
1 parent b60be3f commit 54bc3b6

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import org.postgresql.largeobject.*;
99
import org.postgresql.util.*;
1010

11-
/* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1Statement.java,v 1.13 2002/11/14 05:35:45 barry Exp $
11+
/* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1Statement.java,v 1.14 2002/11/20 07:34:32 barry Exp $
1212
* This class defines methods of the jdbc1 specification. This class is
1313
* extended by org.postgresql.jdbc2.AbstractJdbc2Statement which adds the jdbc2
1414
* methods. The real Statement class (for jdbc1) is org.postgresql.jdbc1.Jdbc1Statement
@@ -47,7 +47,7 @@ public abstract class AbstractJdbc1Statement implements org.postgresql.PGStateme
4747
private String[] m_origSqlFragments;
4848
private String[] m_executeSqlFragments;
4949
protected Object[] m_binds = new Object[0];
50-
private String[] m_bindTypes = new String[0];
50+
protected String[] m_bindTypes = new String[0];
5151
private String m_statementName = null;
5252
private boolean m_useServerPrepare = false;
5353
private static int m_preparedCount = 1;

src/interfaces/jdbc/org/postgresql/jdbc2/AbstractJdbc2Statement.java

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import org.postgresql.largeobject.*;
99
import org.postgresql.util.PSQLException;
1010

11-
/* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc2/Attic/AbstractJdbc2Statement.java,v 1.8 2002/10/30 04:33:29 barry Exp $
11+
/* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc2/Attic/AbstractJdbc2Statement.java,v 1.9 2002/11/20 07:34:32 barry Exp $
1212
* This class defines methods of the jdbc2 specification. This class extends
1313
* org.postgresql.jdbc1.AbstractJdbc1Statement which provides the jdbc1
1414
* methods. The real Statement class (for jdbc2) is org.postgresql.jdbc2.Jdbc2Statement
@@ -59,7 +59,8 @@ public void addBatch(String p_sql) throws SQLException
5959
{
6060
if (batch == null)
6161
batch = new Vector();
62-
batch.addElement(p_sql);
62+
Object[] l_statement = new Object[] {new String[] {p_sql}, new Object[0], new String[0]};
63+
batch.addElement(l_statement);
6364
}
6465

6566
public void clearBatch() throws SQLException
@@ -76,8 +77,25 @@ public int[] executeBatch() throws SQLException
7677
int i = 0;
7778
try
7879
{
79-
for (i = 0;i < size;i++)
80-
result[i] = this.executeUpdate((String)batch.elementAt(i));
80+
//copy current state of statement
81+
String[] l_origSqlFragments = m_sqlFragments;
82+
Object[] l_origBinds = m_binds;
83+
String[] l_origBindTypes = m_bindTypes;
84+
85+
for (i = 0;i < size;i++) {
86+
//set state from batch
87+
Object[] l_statement = (Object[])batch.elementAt(i);
88+
this.m_sqlFragments = (String[])l_statement[0];
89+
this.m_binds = (Object[])l_statement[1];
90+
this.m_bindTypes = (String[])l_statement[2];
91+
result[i] = this.executeUpdate();
92+
}
93+
94+
//restore state of statement
95+
String[] m_sqlFragments = l_origSqlFragments;
96+
Object[] m_binds = l_origBinds;
97+
String[] m_bindTypes = l_origBindTypes;
98+
8199
}
82100
catch (SQLException e)
83101
{
@@ -150,7 +168,21 @@ public void setResultSetType(int value) throws SQLException
150168

151169
public void addBatch() throws SQLException
152170
{
153-
addBatch(this.toString());
171+
if (batch == null)
172+
batch = new Vector();
173+
174+
//we need to create copies, otherwise the values can be changed
175+
Object[] l_newSqlFragments = null;
176+
if (m_sqlFragments != null) {
177+
l_newSqlFragments = new String[m_sqlFragments.length];
178+
System.arraycopy(m_sqlFragments,0,l_newSqlFragments,0,m_sqlFragments.length);
179+
}
180+
Object[] l_newBinds = new String[m_binds.length];
181+
System.arraycopy(m_binds,0,l_newBinds,0,m_binds.length);
182+
String[] l_newBindTypes = new String[m_bindTypes.length];
183+
System.arraycopy(m_bindTypes,0,l_newBindTypes,0,m_bindTypes.length);
184+
Object[] l_statement = new Object[] {l_newSqlFragments, l_newBinds, l_newBindTypes};
185+
batch.addElement(l_statement);
154186
}
155187

156188
public java.sql.ResultSetMetaData getMetaData() throws SQLException

src/interfaces/jdbc/org/postgresql/test/jdbc2/BatchExecuteTest.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,19 @@ public void testPreparedStatement() throws Exception
157157
pstmt.executeBatch();
158158
assertCol1HasValue(7);
159159

160-
con.commit();
160+
//now test to see that we can still use the statement after the execute
161+
pstmt.setInt(1, 3);
162+
pstmt.addBatch();
161163
assertCol1HasValue(7);
162164

165+
pstmt.executeBatch();
166+
assertCol1HasValue(10);
167+
168+
con.commit();
169+
assertCol1HasValue(10);
170+
163171
con.rollback();
164-
assertCol1HasValue(7);
172+
assertCol1HasValue(10);
165173

166174
pstmt.close();
167175
}

0 commit comments

Comments
 (0)