Java 序列化。字段改变值

发布于 2024-12-28 12:23:32 字数 1504 浏览 5 评论 0原文

我有以下问题。我可以将事务状态设置为“开始、结束或正在进行”。我设置了这个,然后将我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泛泛之交 2025-01-04 12:23:32

ObjectOutputStream 缓存它发送的实例电线(不一定是最好的设计)。但无论如何,如果您打算使用它来(重新)发送相同的对象实例,则需要在调用之间重置输出流。

public void reset() 抛出 IOException

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

另一个 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.

public void reset() throws IOException

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.

Another SO thread discussing this very same issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文