简体   繁体   中英

How to serialize Jdbc4SQLXML class in java

I just want to know that how we can "Serialize" the Jdbc4SQLXML class, which is extends java.lang.Object and implements java.sql.SQLXML as per postgres API. Even SQLXML interface is not extending Serializable interface.

Why am doing this is,am trying to get one object from some other JVM that time am getting this exception. java.io.NotSerializableException: org.postgresql.jdbc4.Jdbc4SQLXM L.

Here SQLXML is my return type.

Thanks In Advance.

Jdbc4SQLXML is not Serializable because the interface it implements isn't Serializable, the spec doesn't require it. That's for a good reason: JDBC's SQL/XML support is allowed to be "lazy", where the JDBC object for the SQL/XML result is really just a pointer to the data in the database, and it's fetched on demand. That's also why the result object is invalid once the resultset is closed.

So while PgJDBC could make this object serializable, as it doesn't load lazily, it'd mean we could never change it to lazy loading later.

If you want to serialize the XML, use the standard SQL/XML methods to read the XML from the result object and serialize the resulting XML. Don't try to serialize a JDBC result object directly.

See the JDBC tutorial . You will see there how to get a Document from a result object. Document is serializable.

If a class does not implement serializeable you cant send it thru regular api. You could use your own serializer (that works using getter/setters or Java reflection to get in to other non public fields); then send the string over the web server and do the reverse on the other side. Basically your own serialization. But there are packages that can help like http://x-stream.github.io/ Java 7 has inbuilt helpers too

Important question might be why you want to do this? If there are some resources open in Jvm 1 this object might fail in Jvm 2.

Why not make your own POJO and use that to re create the object?

This will guard in case of changes in future releases too - if something changes in this 3rd party object which was not meant to be serialized.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM