Caucho Forums  

This forum is permanently closed because of spam. For free community support, please visit Google Groups:


Go Back   Caucho Forums > Resin

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 08-24-2010, 02:36 PM
erlendfg erlendfg is offline
Junior Member
 
Join Date: Aug 2010
Location: Oslo, Norway
Posts: 3
Default DB connections not released back to pool using Hibernate

When I enable the spy tag in Resin's connection pool for debugging the database connections, I see that connections are never released back to the pool. They will remain open until timeout.

Our application uses Struts 2, Hibernate 3.3.2ga and runs on Resin 3.0.26. I have also tested the application on Resin 4.0.9 without any luck. The version which is now in production uses c3po instead, but we really want to switch to Resin's own connection pool since it is more reliable and stable when the database is down for maintenance.

I was expecting that the connections were released back to the pool when calling session.close(), but they still remain open.

We have an interceptor in Struts 2 which is called at the end of the request, and it will close the Hibernate session. The relevant classes are shown below. Please note that some methods are disabled at the moment since we are migrating from WebWork to Struts 2.

Code:
public class HibernateCloseInterceptor implements Interceptor {

    private static final long serialVersionUID = 3393621813249316358L;
    private static Logger logger = Logger.getLogger(HibernateCloseInterceptor.class);


    public void init() {
    }


    public void destroy() {
    }


    public String intercept(ActionInvocation invocation) throws Exception {

        try {
            // Prosess the request.
            Object result = invocation.invoke();

            // Close Hibernate used with this request.
            HibernateUtil.commitTransaction();
            return (String) result;
        } finally {
            HibernateUtil.closeSession();
        }
    }

}
Code:
package no.uio.webapps.pay.util;

import java.io.FileInputStream;
import java.sql.Connection;
import java.util.Properties;

import no.uio.webapps.pay.exceptions.InfrastructureException;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.classic.Session;

public class HibernateUtil {

    private static final SessionFactory sessionFactory;
    private static Logger logger = Logger.getLogger(HibernateUtil.class);

    static {
        try {

            // Determine which hibernate configuration file to use:
            FileInputStream hibernateFileInputStream;

            String filename = ConfigUtil.getAppConfPath();
            if (!filename.endsWith("properties")) {
                filename = filename + "hibernate.properties";
            }
            logger.info("Resetting hibernate from: " + filename + "\n");

            hibernateFileInputStream = new FileInputStream(filename);
            // logger.info("DEBUG: bytes:" + hibernateFileInputStream.available() );

            // Reads the hibernate properties from file.
            Properties hibProperties = new Properties();
            hibProperties.load(hibernateFileInputStream);

            AnnotationConfiguration cfg = new AnnotationConfiguration();

            cfg.setProperties(hibProperties);
            cfg.configure();

            sessionFactory = cfg.buildSessionFactory();

        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }


    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }


    public static Session getSession() {
        Session session = getSessionFactory().getCurrentSession();
        Transaction transaction = session.getTransaction();

        try {
            if (!transaction.isActive()) {
                session.beginTransaction();

                logger.info("Starting new transaction");
            } else {
                logger.info("Transaction in progress, returning only the current session");
            }
        } catch (HibernateException e) {
            logger.warn("Exception occured in 'beginTransaction'.", e);
            throw new InfrastructureException(e);
        }
        return session;
    }


    public static Connection getConnection() {
        try {
            return getSession().connection();
        } catch (HibernateException e) {
            logger.warn("Exception occured in 'getConnection'.", e);
            throw new InfrastructureException(e);
        }
    }


    public static void rollbackTransaction() {
        try {
            getSession().getTransaction().rollback();
        } catch (HibernateException e) {
            logger.warn("Exception occured in 'rollbackTransaction'.", e);
            throw new InfrastructureException(e);
        }
    }


    public static void commitTransaction() {
        try {
            getSession().getTransaction().commit();
        } catch (HibernateException e) {
            logger.warn("Exception occured in 'commitTransaction'.", e);
            rollbackTransaction();
            throw new InfrastructureException(e);
        }
    }


    public static void beginTransaction() {
        // Transaction transaction = getSession().getTransaction();
        // try {
        // if (transaction == null) {
        // transaction = getSession().beginTransaction();
        // }
        // } catch (HibernateException e) {
        // logger.warn("Exception occured in 'beginTransaction'.", e);
        // throw new InfrastructureException(e);
        // }
    }


    public static void closeSession() {
        Session session = getSession();
        try {
            if (session != null && session.isOpen()) {
                session.close();
            }
        } catch (HibernateException e) {
            logger.warn("Exception occured in 'closeSession'.", e);
            throw new InfrastructureException(e);
        }
    }

}
Reply With Quote
 

Tags
connection, hibernate

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 04:57 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.