engma
engma

Reputation: 1959

Forcing hibernate to release connection on session Spring MVC

I am using Spring MVC 3 and Hibernate 3.6, I use xml configured transaction management, my code works greate but my JDBC are not being released although it says it does.

I checked it with JProfiler and it says the connection is open.

this is my spring-config code

<bean id="ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/parse_web?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
    <property name="username" value="root" />
    <property name="password" value="miles106" />
    <property name="initialSize" value="5"/>
    <property name="maxActive" value="50000"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="ds" />
    <property name="mappingResources">
        <list>
            <value>com/mubasher/parsewebpage/entities/Changes.hbm.xml</value>
            <value>com/mubasher/parsewebpage/entities/Owners.hbm.xml</value>
            <value>com/mubasher/parsewebpage/entities/Ownerships.hbm.xml</value>
            <value>com/mubasher/parsewebpage/entities/TargetCompanies.hbm.xml</value>
            <value>com/mubasher/parsewebpage/entities/TempData.hbm.xml</value>
            <value>com/mubasher/parsewebpage/entities/Exceptions.hbm.xml</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="dialect">org.hibernate.dialect.MySQL5Dialect</prop>
            <prop key="hibernate.connection.useUnicode">true</prop>
            <prop key="hibernate.connection.characterEncoding">UTF-8</prop>
            <prop key="hibernate.connection.charSet">UTF-8</prop>
            <prop key="hibernate.connection.release_mode">after_statement</prop>
        </props>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

and this is my debug code

DEBUG [myExec-2] (JDBCTransaction.java:223) - re-enabling autocommit DEBUG [myExec-2] (JDBCTransaction.java:143) - committed JDBC Connection DEBUG [myExec-2] (ConnectionManager.java:427) - aggressively releasing JDBC connection DEBUG [myExec-2] (ConnectionManager.java:464) - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)] DEBUG [myExec-2] (HibernateTransactionManager.java:734) - Closing Hibernate Session [org.hibernate.impl.SessionImpl@52ab7af2] after transaction DEBUG [myExec-2] (SessionFactoryUtils.java:789) - Closing Hibernate Session

but in JProfiler i can see that the connection is still open as you see

enter image description here

this is realy causing me problems, my application is doing massive database work, so I need the connection to close as soon as the work is done, should I use maxIdle ?

Upvotes: 1

Views: 9156

Answers (1)

Pavel Horal
Pavel Horal

Reputation: 18194

Connections are not closed, they are reused. This is the whole purpose of commons-dbcp, which stands for Database Connection Pool.

Establishing a new connection is usually an expensive operation. So what DBCP is doing is that instead of closing connection, it leaves it open and returns it to the connection pool for another use.

If you want your database connections to get closed and re-opened with each request, then you need to use a different data source (e.g. org.springframework.jdbc.datasource.SimpleDriverDataSource).


UPDATE 1: Also note, that in your example you are setting maximum number of parallel connections (maxActive) to be 50000. That is some extreme number (default is 8!!!), which IMO can cause a lot of problems.

UPDATE 2: Using maxIdle is a good idea if you don't wan't to get rid of the pool. But that will not save you from "having non-closed connections". If you are thinking about setting maxIdle=0, then drop the pool completely.

UPDAET 3: I just need to stress out this one again - If you need 50000 parallel connections, then there is really something wrong with your code.

Upvotes: 5

Related Questions