Apologies if this message is duplicated somewhere. I can't seem to get
posting to the group to work.
I'm looking at using H2 as a persistent object store. I am storing/
retrieving objects objects directly into the row as OTHER type. Works
well so far, except I ran into this one problem.
I fetch an object by key, then attempt to update a secondary field
using an updateable result set. The update succeeds BUT my object
(which was not updated) now comes out of the row as a byte array,
instead of as a deserialized object. I've tried this with different
object types (String, Integers, more complex objects) and it seems to
happen regardless of the object class.
Am I doing something wrong? Is this just a bug?
I'm attaching a simplified example below. Many thanks
--joel carranza
Gatekeeper Systems
[EMAIL PROTECTED]
import java.io.*;
import java.sql.*;
public class UpdateRow {
static int execute(Connection conn, String sql) throws SQLException {
Statement stmt = conn.createStatement();
int rv = stmt.executeUpdate(sql);
stmt.close();
return rv;
}
private static Object fetch(Connection conn, String sql) throws
SQLException {
Statement s;
ResultSet rs;
s = conn.createStatement();
rs = s.executeQuery(sql);
Object value = null;
if(rs.next()) {
value = rs.getObject(1);
}
rs.close();
s.close();
return value;
}
public static void main(String[] args) throws SQLException,
ClassNotFoundException, IOException {
Class.forName("org.h2.Driver");
File file = File.createTempFile("test", "db");
Connection conn =
DriverManager.getConnection("jdbc:h2:"+file.getAbsolutePath());
execute(conn,"CREATE TABLE object_index (id integer primary key,
object other, number integer)");
PreparedStatement stmt = conn.prepareStatement("INSERT INTO
object_index (id,object) VALUES (1,?)");
stmt.setObject(1,"hello", Types.JAVA_OBJECT);
stmt.execute();
// select object (as string) from row
System.out.println("select = "+fetch(conn,"SELECT
object,id,number
FROM object_index WHERE id =1"));
// prints "hello"
// update row using updatable result set
Statement s =
conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE);
ResultSet rs = s.executeQuery("SELECT object,id,number FROM
object_index WHERE id =1");
if(rs.next()) {
// prints out "hello"
System.out.println("select during update =
"+rs.getObject(1));
rs.updateInt(2, 1);
rs.updateRow(); // succeeds
}
rs.close();
s.close();
// select again
System.out.println("select after update = "+fetch(conn,"SELECT
object,id,number FROM object_index WHERE id =1"));
// prints byte array
conn.close();
}
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "H2
Database" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/h2-database?hl=en
-~----------~----~----~----~------~----~------~--~---