// java -cp :. PGObjectTest import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import org.postgresql.Driver; import org.postgresql.util.PGobject; public class PGObjectTest implements Runnable { private static final int ROWCOUNT = 1000; private static final int THREADCOUNT = 10; private static String[] params = new String[ROWCOUNT]; private static String url; private static String user; private static String pw; public static void main(String[] args) { System.out.println("start"); Driver.getVersion(); url = args[0]; user = args[1]; pw = args[2]; try { createTable(); System.out.println("table created"); try { Thread[] t = new Thread[THREADCOUNT]; for (int i = THREADCOUNT; --i >= 0;) { t[i] = new Thread(new PGObjectTest()); t[i].start(); } for (int i = THREADCOUNT; --i >= 0;) { t[i].join(); } } finally { deleteTable(); System.out.println("table deleted"); } } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(); } System.out.println("end"); } private static void createTable() throws SQLException { Connection c = DriverManager.getConnection(url, user, pw); if (c != null) { try { c.createStatement().executeUpdate( "CREATE TABLE pgotest (" + " a VARCHAR(200) NOT NULL PRIMARY KEY," + " b VARCHAR(200) NOT NULL)"); for (int i = ROWCOUNT; --i >= 0;) { String v = Integer.toString(i, 36); params[i] = v; c.createStatement().executeUpdate( "INSERT INTO pgotest VALUES ('" + v + "', '" + v + "_value')"); } } finally { try { c.close(); } catch (SQLException e) { // ignore } } } } private static void deleteTable() throws SQLException { Connection c = DriverManager.getConnection(url, user, pw); if (c != null) { try { c.createStatement().executeUpdate("DROP TABLE pgotest"); } finally { try { c.close(); } catch (SQLException e) { // ignore } } } } public void run() { System.out.println("thread " + Thread.currentThread().getId()); try { Connection c = DriverManager.getConnection(url, user, pw); for (int i = ROWCOUNT; --i >= 0;) { String p = params[i]; ResultSet s = c.createStatement().executeQuery( "SELECT b FROM pgotest WHERE a = '" + p + "'"); s.next(); Object o = s.getObject(1); try { // should not fail ((String) o).length(); } catch (Exception e) { Object vo = (o instanceof PGobject) ? ((PGobject) o) .getValue() : o; // sometimes the driver gets it right on the second try, // sometimes not Object x = s.getObject(1); Object vx = (o instanceof PGobject) ? ((PGobject) o) .getValue() : o; String y; if (x instanceof PGobject) { try { // seems to work in all cases y = s.getString(1); } catch (Throwable e2) { y = e2.getMessage() + " <-- how did this happen?"; } } else { y = "getObject succeeded"; } System.err.println(e.getMessage() + " in " + Thread.currentThread().getId() + ": " + p + " = " + o + " instanceof " + (o != null ? o.getClass().getName() : "???") + " = " + vo + " instanceof " + (vo != null ? vo.getClass().getName() : "???") + "; " + x + " instanceof " + (x != null ? x.getClass().getName() : "???") + " = " + vx + " instanceof " + (vx != null ? vx.getClass().getName() : "???") + "; " + y); } } c.close(); } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(); } } }