yanglimingcn commented on issue #692: Add Mysql Protocol, only support text 
protocol now, not support trans…
URL: https://github.com/apache/incubator-brpc/pull/692#issuecomment-480111084
 
 
   @jamesge 最近在考虑prepared statement 和 
transaction的实现问题。这两个功能的特点是,需要在一个连接上面进行多次回话,在多次回话过程中连接需要独占。
   调用接口的伪代码:
   // begin transaction
   brpc::MysqlRequest txReq;
   brpc::MysqlResponse txRsp;
   brpc::Controller txCntl;
   txCntl.txState = BEGIN_TX;
   txReq.BeginTx();
   channel.CallMethod(NULL, &txCntl, &txReq, &txRsp, NULL);
   
   const brpc::MysqlReply::Tx& tx = txRsp.reply(0).tx();
   
   //txRsp keep a connection
   // begin crud operation
   brpc::MysqlRequest crudReq1;
   brpc::MysqlResponse curdRsp1;
   brpc::Controller crudCntl1;
   crudCntl1.tx = tx;
   txReq1.Query("sql statement1");
   channel.CallMethod(NULL, &crudCntl1, &crudReq1, &crudRsp1, NULL);
   
   brpc::MysqlRequest crudReq2;
   brpc::MysqlResponse curdRsp2;
   brpc::Controller crudCntl2;
   crudCntl2.tx = tx;
   txReq1.Query("sql statement2");
   channel.CallMethod(NULL, &crudCntl2, &crudReq2, &curdRsp2, NULL);
   // end crud operation
   
   // commit transaction
   // after that release the connect to pool
   brpc::MysqlRequest commitReq;
   brpc::MysqlResponse commitRsp;
   brpc::Controller commitCntl;
   commitCntl.tx = tx;
   commitCntl.txState = END_TX;
   commitReq.Commit();
   channel.CallMethod(NULL, &commitCntl, &commitReq, &commitRsp, NULL);
   
   需要对代码修改的地方有
   1、在开始事务的controller里面设置一个标识,这样在它的OnComplete调用的时候,不释放连接到pool里面,返回的接口里面包含事务的信息。
   
2、后续的crud的controller里面设置事务的信息,在IssueRPC的时候,lb和选择连接的地方,可以根据是否设置事务做分支处理,主要是在选择连接的地方。
   3、在结束事务的controller里面设置一个标识,这样它在OnComplete的时候会释放连接。
   
   prepared statement和这个类似。
   
   事务和prepared 
statement的处理流程在所有关系型数据库都类似,也可以抽象出来一些公共的内容。我参考了golang的database/sql库
   
   以上请戈君评审一下,看有什么建议。

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to