[Pljava-dev] Passing custom java object to PL/Java

From: rakesh at rakeshv(dot)org (Rakesh Vidyadharan)
To:
Subject: [Pljava-dev] Passing custom java object to PL/Java
Date: 2005-10-18 03:05:30
Message-ID: 127953D5-2CBD-4DFE-85CB-DEF2B131900B@rakeshv.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pljava-dev


On 17 Oct 2005, at 16:45, Petr Mich?lek wrote:

> Hi!
>
> I don't know, if I'm right, but try to serialize object Recurrence
> like this:
>
> CLIENT SIDE:
> ============
> class Recurrence implements Serializable //...should be enough
> {
> ...
> }
>
> Recurrence rec = ....;
> ByteArrayOutputStream baos = new ...;
> ObjectOutputStream oos = new ObjectOutputStream( baos );
> oos.writeObject( rec );
> oos.close();
>
> byte[] value = baos.toByteArray();
> statement.setBinaryStream( i++, new ByteArrayInputStream
> (value),value.length); // Try use type BLOB!
> // I don't know if this is most effective methond, how to pass BLOB
> argument to JDBC statement...
>
>
> SERVER SIDE:
> ============
> ....
> Recurrence rec = (Recurrence)new ObjectInputStream
> ( byteArrayInputStream ).readObject();
>
>
> Try it and tell us if such method works!
>
> If it throws SerializebleException, try to pass simpler argument
> like string and so on.
>
> Regards
> Petr Michalek

Thanks Petr for your suggestion. It turned out to be unnecessary. I
was indeed thinking of trying raw serialization or using something
like XStream to convert the object into an XML representation and
parsing it back on the server (less efficient but easier to debug).

It turns out that the first problem with my test program was that I
was specifying the type as "testType" and not "testtype", to which
Postgres converted the name in the typname column of
pg_catalog.pg_type. Once I fixed that issue, I was able to send the
object through to the server. The server "nicely" returned an error
message saying it could not find the appropriate method:

java.sql.SQLException: ERROR: Unable to find static method
RecurrenceTest.test with signature (Ljava/sql/ResultSet;)I

That was all the information I needed :-)

I just updated my static method to take as parameter a ResultSet, and
then just walked the ResultSet to get the values I had set. Ability
to use ResultSetMetaData would have been nice, but since I know the
structure of the data I am passing I can easily work without it.

I have updated the source files at the URL's I posted initially with
the working code in case anyone is interested. I only wish it had
been this easy to process the result of a setof returned by a PL/Java
function, instead of having to parse the delimited fields out of a
PGObject.getValue() string.

Thanks Thomas and Petr once again for your responses.

Rakesh

In response to

Browse pljava-dev by date

  From Date Subject
Next Message Mncedisi Kheswa 2005-10-18 11:43:20 [Pljava-dev] PL/Java help.
Previous Message Petr Michálek 2005-10-17 21:45:05 [Pljava-dev] Passing custom java object to PL/Java