ArraIndexOutOfBoundsException when looping through DataSet after a delete:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 2
at java.util.Vector.get(Vector.java:694)
at com.sun.sql.DataSetImpl$DataSetItr.next(DataSetImpl.java:1797)
at EODDeleteBug.main(EODDeleteBug.java:80)
May be reproduced by:
import java.sql.*;
public class EODDeleteBug
{
static public class Person {
public Person() {
// Needed for DataSet
firstName = null;
lastName = null;
}
public Person(String f, String l) {
firstName = f;
lastName = l;
}
public String firstName;
@ResultColumn(uniqueIdentifier=true)
public String lastName;
public String toString()
{
if (firstName == null) {
return lastName;
} else {
return lastName + ", " + firstName;
}
}
public boolean equals(Person other)
{
if (firstName == null) {
return lastName.equals(other.lastName);
} else {
return lastName.equals(other.lastName) &&
firstName.equals(other.firstName);
}
}
}
interface PersonQueryObject extends BaseQuery {
@Select(sql="select firstName, lastName from persons")
DataSet<Person> getPersons();
@Select(sql="select firstName, lastName from persons", readOnly=false)
DataSet<Person> getAndUpdatePersons();
@java.sql.Update(sql="insert into persons values (?1,?2)")
int insertPerson(String a, String b);
@java.sql.Update(sql="create table persons (firstName varchar(256), lastName varchar(256) unique not null)")
int createTable();
}
static public void main(String []args)
throws Exception
{
String driver = "org.apache.derby.jdbc.EmbeddedDriver";
String url = "jdbc:derby:/export/home/tmp/DB/eod;create=true";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url);
PersonQueryObject pqo = conn.createQueryObject(PersonQueryObject.class);
pqo.createTable();
pqo.insertPerson("John","Doe");
pqo.insertPerson("Wu","Ming");
DataSet<Person> dsp = pqo.getAndUpdatePersons();
System.out.println("--- 1 Delete one of them ---------------------");
for (Person p: dsp) {
System.out.println("Found: " + p);
if (p.lastName.equals("Ming")) {
dsp.delete();
}
}
System.out.println("--- 2 Loop through again ---------------------");
for (Person p: dsp) {
System.out.println("Found: " + p);
}
dsp.close();
pqo.close();
}
}
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 2
at java.util.Vector.get(Vector.java:694)
at com.sun.sql.DataSetImpl$DataSetItr.next(DataSetImpl.java:1797)
at EODDeleteBug.main(EODDeleteBug.java:80)
May be reproduced by:
import java.sql.*;
public class EODDeleteBug
{
static public class Person {
public Person() {
// Needed for DataSet
firstName = null;
lastName = null;
}
public Person(String f, String l) {
firstName = f;
lastName = l;
}
public String firstName;
@ResultColumn(uniqueIdentifier=true)
public String lastName;
public String toString()
{
if (firstName == null) {
return lastName;
} else {
return lastName + ", " + firstName;
}
}
public boolean equals(Person other)
{
if (firstName == null) {
return lastName.equals(other.lastName);
} else {
return lastName.equals(other.lastName) &&
firstName.equals(other.firstName);
}
}
}
interface PersonQueryObject extends BaseQuery {
@Select(sql="select firstName, lastName from persons")
DataSet<Person> getPersons();
@Select(sql="select firstName, lastName from persons", readOnly=false)
DataSet<Person> getAndUpdatePersons();
@java.sql.Update(sql="insert into persons values (?1,?2)")
int insertPerson(String a, String b);
@java.sql.Update(sql="create table persons (firstName varchar(256), lastName varchar(256) unique not null)")
int createTable();
}
static public void main(String []args)
throws Exception
{
String driver = "org.apache.derby.jdbc.EmbeddedDriver";
String url = "jdbc:derby:/export/home/tmp/DB/eod;create=true";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url);
PersonQueryObject pqo = conn.createQueryObject(PersonQueryObject.class);
pqo.createTable();
pqo.insertPerson("John","Doe");
pqo.insertPerson("Wu","Ming");
DataSet<Person> dsp = pqo.getAndUpdatePersons();
System.out.println("--- 1 Delete one of them ---------------------");
for (Person p: dsp) {
System.out.println("Found: " + p);
if (p.lastName.equals("Ming")) {
dsp.delete();
}
}
System.out.println("--- 2 Loop through again ---------------------");
for (Person p: dsp) {
System.out.println("Found: " + p);
}
dsp.close();
pqo.close();
}
}