现在是一些关键操作要记录操作日志,但是在insert的时候,偶然会出现插入失败的问题.现在找不到问题所在?

执行代码

 HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        SsOperLog ssOperLogEntity = new SsOperLog();
        ssOperLogEntity.setOperNo(Long.valueOf(IdGenarater.nextStringValue()));//操作流水号
        UserInfo users = Util.getSessionUserInfo(request);//登录用户
        if (users == null) {//用户还未登录
            String workCode = request.getParameter("workCode");
            String urlString = request.getHeader("host");
            //根据域名获取部门编号
            String deptCode = loginService.getDeptCodeByDomainName(urlString);
            users = loginService.selectByUserIdAndPassword(workCode, null, deptCode);
            if (users == null) {
                return point.proceed();
            }

        }
        ssOperLogEntity.setDeptCode(users.getDeptCode());//用户机构
        ssOperLogEntity.setOperId(Long.valueOf(users.getOperId()));//用户id
        ssOperLogEntity.setOperName(users.getOperName());//操作员
        ssOperLogEntity.setDeptName(users.getDeptName());


        ssOperLogEntity.setOperDate(new Date(System.currentTimeMillis()));//操作日期
        ssOperLogEntity.setOperTime(new Date(System.currentTimeMillis()));//操作时间
        String rquestName = getMthodRemark(point);//操作名称(对应注解里面的remark信息)
        String operType = getMthodOperType(point);//操作类型(对应注解里面的opertype信息)
        String processMethod = point.getSignature().getName();//请求处理方法名 
        ssOperLogEntity.setReruestId(request.getRequestURI());//请求url
        ssOperLogEntity.setRquestName(rquestName);
        if(operType.length()>1){
            //获取大类类型
            ssOperLogEntity.setCategories(operType.substring(0,1));
        }
        ssOperLogEntity.setProcessMethod(processMethod);
        ssOperLogEntity.setOperType(operType);
        ssOperLogEntity.setLocalAddress(request.getRemoteAddr());
        if (!"/aa/login/checkLogin.do".equals(request.getRequestURI())) {
            String requestParams = JSONObject.toJSONString(request.getParameterMap());
            ssOperLogEntity.setRequestParams(requestParams);
        }//请求参数
        Object[] method_param = null;

        Object object;
        try {
            method_param = point.getArgs(); //获取方法参数   
            object = point.proceed();
        } catch (Exception e) {
            ssOperLogEntity.setOperState(OperateStatusEnum.FAIL.getCode());//操作失败
            ssOperLogEntity.setExceptionMesg(e.getMessage());
            if (e.getMessage() == null) {
                ssOperLogEntity.setExceptionMesg(e.toString());
            }
            mongoTemplate.insert(ssOperLogEntity);
            //          ssOperLogMapper.insert(ssOperLogEntity);
            throw e;
        }
        ssOperLogEntity.setOperState(OperateStatusEnum.SUCC.getCode());//操作成功
        mongoTemplate.insert(ssOperLogEntity);  // 这里报错
//        ssOperLogMapper.insert(ssOperLogEntity);
        return object;
    }

异常信息:appserver-b4 等同于ip地址

org.springframework.dao.DataAccessResourceFailureException: Operation on server appserver-b4:27017 failed; nested exception is com.mongodb.MongoException$Network: Operation on server appserver-b4:27017 failed
    at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:77) ~[spring-data-mongodb-1.10.15.RELEASE.jar:na]
    at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:2128) ~[spring-data-mongodb-1.10.15.RELEASE.jar:na]
    at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:461) ~[spring-data-mongodb-1.10.15.RELEASE.jar:na]
    at org.springframework.data.mongodb.core.MongoTemplate.insertDBObject(MongoTemplate.java:1026) ~[spring-data-mongodb-1.10.15.RELEASE.jar:na]
    at org.springframework.data.mongodb.core.MongoTemplate.doInsert(MongoTemplate.java:835) ~[spring-data-mongodb-1.10.15.RELEASE.jar:na]
    at org.springframework.data.mongodb.core.MongoTemplate.insert(MongoTemplate.java:776) ~[spring-data-mongodb-1.10.15.RELEASE.jar:na]
    at org.springframework.data.mongodb.core.MongoTemplate.insert(MongoTemplate.java:767) ~[spring-data-mongodb-1.10.15.RELEASE.jar:na]
    at com.wx.app.ygp.service.log.LogService.doBefore(LogService.java:115) ~[classes/:na]
    at sun.reflect.GeneratedMethodAccessor867.invoke(Unknown Source) ~[na:na]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_162]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_162]
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621) ~[spring-aop-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:610) ~[spring-aop-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:68) ~[spring-aop-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:168) ~[spring-aop-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:654) ~[spring-aop-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at com.wx.app.ygp.action.system.LoginController$$EnhancerBySpringCGLIB$$3d4c3a49.checkLogin(<generated>) ~[classes/:na]
    at sun.reflect.GeneratedMethodAccessor1317.invoke(Unknown Source) ~[na:na]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_162]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_162]
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:222) ~[spring-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137) ~[spring-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) ~[spring-webmvc-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814) ~[spring-webmvc-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737) ~[spring-webmvc-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) [spring-webmvc-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) [spring-webmvc-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) [spring-webmvc-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) [spring-webmvc-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648) [servlet-api.jar:na]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) [spring-webmvc-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) [servlet-api.jar:na]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292) [catalina.jar:8.0.44]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [catalina.jar:8.0.44]
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) [tomcat-websocket.jar:8.0.44]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) [catalina.jar:8.0.44]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [catalina.jar:8.0.44]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121) [spring-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) [catalina.jar:8.0.44]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [catalina.jar:8.0.44]
    at org.springframework.session.web.http.SessionRepositoryFilter.doFilterInternal(SessionRepositoryFilter.java:167) [spring-session-1.3.1.RELEASE.jar:na]
    at org.springframework.session.web.http.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:80) [spring-session-1.3.1.RELEASE.jar:na]
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) [spring-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262) [spring-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) [catalina.jar:8.0.44]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [catalina.jar:8.0.44]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212) [catalina.jar:8.0.44]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94) [catalina.jar:8.0.44]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504) [catalina.jar:8.0.44]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141) [catalina.jar:8.0.44]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) [catalina.jar:8.0.44]
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620) [catalina.jar:8.0.44]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) [catalina.jar:8.0.44]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502) [catalina.jar:8.0.44]
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132) [tomcat-coyote.jar:8.0.44]
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684) [tomcat-coyote.jar:8.0.44]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1533) [tomcat-coyote.jar:8.0.44]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1489) [tomcat-coyote.jar:8.0.44]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_162]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_162]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-util.jar:8.0.44]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_162]
Caused by: com.mongodb.MongoException$Network: Operation on server appserver-b4:27017 failed
    at com.mongodb.DBTCPConnector.doOperation(DBTCPConnector.java:215) ~[mongo-java-driver-2.14.3.jar:na]
    at com.mongodb.DBCollectionImpl.writeWithCommandProtocol(DBCollectionImpl.java:567) ~[mongo-java-driver-2.14.3.jar:na]
    at com.mongodb.DBCollectionImpl.insertWithCommandProtocol(DBCollectionImpl.java:528) ~[mongo-java-driver-2.14.3.jar:na]
    at com.mongodb.DBCollectionImpl.insertImpl(DBCollectionImpl.java:206) ~[mongo-java-driver-2.14.3.jar:na]
    at com.mongodb.DBCollection.insert(DBCollection.java:176) ~[mongo-java-driver-2.14.3.jar:na]
    at com.mongodb.DBCollection.insert(DBCollection.java:93) ~[mongo-java-driver-2.14.3.jar:na]
    at com.mongodb.DBCollection.insert(DBCollection.java:78) ~[mongo-java-driver-2.14.3.jar:na]
    at com.mongodb.DBCollection.insert(DBCollection.java:120) ~[mongo-java-driver-2.14.3.jar:na]
    at org.springframework.data.mongodb.core.MongoTemplate$9.doInCollection(MongoTemplate.java:1031) ~[spring-data-mongodb-1.10.15.RELEASE.jar:na]
    at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:459) ~[spring-data-mongodb-1.10.15.RELEASE.jar:na]
    ... 63 common frames omitted
Caused by: java.net.SocketException: Broken pipe (Write failed)
    at java.net.SocketOutputStream.socketWrite0(Native Method) ~[na:1.8.0_162]
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:111) ~[na:1.8.0_162]
    at java.net.SocketOutputStream.write(SocketOutputStream.java:155) ~[na:1.8.0_162]
    at org.bson.io.PoolOutputBuffer.pipe(PoolOutputBuffer.java:153) ~[mongo-java-driver-2.14.3.jar:na]
    at com.mongodb.DBCollectionImpl.sendWriteCommandMessage(DBCollectionImpl.java:639) ~[mongo-java-driver-2.14.3.jar:na]
    at com.mongodb.DBCollectionImpl.access$300(DBCollectionImpl.java:50) ~[mongo-java-driver-2.14.3.jar:na]
    at com.mongodb.DBCollectionImpl$4.execute(DBCollectionImpl.java:576) ~[mongo-java-driver-2.14.3.jar:na]
    at com.mongodb.DBCollectionImpl$4.execute(DBCollectionImpl.java:567) ~[mongo-java-driver-2.14.3.jar:na]
    at com.mongodb.DBPort.doOperation(DBPort.java:187) ~[mongo-java-driver-2.14.3.jar:na]
    at com.mongodb.DBTCPConnector.doOperation(DBTCPConnector.java:208) ~[mongo-java-driver-2.14.3.jar:na]
    ... 72 common frames omitted

配置MongoDB文件:

mongo.hostport=127.0.0.1:27017
mongo.connectionsPerHost=100
mongo.threadsAllowedToBlockForConnectionMultiplier=6
mongo.dataName=ygp
mongo.username=ygp
mongo.password=ygp_admin

mongo.connectTimeout=60000

mongo.maxWaitTime=120000
mongo.autoConnectRetry=true
mongo.socketKeepAlive=true

mongo.socketTimeout=1500
mongo.slaveOk=true


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mongo="http://www.springframework.org/schema/data/mongo" xsi:schemaLocation="http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.0.xsd
          http://www.springframework.org/schema/data/mongo
          http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">


    <!--设置用户验证-->
    <bean id="userCredentials" class="org.springframework.data.authentication.UserCredentials">
        <constructor-arg name="username" value="${mongo.username}" />
        <constructor-arg name="password" value="${mongo.password}" />
    </bean>


    <!-- 定义mongo对象,对应的是mongodb官方jar包中的Mongo,replica-set设置集群副本的ip地址和端口 -->
    <mongo:mongo id="mongo" replica-set="${mongo.hostport}">
        <!-- 一些连接属性的设置 -->
        <mongo:options
                connections-per-host="${mongo.connectionsPerHost}"
                threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}"
                connect-timeout="${mongo.connectTimeout}"
                max-wait-time="${mongo.maxWaitTime}"
                auto-connect-retry="${mongo.autoConnectRetry}"
                socket-keep-alive="${mongo.socketKeepAlive}"
                socket-timeout="${mongo.socketTimeout}"
                slave-ok="${mongo.slaveOk}"
                write-number="1"
                write-timeout="0"
                write-fsync="true" />
    </mongo:mongo>

    <!-- mongo的工厂,通过它来取得mongo实例,dbname为mongodb的数据库名,没有的话会自动创建 -->
    <bean id="mongoDbFactory"
          class="org.springframework.data.mongodb.core.SimpleMongoDbFactory">
        <constructor-arg ref="mongo" />
        <constructor-arg value="${mongo.dataName}" />
        <constructor-arg ref="userCredentials" />
    </bean>

    <bean id="mappingContext"
          class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" />
    <!--排除 _class -->
    <bean id="defaultMongoTypeMapper"
          class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper">
        <constructor-arg name="typeKey">
            <null />
        </constructor-arg>
    </bean>

    <!-- collection的映射 -->
    <bean id="mappingMongoConverter"
          class="org.springframework.data.mongodb.core.convert.MappingMongoConverter">
        <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
        <constructor-arg name="mappingContext" ref="mappingContext" />
        <property name="typeMapper" ref="defaultMongoTypeMapper" />
    </bean>

    <!-- mongodb的主要操作对象,所有对mongodb的增删改查的操作都是通过它完成 -->
    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
        <constructor-arg name="mongoConverter" ref="mappingMongoConverter" />
    </bean>


</beans>

3个回答

基本问题定位了,是nginx代理了MongoDB的端口进行的转发,造成了网络中断,不用nginx代理,直连MongoDB后,没有这问题了

qq_39164154
qq_39164154 这是MongoDB啊
6 个月之前 回复

mongodb 连接超时了,如果是偶尔出现,有可能是网络抖动

qq_39164154
qq_39164154 问题定位了,nginx做端口转发出的问题
6 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问