Bug #886: jdbc "update row" can mess up other columns

From: pgsql-bugs(at)postgresql(dot)org
To: pgsql-bugs(at)postgresql(dot)org
Subject: Bug #886: jdbc "update row" can mess up other columns
Date: 2003-01-24 23:46:26
Message-ID: 20030124234626.F2932475CBC@postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs pgsql-jdbc

Andrew Fyfe (afyfe(at)bigtribe(dot)com) reports a bug with a severity of 3
The lower the number the more severe it is.

Short Description
jdbc "update row" can mess up other columns

Long Description
The attached program when run gives me the following output

DatabaseProductName: PostgreSQL
DatabaseProductVersion: 7.3.1
DriverName: PostgreSQL Native Driver
DriverVersion: PostgreSQL 7.3.1 JDBC3 jdbc driver build 106
--- Forward ---
1: Colombian 7.99
2: French_Roast 9.99
--- Backward ---
2: French_Roast 9.99
1: Colombian 7.99
--- Update Row 2 ---
--- Forward ---
1: Colombian 7.99
2: Colombian 10.99

Note that the update of the price in row 2 caused jdbc to misrepesent the name. If one does not run through the rows backward (give the arg "b" when running the code), then the output is correct:

--- Forward ---
1: Colombian 7.99
2: French_Roast 9.99
--- Update Row 2 ---
--- Forward ---
1: Colombian 7.99
2: French_Roast 10.99

Suppressing the first forward pass through the data (giving "f" or "fb" to suppress both) will cause an exception to be thrown.

Note that another forward pass after the backward makes the problem go away.

Sample Code
import java.util.*;
import java.sql.*;

class pgbug
{
static String url = "jdbc:postgresql://localhost/cdb";
static String driver = "org.postgresql.Driver";
static String userName = "cdb";
static String password = "cdb";

public static void print(ResultSet rs) {
try {
String s = rs.getString("COF_NAME");
float n = rs.getFloat("PRICE");
int rowNum = rs.getRow();
System.out.println(rowNum + ":\t" + s + "\t" + n);
} catch (SQLException sqlEx) { /* ignore */
System.out.println("print ResultSet failed");
}
}

public static void forward(ResultSet rs) {
try {
System.out.println("--- Forward ---");
rs.beforeFirst();
while (rs.next()) {
print(rs);
}
} catch (SQLException sqlEx) { /* ignore */
System.out.println("print ResultSet failed");
}
}

public static void backward(ResultSet rs) {
try {
System.out.println("--- Backward ---");
rs.afterLast();
while (rs.previous()) {
print(rs);
}
} catch (SQLException sqlEx) { /* ignore */
System.out.println("print ResultSet failed");
}
}

public static void main(String args [])
throws Exception
{

Connection conn = null;
Statement stmt = null;

try {
// Load the mysql JDBC driver
Class.forName(driver).newInstance();

// Connect to the database
Properties props = new Properties();
props.put("user", userName);
props.put("password", password);
props.put("loglevel", "0");
conn = DriverManager.getConnection(url, props);

// Create a Statement
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);

DatabaseMetaData meta = conn.getMetaData();
System.out.println("DatabaseProductName: " +
meta.getDatabaseProductName());
System.out.println("DatabaseProductVersion: " +
meta.getDatabaseProductVersion());
System.out.println("DriverName: " +
meta.getDriverName());
System.out.println("DriverVersion: " +
meta.getDriverVersion());

try {
stmt.executeUpdate("drop table coffees");
} catch (SQLException sqlEx) { /* ignore */
System.out.println("drop table failed");
}

// create the table
String createTableCoffees = "create table coffees " +
"(COF_NAME VARCHAR(32) primary key, PRICE FLOAT)";
try {
stmt.executeUpdate(createTableCoffees);
} catch (SQLException sqlEx) { /* ignore */
System.out.println("create table failed");
}

stmt.executeUpdate("insert into coffees " +
"VALUES ('Colombian', 7.99)");
stmt.executeUpdate("insert into coffees " +
"VALUES ('French_Roast', 9.99)");

// Select from the test table
String query = "select cof_name, price from coffees";
ResultSet rs = stmt.executeQuery(query);

if (args.length == 0 || args[0].indexOf('f') < 0) {
forward(rs);
}
if (args.length == 0 || args[0].indexOf('b') < 0) {
backward(rs);
}

if (rs.absolute(2)) {
System.out.println("--- Update Row 2 ---");
rs.updateFloat("PRICE", 10.99f);
rs.updateRow();
}

forward(rs);
} finally {
if (stmt != null) {
try {
stmt.close();
}
catch (SQLException sqlEx) {
System.out.println("closing statement failed");
}
}
if (conn != null) {
try {
conn.close();
}
catch (SQLException sqlEx) {
System.out.println("closing connection failed");
}
}
}

}
}

No file was uploaded with this report

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Giles Lean 2003-01-25 03:14:59 Re: Bug #882: Cannot manually log in to database.
Previous Message Tom Lane 2003-01-24 20:53:54 Re: Bug #884: Schema access not inherited by functions declared SECURITY DEFINER

Browse pgsql-jdbc by date

  From Date Subject
Next Message Renaud Waldura 2003-01-25 02:12:03 Re: Requiring Ant 1.5 for build breaks JDK 1.1 compatibility
Previous Message Dave Cramer 2003-01-24 19:33:42 Re: [HACKERS] JDBC drivers and streaming content