3

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;
}
user1018513
  • 1,682
  • 1
  • 20
  • 42

1 Answers1

5

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.

Community
  • 1
  • 1
Perception
  • 79,279
  • 19
  • 185
  • 195