code: public void doFilter(ServletRequest request, ServletResponse response,
              FilterChain chain) throws IOException, ServletException {
  
          Session session = SessionFactory.getSession();
  
          // begin
          try {
              session.begin();
  
          } catch(Exception ex){
              logger.error("Failed to begin Session.");
              logger.error(ExceptionUtil.toString(ex));
              throw new RuntimeException(ex);
          }
  
          try {
              chain.doFilter(request, response);
  
              // commit
              if(!session.isRollbackOnly()){
                  try {
                      session.commit();
  
                  } catch(Exception ex){
                      logger.error("Failed to commit Session.");
                      logger.error(ExceptionUtil.toString(ex));
  
                      if(ex instanceof RuntimeException){
                          throw (RuntimeException) ex;
                      }
  
                      throw new RuntimeException(ex);
                  }
              }
          } catch(Exception ex){
              session.setRollbackOnly();
  
          } finally {
              // rollback
              if(session.isRollbackOnly()){
                  try {
                      session.rollback();
  
                  } catch (Exception e) {
                      logger.error("Failed to rollback Session.");
                      logger.error(ExceptionUtil.toString(e));
  
                      if (e instanceof RuntimeException) {
                          throw (RuntimeException) e;
                      }
  
                      throw new RuntimeException(e);
                  }
              }
  
              // release
              try {
                  session.release();
  
              } catch(Exception ex){
                  logger.error("Failed to release Session.");
                  logger.error(ExceptionUtil.toString(ex));
  
                  if(ex instanceof RuntimeException){
                      throw (RuntimeException) ex;
                  }
  
                  throw new RuntimeException(ex);
              }
          }
      }
docs: {@inheritDoc}