Java序列化。栏位变更值 [英] Java serialization. Field changes value

查看:96
本文介绍了Java序列化。栏位变更值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题。我可以将事务状态设置为开始,结束或进行中。我进行了设置,然后将我的Transaction对象序列化到服务器上,由服务器检索它。第一次(当事务处于启动模式时)就像超级按钮一样工作,但是随后当我重新发送对象时,这次是在进行中模式下,服务器继续在启动模式下看到它。我已经在序列化之前的代码行和反序列化之后的代码行测试了代码,这肯定是问题所在。任何帮助将不胜感激。相关代码段如下:

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:

序列化

        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类:

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;
}


推荐答案

ObjectOutputStream 缓存通过网络发送的实例(不一定是最佳设计tbh)。但是无论如何,如果打算使用它重新发送相同的对象实例,则需要在调用之间重置

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.


public void reset()引发IOException

public void reset() throws IOException

Reset将忽略已写入流中的任何已经
的对象的状态。将状态重置为与新的ObjectOutputStream相同的
。流中的当前点是
,标记为reset,因此相应的ObjectInputStream将在同一点被重置
。先前写入流中的对象不会被视为
。它们将再次写入流中的

Reset will disregard the state of any objects already written to the stream. The state is reset to be the same as a new ObjectOutputStream. The current point in the stream is marked as reset so the corresponding ObjectInputStream will be reset at the same point. Objects previously written to the stream will not be refered to as already being in the stream. They will be written to the stream again.

另一个SO线程讨论了这个相同的问题。

这篇关于Java序列化。栏位变更值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆