Java 序列化。字段改变值
我有以下问题。我可以将事务状态设置为“开始、结束或正在进行”。我设置了这个,然后将我的 Transaction 对象序列化到服务器,服务器检索它。第一次(当事务处于启动模式时)工作起来就像一个魅力,但是当我重新发送对象时,这次是在“持续”模式下,服务器继续在“启动”模式下看到它。我已经在序列化之前的行和反序列化之后的行测试了代码,这肯定是问题所在。任何帮助将非常感激。相关代码片段如下:
序列化
if ((query instanceof Transaction) && !(trx.getTransactionState()==Consts.trx_start)) System.out.println("Not start");
oos.writeObject(query);
oos.flush();
反序列化
while (true) {
Object statement = null;
try {
statement = ois.readObject();
if ((statement instanceof Transaction) && !(((Transaction) statement).getTransactionState()==Consts.trx_start)) System.out.println("Not start 2");
handle_statement(statement, socket);
} catch (IOException e) {
和Transaction类:
public class Transaction extends Statement{
/**
*
*/
private static final long serialVersionUID = -2284996855537430822L;
Statement statement_list;
int trx_state;
/**
*
*/
public Transaction() {
trx_state = Consts.trx_start;;
}
/**
* @param statement
*/
public void setStatement(Statement statement ) {
statement_list = statement;
}
public void setTransactionState(int state) {
trx_state = state;
}
public int getTransactionState() {
return trx_state;
}
/**
* @return
*/
public Statement getStatement() {
return statement_list;
}
I have the following problem. I can set the transaction state to be either "start, end or ongoing". I set this, then serialise my Transaction object over to the server, who retrieves it. Works like a charm the first time (when the transaction is in start mode), but then when I resend the object, this time in "ongoing" mode, the server continues to see it in "start" mode. I've tested the code at the line before the serialization, and the line after the deserialization, and this is definitely where the problem is. Any help would be very much appreciated. The relevant code snippets are as follows:
serialization
if ((query instanceof Transaction) && !(trx.getTransactionState()==Consts.trx_start)) System.out.println("Not start");
oos.writeObject(query);
oos.flush();
deserialization
while (true) {
Object statement = null;
try {
statement = ois.readObject();
if ((statement instanceof Transaction) && !(((Transaction) statement).getTransactionState()==Consts.trx_start)) System.out.println("Not start 2");
handle_statement(statement, socket);
} catch (IOException e) {
and the Transaction class:
public class Transaction extends Statement{
/**
*
*/
private static final long serialVersionUID = -2284996855537430822L;
Statement statement_list;
int trx_state;
/**
*
*/
public Transaction() {
trx_state = Consts.trx_start;;
}
/**
* @param statement
*/
public void setStatement(Statement statement ) {
statement_list = statement;
}
public void setTransactionState(int state) {
trx_state = state;
}
public int getTransactionState() {
return trx_state;
}
/**
* @return
*/
public Statement getStatement() {
return statement_list;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ObjectOutputStream 缓存它发送的实例电线(不一定是最好的设计)。但无论如何,如果您打算使用它来(重新)发送相同的对象实例,则需要在调用之间
重置
输出流。另一个 SO 线程讨论这个同样的问题。
The ObjectOutputStream caches instances that it sends over the wire (not necessarily the best design tbh). But in any case, you need to
reset
the output streams in-between calls if you plan on using it to (re)send the same object instance.Another SO thread discussing this very same issue.