Search in sources :

Example 1 with SecondHandler

use of io.mycat.backend.mysql.nio.handler.SecondHandler in project Mycat_plus by coderczp.

the class DruidMycatRouteStrategy method middlerResultRoute.

private RouteResultset middlerResultRoute(final SchemaConfig schema, final String charset, final SQLSelect sqlselect, final int sqlType, final SQLStatement statement, final ServerConnection sc) {
    final String middlesql = SQLUtils.toMySqlString(sqlselect);
    MiddlerResultHandler<String> middlerResultHandler = new MiddlerQueryResultHandler<>(new SecondHandler() {

        @Override
        public void doExecute(List param) {
            sc.getSession2().setMiddlerResultHandler(null);
            String sqls = null;
            // 路由计算
            RouteResultset rrs = null;
            try {
                sqls = buildSql(statement, sqlselect, param);
                rrs = MycatServer.getInstance().getRouterservice().route(MycatServer.getInstance().getConfig().getSystem(), schema, sqlType, sqls.toLowerCase(), charset, sc);
            } catch (Exception e) {
                StringBuilder s = new StringBuilder();
                LOGGER.warn(s.append(this).append(sqls).toString() + " err:" + e.toString(), e);
                String msg = e.getMessage();
                sc.writeErrMessage(ErrorCode.ER_PARSE_ERROR, msg == null ? e.getClass().getSimpleName() : msg);
                return;
            }
            NonBlockingSession noBlockSession = new NonBlockingSession(sc.getSession2().getSource());
            noBlockSession.setMiddlerResultHandler(null);
            // session的预编译标示传递
            noBlockSession.setPrepared(sc.getSession2().isPrepared());
            if (rrs != null) {
                noBlockSession.setCanClose(false);
                noBlockSession.execute(rrs, ServerParse.SELECT);
            }
        }
    });
    sc.getSession2().setMiddlerResultHandler(middlerResultHandler);
    sc.getSession2().setCanClose(false);
    // 路由计算
    RouteResultset rrs = null;
    try {
        rrs = MycatServer.getInstance().getRouterservice().route(MycatServer.getInstance().getConfig().getSystem(), schema, ServerParse.SELECT, middlesql, charset, sc);
    } catch (Exception e) {
        StringBuilder s = new StringBuilder();
        LOGGER.warn(s.append(this).append(middlesql).toString() + " err:" + e.toString(), e);
        String msg = e.getMessage();
        sc.writeErrMessage(ErrorCode.ER_PARSE_ERROR, msg == null ? e.getClass().getSimpleName() : msg);
        return null;
    }
    if (rrs != null) {
        rrs.setCacheAble(false);
    }
    return rrs;
}
Also used : NonBlockingSession(io.mycat.server.NonBlockingSession) List(java.util.List) MiddlerQueryResultHandler(io.mycat.backend.mysql.nio.handler.MiddlerQueryResultHandler) SecondHandler(io.mycat.backend.mysql.nio.handler.SecondHandler) SQLNonTransientException(java.sql.SQLNonTransientException) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) RouteResultset(io.mycat.route.RouteResultset)

Example 2 with SecondHandler

use of io.mycat.backend.mysql.nio.handler.SecondHandler in project Mycat-Server by MyCATApache.

the class DruidMycatRouteStrategy method middlerResultRoute.

private RouteResultset middlerResultRoute(final SchemaConfig schema, final String charset, final SQLSelect sqlselect, final int sqlType, final SQLStatement statement, final ServerConnection sc) {
    final String middlesql = SQLUtils.toMySqlString(sqlselect);
    MiddlerResultHandler<String> middlerResultHandler = new MiddlerQueryResultHandler<>(new SecondHandler() {

        @Override
        public void doExecute(List param) {
            sc.getSession2().setMiddlerResultHandler(null);
            String sqls = null;
            // 路由计算
            RouteResultset rrs = null;
            try {
                sqls = buildSql(statement, sqlselect, param);
                rrs = MycatServer.getInstance().getRouterservice().route(MycatServer.getInstance().getConfig().getSystem(), schema, sqlType, sqls.toLowerCase(), charset, sc);
            } catch (Exception e) {
                StringBuilder s = new StringBuilder();
                LOGGER.warn(s.append(this).append(sqls).toString() + " err:" + e.toString(), e);
                String msg = e.getMessage();
                sc.writeErrMessage(ErrorCode.ER_PARSE_ERROR, msg == null ? e.getClass().getSimpleName() : msg);
                return;
            }
            NonBlockingSession noBlockSession = new NonBlockingSession(sc.getSession2().getSource());
            noBlockSession.setMiddlerResultHandler(null);
            // session的预编译标示传递
            noBlockSession.setPrepared(sc.getSession2().isPrepared());
            if (rrs != null) {
                noBlockSession.setCanClose(false);
                noBlockSession.execute(rrs, ServerParse.SELECT);
            }
        }
    });
    sc.getSession2().setMiddlerResultHandler(middlerResultHandler);
    sc.getSession2().setCanClose(false);
    // 路由计算
    RouteResultset rrs = null;
    try {
        rrs = MycatServer.getInstance().getRouterservice().route(MycatServer.getInstance().getConfig().getSystem(), schema, ServerParse.SELECT, middlesql, charset, sc);
    } catch (Exception e) {
        StringBuilder s = new StringBuilder();
        LOGGER.warn(s.append(this).append(middlesql).toString() + " err:" + e.toString(), e);
        String msg = e.getMessage();
        sc.writeErrMessage(ErrorCode.ER_PARSE_ERROR, msg == null ? e.getClass().getSimpleName() : msg);
        return null;
    }
    if (rrs != null) {
        rrs.setCacheAble(false);
    }
    return rrs;
}
Also used : NonBlockingSession(io.mycat.server.NonBlockingSession) List(java.util.List) MiddlerQueryResultHandler(io.mycat.backend.mysql.nio.handler.MiddlerQueryResultHandler) SecondHandler(io.mycat.backend.mysql.nio.handler.SecondHandler) SQLNonTransientException(java.sql.SQLNonTransientException) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) RouteResultset(io.mycat.route.RouteResultset)

Aggregations

MiddlerQueryResultHandler (io.mycat.backend.mysql.nio.handler.MiddlerQueryResultHandler)2 SecondHandler (io.mycat.backend.mysql.nio.handler.SecondHandler)2 RouteResultset (io.mycat.route.RouteResultset)2 NonBlockingSession (io.mycat.server.NonBlockingSession)2 SQLNonTransientException (java.sql.SQLNonTransientException)2 SQLSyntaxErrorException (java.sql.SQLSyntaxErrorException)2 List (java.util.List)2