Potential problem with Open Session in View

This article is to introduce the solution of an open connection problem for Open Session in View

 

Servlet 2.3 Filter that binds a Hibernate Session to the thread for the entire processing of the request. Intended for the "Open Session in View" pattern, i.e. to allow for lazy loading in web views despite the original transactions already being completed.

 

This filter makes Hibernate Sessions available via the current thread, which will be autodetected by transaction managers. OpenSessionInViewFilter or  openSessionInViewInterceptor should close the connection  after processing HTTP request ideally.

 

<bean id="sessionFactory"

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" scope="singleton">

<property name="configLocation" value="classpath:orm/hibernate.cfg.xml" />               

 </bean>

 

<?xml version=’1.0′ encoding=’utf-8′?>

<!DOCTYPE hibernate-configuration PUBLIC

        "-//Hibernate/Hibernate Configuration DTD//EN"

        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        ….

        <property name="hibernate.connection.datasource">java:comp/env/jdbc/mysqlDB</property>

        …..

    </session-factory>

</hibernate-configuration>

 

The preceding code might be often used by developers. However, there would be a problem that openSessionInViewFilter will not close the connection after processing HTTP request. (For example, you initialise a new session that not using getSession within HibernateTemplate, will not be managed by openSessionInViewFilter)

 

If we pass dataSource as property of org.springframework.orm.hibernate3.LocalSessionFactoryBean rather than via hibernate-configuration it should work fine.

 

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" scope="singleton">

<property name="configLocation" value="classpath:orm/hibernate.cfg.xml" />       

                <property name="dataSource" ref="dataSource"/>       

</bean>

 

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">

                <property name="jndiName" value="java:comp/env/jdbc/mysqlDB"/>   

</bean>

 

<?xml version=’1.0′ encoding=’utf-8′?>

<!DOCTYPE hibernate-configuration PUBLIC

        "-//Hibernate/Hibernate Configuration DTD//EN"

        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        ….

        <!–<property name="hibernate.connection.datasource">java:comp/env/jdbc/mysqlDB</property>–>

        …..

    </session-factory>

</hibernate-configuration>

 

Besides, there might be a problem if you mix using anything other than hibernate, such as jdbc api (java.sql.Connection,java.sql.CallableStatement) we can use org.springframework.jdbc.object.StoredProcedure (spring framework managed) instead of jdbc core api to fix this problem

 

Advertisements
This entry was posted in Spring. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s