An error occurred while using the cache value in JSP.
Solutions to cache value errors in JSP
During this period of time, we found that the system may occasionally encounter data disorder during the data retrieval process. We should extract data A according to the logic and the result will be B's data. Carefully checked the code,
I found that the Code logic was okay, and I was instantly confused. Where did the problem occur. I thought about it for a moment, and there were no problems in the past. Since the cache was added, there were occasional problems. It must be a cache problem.
The cache source code is carefully studied. The original problem occurs when DefaultKeyGenerator generates the key. The Code is as follows:
public class DefaultKeyGenerator implements KeyGenerator { public static final int NO_PARAM_KEY = 0; public static final int NULL_PARAM_KEY = 53; public Object generate(Object target, Method method, Object... params) { if (params.length == 1) { return (params[0] == null ? NULL_PARAM_KEY : params[0]); } if (params.length == 0) { return NO_PARAM_KEY; } int hashCode = 17; for (Object object : params) { hashCode = 31 * hashCode + (object == null ? NULL_PARAM_KEY : object.hashCode()); } return Integer.valueOf(hashCode); } }
From the source code, we found that when the parameter: params has one or zero, the NULL_PARAM_KEY or the parameter params [0] is directly returned. if the params parameter is greater than 1, the system returns the value of the hash value of each parameter Plus 31*17. In this case, the problem occurs. Although the values of parameters A and B are different, the hashcode values of these parameters may be the same, then, Data A may be used to retrieve data B. (In the cache, like map, there are keys and values. value values are obtained based on keys ).
For example, in the following code, although the parameter values are different, the keys are the same.
public static void main(String argv[]) { DefaultKeyGenerator g = new DefaultKeyGenerator(); Integer param0 = 1000000759; String param1 = "11"; System.out.println(" param0="+param0+", param1="+param1+" generate key: "+g.generate(null,null,param0,param1)); Integer param01 = 1000000757; String param11 = "31"; System.out.println("param01="+param01+",param11="+param11+" generate key: "+g.generate(null,null,param01,param11)); }
The running result is as follows:
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!