public class FlushedOpenSessionInViewFilter extends OpenSessionInViewFilter {
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
logger.debug("FIX ME!!!access FlushedOpenSessionInViewFilter");
SessionFactory sessionFactory = lookupSessionFactory(request);
Session session = null;
boolean participate = false;
if (isSingleSession()) {
// single session mode
logger.debug("FIX ME!!!Single Hibernate Session");
if (TransactionSynchronizationManager.hasResource(sessionFactory)) {
// Do not modify the Session: just set the participate flag.
try {
logger.debug("FIX ME!!!begin unbind sessionFactory");
TransactionSynchronizationManager
.unbindResource(sessionFactory);
logger.debug("FIX ME!!!end unbind sessionFactory");
if (TransactionSynchronizationManager.hasResource(lookupDataSource()))
{
logger.debug("FIX ME!!!begin unbind DataSource");
TransactionSynchronizationManager.unbindResource(lookupDataSource());
logger.debug("FIX ME!!!end unbind DataSource");
}
logger.debug("FIX ME!!!begin close hibernate session");
closeSession(session, sessionFactory);
logger.debug("FIX ME!!!end close hibernate session");
} catch (Exception e) {
logger.debug("FIX ME!!!unbind sessionFactory error",e);
}
}
logger.debug("FIX ME!!!begin opening single Hibernate session");
session = getSession(sessionFactory);
logger.debug("FIX ME!!!end opening single Hibernate session");
if (!TransactionSynchronizationManager.hasResource(sessionFactory)){
logger.debug("FIX ME!!!begin binding session");
TransactionSynchronizationManager.bindResource(sessionFactory,
new SessionHolder(session));
logger.debug("FIX ME!!!end binding session");
}
} else {
// deferred close mode
logger.debug("FIX ME!!!not single hibernate session");
if (SessionFactoryUtils.isDeferredCloseActive(sessionFactory)) {
// Do not modify deferred close: just set the participate flag.
participate = true;
} else {
SessionFactoryUtils.initDeferredClose(sessionFactory);
}
}
try {
logger.debug("FIX ME!!!Now filter in FlushedOpenSessionInViewFilter");
filterChain.doFilter(request, response);
logger.debug("FIX ME!!!FlushedOpenSessionInViewFilter filter OK! ");
}
finally {
logger.debug("FIX ME!!!FlushedOpenSessionInViewFilter clean session ");
if (!participate) {
if (isSingleSession()) {
// single session mode
try {
logger.debug("FIX ME!!!unbind sessionFactory in OpenSessionInViewFilter");
TransactionSynchronizationManager
.unbindResource(sessionFactory);
/*if (TransactionSynchronizationManager.hasResource(lookupDataSource()))
{
logger.debug("FIX ME!!!Unbind DataSource in OpenSessionInViewFilter");
TransactionSynchronizationManager.unbindResource(lookupDataSource());
}*/
logger.debug("FIX ME!!!Closing single Hibernate session in OpenSessionInViewFilter");
closeSession(session, sessionFactory);
} catch (Exception e) {
logger.debug("FIX ME!!!unbind sessionFactory error",e);
}
} else {
// deferred close mode
SessionFactoryUtils.processDeferredClose(sessionFactory);
}
}
logger.debug("FIX ME!!!FlushedOpenSessionInViewFilter END ");
}
}
protected void closeSession(Session arg0, SessionFactory arg1) {
arg0.flush();
super.closeSession(arg0, arg1);
}
protected DataSource lookupDataSource() {
String dataSourceBeanName = "dataSource";
WebApplicationContext wac =
WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
return (DataSource) wac.getBean(dataSourceBeanName, DataSource.class);
}
}
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
logger.debug("FIX ME!!!access FlushedOpenSessionInViewFilter");
SessionFactory sessionFactory = lookupSessionFactory(request);
Session session = null;
boolean participate = false;
if (isSingleSession()) {
// single session mode
logger.debug("FIX ME!!!Single Hibernate Session");
if (TransactionSynchronizationManager.hasResource(sessionFactory)) {
// Do not modify the Session: just set the participate flag.
try {
logger.debug("FIX ME!!!begin unbind sessionFactory");
TransactionSynchronizationManager
.unbindResource(sessionFactory);
logger.debug("FIX ME!!!end unbind sessionFactory");
if (TransactionSynchronizationManager.hasResource(lookupDataSource()))
{
logger.debug("FIX ME!!!begin unbind DataSource");
TransactionSynchronizationManager.unbindResource(lookupDataSource());
logger.debug("FIX ME!!!end unbind DataSource");
}
logger.debug("FIX ME!!!begin close hibernate session");
closeSession(session, sessionFactory);
logger.debug("FIX ME!!!end close hibernate session");
} catch (Exception e) {
logger.debug("FIX ME!!!unbind sessionFactory error",e);
}
}
logger.debug("FIX ME!!!begin opening single Hibernate session");
session = getSession(sessionFactory);
logger.debug("FIX ME!!!end opening single Hibernate session");
if (!TransactionSynchronizationManager.hasResource(sessionFactory)){
logger.debug("FIX ME!!!begin binding session");
TransactionSynchronizationManager.bindResource(sessionFactory,
new SessionHolder(session));
logger.debug("FIX ME!!!end binding session");
}
} else {
// deferred close mode
logger.debug("FIX ME!!!not single hibernate session");
if (SessionFactoryUtils.isDeferredCloseActive(sessionFactory)) {
// Do not modify deferred close: just set the participate flag.
participate = true;
} else {
SessionFactoryUtils.initDeferredClose(sessionFactory);
}
}
try {
logger.debug("FIX ME!!!Now filter in FlushedOpenSessionInViewFilter");
filterChain.doFilter(request, response);
logger.debug("FIX ME!!!FlushedOpenSessionInViewFilter filter OK! ");
}
finally {
logger.debug("FIX ME!!!FlushedOpenSessionInViewFilter clean session ");
if (!participate) {
if (isSingleSession()) {
// single session mode
try {
logger.debug("FIX ME!!!unbind sessionFactory in OpenSessionInViewFilter");
TransactionSynchronizationManager
.unbindResource(sessionFactory);
/*if (TransactionSynchronizationManager.hasResource(lookupDataSource()))
{
logger.debug("FIX ME!!!Unbind DataSource in OpenSessionInViewFilter");
TransactionSynchronizationManager.unbindResource(lookupDataSource());
}*/
logger.debug("FIX ME!!!Closing single Hibernate session in OpenSessionInViewFilter");
closeSession(session, sessionFactory);
} catch (Exception e) {
logger.debug("FIX ME!!!unbind sessionFactory error",e);
}
} else {
// deferred close mode
SessionFactoryUtils.processDeferredClose(sessionFactory);
}
}
logger.debug("FIX ME!!!FlushedOpenSessionInViewFilter END ");
}
}
protected void closeSession(Session arg0, SessionFactory arg1) {
arg0.flush();
super.closeSession(arg0, arg1);
}
protected DataSource lookupDataSource() {
String dataSourceBeanName = "dataSource";
WebApplicationContext wac =
WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
return (DataSource) wac.getBean(dataSourceBeanName, DataSource.class);
}
}