对于web app使用 per request per session的方式
配置<property name="current_session_context_class">thread</property>
通过SessionFactory的getCurrentSession()方法,该方法会自动获得与当前线程绑定的Session实例。
注意在Servlet的请求方法中不要使用openSession()方法,因为Sevlet采用多线程处理用户请求,A线程通过openSession()获得一个Session的实例,进行数据库操作。同时B线程也通过openSession获得一个新的Session的实例,此时A线程再次使用Session时已经是一个全新的Session实例,会导致冲突。
配置<property name="current_session_context_class">thread</property>
通过SessionFactory的getCurrentSession()方法,该方法会自动获得与当前线程绑定的Session实例。
注意在Servlet的请求方法中不要使用openSession()方法,因为Sevlet采用多线程处理用户请求,A线程通过openSession()获得一个Session的实例,进行数据库操作。同时B线程也通过openSession获得一个新的Session的实例,此时A线程再次使用Session时已经是一个全新的Session实例,会导致冲突。