使用mybatis、springmvc、memcache(之前使用ehcache不会报错),报如下错误:

ERROR 2014-10-27 17:02:02 com.dbt.vintegration.vgnrebatereceipts.vpsrebatereceiptsprofitinfo.action.VpsRebateReceiptsProfitInfoAction In the method VpsRebateReceiptsProfitInfoAction.index(HttpServletRequest request,HttpServletResponse response) exists error!
org.apache.ibatis.executor.ExecutorException: Cannot get Configuration as configuration factory was not set.
at org.apache.ibatis.executor.loader.ResultLoaderMap$LoadPair.getConfiguration(ResultLoaderMap.java:208)
at org.apache.ibatis.executor.loader.ResultLoaderMap$LoadPair.load(ResultLoaderMap.java:179)
at org.apache.ibatis.executor.loader.AbstractEnhancedDeserializationProxy.invoke(AbstractEnhancedDeserializationProxy.java:75)
at org.apache.ibatis.executor.loader.cglib.CglibProxyFactory$EnhancedDeserializationProxyImpl.intercept(CglibProxyFactory.java:175)
at com.dbt.vintegration.vgnrebatereceipts.vgnrebatereceiptsinfom.bean.VgnRebateReceiptsInfoM$$EnhancerByCGLIB$$c6ff32bf.getProfitList(<generated>)
at com.dbt.vintegration.vgnrebatereceipts.vpsrebatereceiptsprofitinfo.action.VpsRebateReceiptsProfitInfoAction.getReceiptsInfoMList(VpsRebateReceiptsProfitInfoAction.java:174)
at com.dbt.vintegration.vgnrebatereceipts.vpsrebatereceiptsprofitinfo.action.VpsRebateReceiptsProfitInfoAction.appmarketrebate(VpsRebateReceiptsProfitInfoAction.java:140)
at com.dbt.vintegration.vgnrebatereceipts.vpsrebatereceiptsprofitinfo.action.VpsRebateReceiptsProfitInfoAction.index(VpsRebateReceiptsProfitInfoAction.java:92)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at com.dbt.framework.base.web.PublicContextFilter.doFilter(PublicContextFilter.java:35)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

由ehcache切换为memcache后,后台业务逻辑代码都是一样的,第一次读取出来至微信上没有问题,第二次就报错了,很容易想到这是缓存原因导致的,定位到问题发现只要mybatis中resultMap标签使用了collection、association就会报这个错,如这种配置:

<resultMap type="VgnRebateReceiptsInfoM" id="queryMyReceiptsRebateListResultMap" extends="vgnRebateReceiptsInfoMResultMap">
<result property="ifread" column="ifread"/>
<collection property="profitList" javaType="java.util.ArrayList" ofType="VpsRebateReceiptsProfitInfo" column="receiptskey"  select="com.dbt.vintegration.vgnrebatereceipts.vpsrebatereceiptsprofitinfo.dao.IVpsRebateReceiptsProfitInfoDao.queryDetailofReceiptByReceiptskey"></collection>
</resultMap>

百度后发现memcache不能与mybatis很好的结合,不能自动把持久化对象中的对象(包含javabean、list等对象)自动与mybatis的property转换,即序列化。

解决方案如下:

1、在java对象中定义一个序列化字符串,如

 

/** 超市返利详情list **/
	private List<VpsRebateReceiptsProfitInfo> profitList;
	/** 超市返利详情list序列化**/
	private transient String  profitListSerial;
	
	public String getProfitListSerial() {
		return profitListSerial;
	}
	public void setProfitListSerial(String profitListSerial) {
		this.profitListSerial = profitListSerial;
	}


同时生成get、set方法,即把profitList对象转换为profitListSerial字符串,存入memcache中

 

2、在service中进行序列和反序列化操作

 

/**
	 * 查询我的超市返利小票List
	 * 
	 * @param map
	 *            筛选条件
	 * @return List<VgnRebateReceiptsInfoM> 返利小票list
	 */
	@SuppressWarnings("unchecked")
	public List<VgnRebateReceiptsInfoM> queryMyReceiptsRebateList(
			Map<String, Object> map) {
		String userkey = map.get("userkey") == null ? null : map.get("userkey")
				.toString();
		StringBuffer keyBuffer = CacheUtil.getCachekeyMethodPrx(
				CacheUtil.cacheKey.vgnrebate.KEY_VGN_REBATE_RECEIPTS_INFO_M,
				userkey, Thread.currentThread().getStackTrace()[1]
						.getMethodName(), new Object[]{map});
		Object object = CacheUtil.getObjectValue(keyBuffer.toString());
		if (null == object) {
			// 查询数据库并序列化
			object = serializeSkuToJSON(map);
			CacheUtil.put(keyBuffer.toString(), object);
		} else {
			List<VgnRebateReceiptsInfoM> list = (List<VgnRebateReceiptsInfoM>) object;
			if (null != list && !list.isEmpty()) {
				for (VgnRebateReceiptsInfoM vgnRebateReceiptsInfoM : list) {
					if (StringUtils.isNotBlank(vgnRebateReceiptsInfoM
							.getProfitListSerial())) {
						List<VpsRebateReceiptsProfitInfo> profitInfoList = JSON
								.parseArray(vgnRebateReceiptsInfoM
										.getProfitListSerial(),
										VpsRebateReceiptsProfitInfo.class);
						vgnRebateReceiptsInfoM.setProfitList(profitInfoList);
					}
				}
			}
		}
		return (List<VgnRebateReceiptsInfoM>) object;
	}
	private List<VgnRebateReceiptsInfoM> serializeSkuToJSON(
			Map<String, Object> map) {
		List<VgnRebateReceiptsInfoM> list = vgnRebateReceiptsInfoMDao
				.queryMyReceiptsRebateList(map);
		if (null != list && !list.isEmpty()) {
			for (VgnRebateReceiptsInfoM vgnRebateReceiptsInfoM : list) {
				vgnRebateReceiptsInfoM.setProfitListSerial(CustSerializeUtil
						.serializeJSONString(vgnRebateReceiptsInfoM
								.getProfitList()));
			}
		}
		return list;
	}


思路为:先将查询DB的结果集,转换为json对象(获取其他格式),然后在存储至profitListSerial字段中,后续查询走缓存,再获取该profitListSerial字段进行解析或者反序列化.

 

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐