Hi Marco;
Thank you for the advice. I tried your suggestion and I can see that the object is
written in the database properly. But I also can see the following trace in the log
file (see trace one). Below is the exact implementation of my write method:
----------------------------------
SerialTest st=new SerialTest(1, "String one");
ByteArrayOutputStream byteOut=new ByteArrayOutputStream();
ObjectOutputStream oOut=new ObjectOutputStream(byteOut);
oOut.writeObject(st);
oOut.flush();
byte[] outs=byteOut.toByteArray();
//ByteArrayInputStream byteIn=new ByteArrayInputStream(outs);
prepSt=connection.prepareStatement("insert into BLOBTAB values (?,?)");
prepSt.setInt(1, 1);
//prepSt.setBinaryStream(2, byteIn, outs.length);
prepSt.setObject(2, outs);
prepSt.execute();
connection.commit();
prepSt.close();
----------------------------------
I get similar trace if I use prepSt.setBinaryStream() instead of prepSt.setObject()
(see commented out statements).
The other issue is related to reading the object from the database.
If I try to read the previously written object using the following code:
-----------------------------------
prepSt=connection.prepareStatement("select ID,TSTBLOB from BLOBTAB");
rs=prepSt.executeQuery();
while(rs.next()) {
int id=rs.getInt(1);
byte[] bytes=(byte[])rs.getObject(2); // Exception here
java.io.ByteArrayInputStream byteIn=new ByteArrayInputStream(bytes);
java.io.ObjectInputStream oIn=new ObjectInputStream(byteIn);
Object obj=oIn.readObject();
byteIn.close();
oIn.close();
SerialTest st=(SerialTest)obj;
System.out.println("ID:"+id+" Ser ID:"+st.ival+" Ser Str:"+st.sval);
}
prepSt.close();
-----------------------------------
my application gets the following exception "java.lang.ClassCastException:
com.sap.dbtech.jdbc.translators.Getval$InputStream" and the following trace is written
in (see trace two below).
But if read the same previously recorded object with the following code:
-----------------------------------
prepSt=connection.prepareStatement("select ID,TSTBLOB from BLOBTAB");
rs=prepSt.executeQuery();
while(rs.next()) {
int id=rs.getInt(1);
java.sql.Blob bl=rs.getBlob(2);
java.io.ObjectInputStream oIn=new ObjectInputStream(bl.getBinaryStream());
Object obj=oIn.readObject();
oIn.close();
SerialTest st=(SerialTest)obj;
System.out.println("ID:"+id+" Ser ID:"+st.ival+" Ser Str:"+st.sval);
}
prepSt.close();
-----------------------------------
I can get the object (so everything is OK from the application perspectives) but I
also see the following trace in the log (see trace three).
The reason why I am so eager to use setObejct()/getObject() combination is very
simple: I am using the JDBC driver with the application server (Borland Enterprise
Server). Specifically I map java.io.Serializable attributes of CMP entity beans.
When I do it, the EJB container generates setObject/getObject methods to write/read
objects, which I can not control. So without these methods I can not use CMP entity
beans.
And one more issue: I downloaded the source code for the driver and tried to compile
it but apparently I am missing one utility class: NameHandling. It is used in
jdbc.ConnectionSapDB class (line 233). Can I get this utility class somewhere?
Thank you for your time,
Vadim
------------------------- Trace one ------------------------------------
---- Thread 5debc3 main
==================================
package com.sap.dbtech.jdbc, "SAP DB JDBC Driver", "SAP AG", "7.3.0 Build
023-000-085-131" on Java 1.3.1_01
new Connection 'jdbc:sapdb://ALAMEDA/CMONITOR'
---- Thread 216869 Finalizer
new RTEException: -708 sendData: getOutputStream failed
whereAmI
java.lang.Throwable
at com.sap.dbtech.util.Tracer.whereAmI(Tracer.java:385)
at com.sap.dbtech.rte.comm.RTEException.<init>(RTEException.java:62)
at com.sap.dbtech.rte.comm.SocketComm.sendData(SocketComm.java:702)
at com.sap.dbtech.rte.comm.SocketComm.release(SocketComm.java:600)
at com.sap.dbtech.rte.comm.SocketComm.finalize(SocketComm.java:336)
at java.lang.ref.Finalizer.invokeFinalizeMethod(Native Method)
at java.lang.ref.Finalizer.runFinalizer(Finalizer.java:86)
at java.lang.ref.Finalizer.access$100(Finalizer.java:17)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:163)
---- Thread 5debc3 main
Connection(172e08)::getMetaData ()
<-getMetaData: com.sap.dbtech.jdbc.DatabaseMetaDataSapDB@2d086a
DatabaseMetaData(42719c)::supportsTransactions ()
<-supportsTransactions: true
DatabaseMetaData(42719c)::getTables (null, null, %, [Ljava.lang.String;@30c221)
<-getTables: com.sap.dbtech.jdbc.ResultSetSapDB@12f614
ResultSet(93dcd)::next ()
<-next: false
ResultSet(93dcd)::close ()
<-close
Connection(172e08)::prepareStatement (insert into BLOBTAB values (?,?))
<-prepareStatement: com.sap.dbtech.jdbc.CallableStatementSapDB@13582d
PreparedStatement(56a499)::setInt (1, 1)
<-setInt
PreparedStatement(56a499)::setObject (2, [B@506411)
<-setObject
PreparedStatement(56a499)::execute ()
<-execute: false
Connection(172e08)::commit ()
<-commit
Statement(56a499)::close ()
<-close
Connection(172e08)::close ()
<-close
-------------------------------------------------------------------------------------
----------------------------------- Trace two ---------------------------------------
---- Thread 5debc3 main
==================================
package com.sap.dbtech.jdbc, "SAP DB JDBC Driver", "SAP AG", "7.3.0 Build
023-000-085-131" on Java 1.3.1_01
new Connection 'jdbc:sapdb://ALAMEDA/CMONITOR'
---- Thread 216869 Finalizer
new RTEException: -708 sendData: getOutputStream failed
whereAmI
java.lang.Throwable
at com.sap.dbtech.util.Tracer.whereAmI(Tracer.java:385)
at com.sap.dbtech.rte.comm.RTEException.<init>(RTEException.java:62)
at com.sap.dbtech.rte.comm.SocketComm.sendData(SocketComm.java:702)
at com.sap.dbtech.rte.comm.SocketComm.release(SocketComm.java:600)
at com.sap.dbtech.rte.comm.SocketComm.finalize(SocketComm.java:336)
at java.lang.ref.Finalizer.invokeFinalizeMethod(Native Method)
at java.lang.ref.Finalizer.runFinalizer(Finalizer.java:86)
at java.lang.ref.Finalizer.access$100(Finalizer.java:17)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:163)
---- Thread 5debc3 main
Connection(172e08)::getMetaData ()
<-getMetaData: com.sap.dbtech.jdbc.DatabaseMetaDataSapDB@2d086a
DatabaseMetaData(42719c)::supportsTransactions ()
<-supportsTransactions: true
DatabaseMetaData(42719c)::getTables (null, null, %, [Ljava.lang.String;@30c221)
<-getTables: com.sap.dbtech.jdbc.ResultSetSapDB@12f614
ResultSet(93dcd)::next ()
<-next: false
ResultSet(93dcd)::close ()
<-close
Connection(172e08)::prepareStatement (select ID,TSTBLOB from BLOBTAB)
<-prepareStatement: com.sap.dbtech.jdbc.CallableStatementSapDB@1efb05
PreparedStatement(22c95b)::executeQuery ()
<-executeQuery: com.sap.dbtech.jdbc.ResultSetSapDB@51acd3
ResultSet(2981ca)::next ()
<-next: true
ResultSet(2981ca)::getInt (1)
<-getInt: 1
ResultSet(2981ca)::getObject (2)
<-getObject: com.sap.dbtech.jdbc.translators.Getval$InputStream@21c887
--------------------------------------------------------------------------------------
--------------------------- Trace three ----------------------------------------------
---- Thread 5debc3 main
==================================
package com.sap.dbtech.jdbc, "SAP DB JDBC Driver", "SAP AG", "7.3.0 Build
023-000-085-131" on Java 1.3.1_01
new Connection 'jdbc:sapdb://ALAMEDA/CMONITOR'
---- Thread 216869 Finalizer
new RTEException: -708 sendData: getOutputStream failed
whereAmI
java.lang.Throwable
at com.sap.dbtech.util.Tracer.whereAmI(Tracer.java:385)
at com.sap.dbtech.rte.comm.RTEException.<init>(RTEException.java:62)
at com.sap.dbtech.rte.comm.SocketComm.sendData(SocketComm.java:702)
at com.sap.dbtech.rte.comm.SocketComm.release(SocketComm.java:600)
at com.sap.dbtech.rte.comm.SocketComm.finalize(SocketComm.java:336)
at java.lang.ref.Finalizer.invokeFinalizeMethod(Native Method)
at java.lang.ref.Finalizer.runFinalizer(Finalizer.java:86)
at java.lang.ref.Finalizer.access$100(Finalizer.java:17)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:163)
---- Thread 5debc3 main
Connection(172e08)::getMetaData ()
<-getMetaData: com.sap.dbtech.jdbc.DatabaseMetaDataSapDB@2d086a
DatabaseMetaData(42719c)::supportsTransactions ()
<-supportsTransactions: true
DatabaseMetaData(42719c)::getTables (null, null, %, [Ljava.lang.String;@30c221)
<-getTables: com.sap.dbtech.jdbc.ResultSetSapDB@12f614
ResultSet(93dcd)::next ()
<-next: false
ResultSet(93dcd)::close ()
<-close
Connection(172e08)::prepareStatement (select ID,TSTBLOB from BLOBTAB)
<-prepareStatement: com.sap.dbtech.jdbc.CallableStatementSapDB@1efb05
PreparedStatement(22c95b)::executeQuery ()
<-executeQuery: com.sap.dbtech.jdbc.ResultSetSapDB@51acd3
ResultSet(2981ca)::next ()
<-next: true
ResultSet(2981ca)::getInt (1)
<-getInt: 1
ResultSet(2981ca)::getBlob (2)
<-getBlob: com.sap.dbtech.jdbc.translators.GetvalLob@21c887
ResultSet(2981ca)::next ()
<-next: false
Statement(22c95b)::close ()
<-close
Connection(172e08)::close ()
<-close
--------------------------------------------------------------------------------------
-----Original Message-----
From: Paskamp, Marco [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, March 26, 2002 4:45 AM
To: Vadim Parfenov; [EMAIL PROTECTED]
Subject: RE: Storing java.io.Serializable objects in SapDB
Hello,
make a byte array from your serialized object and insert this byte array:
SerialTest st=new SerialTest(1, "String one"); // SerialTest is
java.io.Serializable object
PreparedStatement prepSt=connection.prepareStatement("insert into
BLOBTAB values (?,?)");
prepSt.setInt(1, 1);
prepSt.setObject(2, st.getBytes()); // This column is declared as
LONG BYTE
prepSt.execute();
connection.commit();
prepSt.close();
Regards,
Marco
----------------------------------------------
Marco PASKAMP
SAP DB, SAP Labs Berlin
> -----Original Message-----
> From: Vadim Parfenov [mailto:[EMAIL PROTECTED]]
> Sent: Dienstag, 26. M�rz 2002 01:35
> To: [EMAIL PROTECTED]
> Subject: Storing java.io.Serializable objects in SapDB
>
>
> Hello;
> I need to store serializable java objects in SapDb. I tried
> the following simple approach:
> 1) Declared LONG BYTE column in my database table;
> 2) Used PreparedStatement.setObject(int, java.lang.Object)
> method to set the object to be stored. Below is small code
> fragment of what I tried to do:
> SerialTest st=new SerialTest(1, "String one"); //
> SerialTest is java.io.Serializable object
> PreparedStatement
> prepSt=connection.prepareStatement("insert into BLOBTAB
> values (?,?)");
> prepSt.setInt(1, 1);
> prepSt.setObject(2, st); // This column is declared as
> LONG BYTE
> prepSt.execute();
> connection.commit();
> prepSt.close();
> Unfortunately the attempt to store object using this approach
> results in the following exception:
> com.sap.dbtech.jdbc.translators.ConversionExceptionSapDB:
> Cannot put ASCII data into this long column
> at
> com.sap.dbtech.jdbc.translators.StreamTranslator.transAsciiStr
> eamForInput(StreamTranslator.java:251)
> at
> com.sap.dbtech.jdbc.translators.StreamTranslator.transStringFo
> rInput(StreamTranslator.java:340)
> at
> com.sap.dbtech.jdbc.translators.DBTechTranslator.transObjectFo
> rInput(DBTechTranslator.java:1031)
> at
> com.sap.dbtech.jdbc.CallableStatementSapDB.setObject(CallableS
> tatementSapDB.java:1571)
> at
> com.sap.dbtech.jdbc.trace.PreparedStatement.setObject(Prepared
> Statement.java:1101)
> at sapdb.BasicCheck.writeTestObject(BasicCheck.java:134)
> Are there any ways to write/read java.io.Serializable objects
> using com.sap.dbtech.jdbc.DriverSapDB driver? I am using
> sapdb-jdbc-bin-7.3.0.23a.jar driver (the latest I found) with
> SapDB 7.3.
> Any help is appreciated.
> Thank you,
> Vadim
>
_______________________________________________
sapdb.general mailing list
[EMAIL PROTECTED]
http://listserv.sap.com/mailman/listinfo/sapdb.general