× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



For my J2EE development I have each request go through a filter (i.e.
doFilter()) and that creates a ThreadLocal instance which grabs a connection
from the Tomcat connection pool. That connection is then statically used
through out that request's calls to the DB.  As the request is leaving the
server the hibernate session is "closed" and returned to the pool.

HTH,
Aaron Bartell

Here is the code for DBConn.java
public class DBConn implements Filter {
    private static Logger logger = Logger.getLogger(DBConn.class.getName());
    private static SessionFactory sessionFactory;
    private static ThreadLocal tl = new ThreadLocal();

    static {
        try {
            sessionFactory = new
Configuration().configure().buildSessionFactory();
        } catch (HibernateException e) {
            e.printStackTrace();
        }
    }

    public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
            throws IOException, ServletException {
        try {
            chain.doFilter(request, response);
        } finally {
            Session s = (Session) tl.get();
            if (s != null) {
                try {
                    s.flush();
                    s.close();
                } catch (HibernateException e) {
                    e.printStackTrace();
                }
                tl.set(null);
            }
        }
    }

    public static Session sess() {
        Session s = (Session) tl.get();
        if (s == null || !s.isOpen()) {
            try {
                s = sessionFactory.openSession();
            } catch (HibernateException e) {
                e.printStackTrace();
            }
            tl.set(s);
        }
        return s;
    }

    public static void destroyThreadSession() {
        Session s = (Session) tl.get();
        if (s != null) {
            try {
                s.flush();
                s.close();
            } catch (HibernateException e) {
                e.printStackTrace();
            }
            tl.set(null);
        }
    }
    
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    public void destroy() {
    }
}


Web.xml entry:
        <!-- Hibernate data base connection filter -->
        <filter>
                <filter-name>DBConn</filter-name>
                <filter-class>com.mowyourlawn.util.DBConn</filter-class>
        </filter> 


As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.