Since Sessionfactory.opensession () each get a different object, there is no multi-threaded access to the problem, why do ThreadLocal and other things to solve the so-called problem.
If this is the problem, then I understand that: "Multi-threaded access violation" is not the first problem to be solved, but to solve another problem arising from the problem. The real solution is "how to get a Session object for database operations at any time in the process of processing the program."
A request by the servlet process, will penetrate a lot of layers of code, with parameter transmission is obviously inconvenient, so to be able to "always get", but at the same time hope that the whole process is the same session object, so as to facilitate transaction control requirements. So what to do. Both "Singleton" and "global variables" can achieve this goal, but there will be a "multi-threaded access violation" issue. So there's a way to ThreadLocal like "global variables."