See a lot of projects used in the Opensessioninview, this approach is nothing more than the development of convenient, you can operate in the JSP page database layer business. Below say Opensessioninview's usage and performance problem.
Use:
1, add a filter, which is used to control the transaction and database connection management, the code is as follows:
Sessionfactory sessionfactory = lookupsessionfactory (request);
Session session = NULL;
Boolean participate = false; if (Issinglesession ()) {//single session mode if (Transactionsynchronizationmanager.hasresource sessionfacto
ry)) {//does not modify the Session:just set the participate flag.
participate = true;
else {logger.debug ("Opening single Hibernate Sessions in Opensessioninviewfilter");
Session = GetSession (sessionfactory);
Transactionsynchronizationmanager.bindresource (Sessionfactory, new Sessionholder (session)); } else {//deferred close mode if (Sessionfactoryutils.isdeferredcloseactive (sessionfactory)) {//Do n
OT Modify deferred Close:just set the participate flag.
participate = true;
else {sessionfactoryutils.initdeferredclose (sessionfactory);
} try {Filterchain.dofilter (request, response); finally {if (!participate) {
if (Issinglesession ()) {//single session mode Transactionsynchronizationmanager
. Unbindresource (Sessionfactory);
Logger.debug ("Closing single Hibernate Sessions in Opensessioninviewfilter");
CloseSession (session, Sessionfactory);
}else {//deferred close mode Sessionfactoryutils.processdeferredclose (sessionfactory); }
}
}
Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/JSP/
The current code is just an example, the specific need to refer to their own project in the actual situation (Spring configuration, hibernate configuration, etc.)
2, add the configuration information of the filter in Web.xml
<filter>
<filter-name>OpenSessionInView</filter-name>
<filter-class>com .... opensessioninview</filter-class>
</filter>
<filter-mapping>
<filter-name> opensessioninview</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping >
3, in the specific JSP page to take the session operation data
This is the end of the usage here, let's discuss the performance issues briefly below
In fact, everyone according to the configuration of the above can be thought: for the configuration of filter processing, each will intercept to a request, in the background processing will open a thing and open a database connection (thread-safe), after the completion of the background processing will close the current connection. Interception to all requests to do so, people think Opensessioninview way performance will be good where to go? (If you are matching certain requests, the effect is a little bit better)
drew a simple schematic diagram of Opensessioninview, a bit shabby, do not like to spray:
Author: csdn Blog Maple fluttering moment