-1

我有以下代码结构。

Transaction 类型的事务处理程序,它是客户端处理程序类中的一个字段,它与服务器通信。(客户端处理程序和服务器并置),客户端通过序列化对象消息与客户端处理程序对话。

当一个新的事务请求来自客户端时,(使用对象输入流的 readObject() 方法进入线程),然后我执行一系列 trx_handler.setFoo(trx.getFoo)))。这很好用,我可以处理第一个请求。但是当后续请求进来时(由于循环结构,它仅在第一个请求完成后才开始执行,我发现 trx 处理程序已重新初始化为其默认值,对象仍然存在,但里面的所有值都是默认的。什么会导致这个问题?

我的第一个猜测是垃圾收集,但是在我的客户端处理程序类中,总是有一个指向这个 trx_handler 的指针。

下面的代码说明了会发生什么。语句首先是 start 类型,因此 trx_handler 将被正确初始化。然后将调用handle_statement。然后应该接收后续语句,但此时 trx_handler 已重新初始化为其默认设置,因此 access_set 字段为空,会话 ID 也是如此,并且对 hande_statement 中的对象所做的任何修改都不可见

谢谢

  public class Handler { 

  private Statement trx_handler; 

 /* Constructor initialises trx_handler to new Statement(); */

 public ClientHandler(final Socket socket, long uid, Server server, ObjectInputStream ois) throws           IOException, Exception {
 LOGGER.info("Constructing Handler");
 this.uid = uid;
 this.server = server;
 this.socket = socket;
 this.database = server.getDB();
 this.trx_sys = database.getTransactionManager();
 create_listening(socket, ois);
 out = socket.getOutputStream();
 oos = new ObjectOutputStream(out);
 this.trx_handler = new Statement(false);

}

  private void create_incoming(final Socket socket, final ObjectInputStream stream) {
  Thread incoming = new Thread() {
  @Override
  public void run() {
    ObjectInputStream ois = stream;
    InputStream in = null;
    while (true) {
      Object statement = null;

      try {

        statement = ois.readObject();
        execute_stat(statement, socket, null);
        LOGGER.info("Ready to execute next "); 
      } catch (SocketException e) {
        LOGGER.severe("Connection Closed");
        return;
      } catch (IOException e) {
        LOGGER.severe("Connection Closed");
        return;
      } catch (ClassNotFoundException e) {
        e.printStackTrace();
      } catch (Exception e) {
        e.printStackTrace();
        String error_message = e.getMessage();
        send_error(socket, error_message);
      }

    }
  }
};
incoming.setDaemon(true);
incoming.start();

}

 private synchronized void execute_stat(Statement trx) {
  if (trx.getTransactionState() == Consts.trx_end) {
    trx_sys.commitTransaction(trx_handler);
    return;
  } else if (trx.getTransactionState() == Consts.trx_start) {
   try {
    trx_handler.setAccessSet(trx.getAccessSet());
    trx_handler.setSession_id(trx.getSession_id());
    trx_sys.startTransaction(trx_handler);
    handle_statement(socket, trx_handler);


    /* TEST HERE THAT FIELDS IN TRX_HANDLER ARE CORRECTLY SET (INCLUDING SOME MODIFIED IN       
    handle_statement and they are correctly set */

  return;
  } catch (Exception ex) {
    Logger.getLogger(ClientHandler.class.getName()).log(Level.SEVERE, null, ex);
  }
}
try {
  LOGGER.info("Execute Trx: stat");

  /* Can't see modifications made in the start case */ 
  Statement stats = trx.getStatement();
  trx_handler.setStatement(stats);
  handle_statement(stats, socket, trx_handler);

} catch (Exception e) {
  e.printStackTrace();

}

return;
}
4

1 回答 1

0

您需要为每个交易发送一个全新的对象,使用ObjectOutputStream.writeUnshared(),或者ObjectOutputStream.reset()在发送之间调用。

于 2013-03-03T02:13:04.577 回答