fescar的创新之处在于,每个局部事务执行完立即提交,释放本地锁;它会去解析你代码中的sql,从数据库中获得事务提交前的事务资源即数据,存放到undo_log中,全局事务协调器在回滚的时候直接使用undo_log中的数据覆盖你提交的数据。 spring boot如何集成fescar?
我们可以从官方代码库中看到,fescar目前提供的示例是针对使用dubbo的服务,那spring boot的项目如何集成fescar呢?
import com.alibaba.fescar.core.context.rootcontext;
import org.apache.commons.lang.stringutils;
import org.slf4j.logger;
import org.slf4j.loggerfactory;
import org.springframework.web.filter.onceperrequestfilter;
import javax.servlet.filterchain;
import javax.servlet.servletexception;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import java.io.ioexception;
public class fescarxidfilter extends onceperrequestfilter {
protected logger logger = loggerfactory.getlogger(fescarxidfilter.class);
@override
protected void dofilterinternal(httpservletrequest request, httpservletresponse response, filterchain filterchain) throws servletexception, ioexception {
string xid = rootcontext.getxid();
string restxid = request.getheader("fescar-xid");
boolean bind = false;
if(stringutils.isblank(xid)&&stringutils.isnotblank(restxid)){
rootcontext.bind(restxid);
bind = true;
if (logger.isdebugenabled()) {
logger.debug("bind[" + restxid + "] to rootcontext");
}
}
try{
filterchain.dofilter(request, response);
} finally {
if (bind) {
string unbindxid = rootcontext.unbind();
if (logger.isdebugenabled()) {
logger.debug("unbind[" + unbindxid + "] from rootcontext");
}
if (!restxid.equalsignorecase(unbindxid)) {
logger.warn("xid in change during http rest from " + restxid + " to " + unbindxid);
if (unbindxid != null) {
rootcontext.bind(unbindxid);
logger.warn("bind [" + unbindxid + "] back to rootcontext");
}
}
}
}
}
}
这样就完成了fescar的集成。 开始使用吧!
首先在项目中初始化两个bean:
@bean
public fescarxidfilter fescarxidfilter(){
return new fescarxidfilter();
}
@bean
public globaltransactionscanner scanner(){
globaltransactionscanner scanner = new globaltransactionscanner("fescar-test","my_test_tx_group");
return scanner;
}