[英]Non-serializable field in serializable class (transient keyword)
I have a serializable class that extends Servlet我有一个扩展 Servlet 的可序列化类
public class FileDownload extends HttpServlet{
@SuppressWarnings("compatibility:6754389671327954013")
private static final long serialVersionUID = 1L;
private ResultSet rset;
......
}
My Question is:我的问题是:
Is the rset
object automatically converted as transient
at compile- or run-time? rset
对象是否在编译时或运行时自动转换为transient
? or do I have to mark it explicitly as transient
?还是我必须将其明确标记为
transient
? (That is a warning brought up from my IDE JDeveloper). (这是我的 IDE JDeveloper 发出的警告)。
No, the field is not neglected by serialization - you'll get a java.io.NotSerializableException
if you try to serialize an instance of FileDownload
.不,序列化不会忽略该字段 - 如果您尝试序列化
FileDownload
的实例,您将得到java.io.NotSerializableException
。 Mark it transient
.将其标记为
transient
。 Btw, what is a ResultSet
doing as a field in a Servlet
?顺便说一句,
ResultSet
作为Servlet
中的字段做什么? This is not thread-safe.这不是线程安全的。
ResultSets
should be local variables only, in any context.在任何上下文中,
ResultSets
都应该只是局部变量。
You have to call transient
your own.But you cannot serialize
an object unless it implements Serializable
interface.ResultSet is such kind of object.你必须自己调用
transient
。但是你不能serialize
一个对象,除非它实现了Serializable
接口。ResultSet 就是这样一种对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.