0

I am in the process of learning Hibernate, Spring and JPA.

I would like to know whether my database connections are closed automatically by Spring or not. When I looked at database table v$session I can find there are four JDBC Thin Client sessions. So would like to know whether these connections are from my application.

Below is my applicationContext.xml. Any help is highly appreciable.

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
>   
    <context:component-scan base-package="net.test" />
    <!-- Data Source Declaration -->    
    <bean id="DataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="jdbc/myDS"/>     
</bean>
    <bean
        class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
    <bean class="org.springframework.orm.hibernate4.HibernateExceptionTranslator" />
    <!-- JPA Entity Manager Factory -->
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="DataSource" />
        <property name="packagesToScan" value="net.test.entity" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="true" />
                <property name="generateDdl" value="false" />
                <property name="databasePlatform" value="${jdbc.dialectClass}" />
            </bean>
        </property>
    </bean>
    <bean id="defaultLobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler" />
    <!-- Session Factory Declaration -->
    <bean id="SessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="DataSource" />
        <property name="annotatedClasses">
            <list>
                <value>net.test.entity.Employee</value>
                <value>net.test.entity.Department</value>

            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory
                </prop>
            </props>
        </property>
    </bean> 
    <tx:annotation-driven transaction-manager="txManager" />
    <tx:annotation-driven transaction-manager="transactionManager" />   
    <bean id="txManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="SessionFactory" />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    <!-- <tx:annotation-driven transaction-manager="txManager"/> -->
    <context:annotation-config />
    <bean id="hibernateStatisticsMBean" class="org.hibernate.jmx.StatisticsService">
        <property name="statisticsEnabled" value="true" />
        <property name="sessionFactory" value="#{entityManagerFactory.sessionFactory}" />
    </bean>
    <bean name="ehCacheManagerMBean"
        class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" />
    <bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean">
        <property name="locateExistingServerIfPossible" value="true" />
    </bean>
    <bean id="jmxExporter" class="org.springframework.jmx.export.MBeanExporter"
        lazy-init="false">
        <property name="server" ref="mbeanServer" />
        <property name="registrationBehaviorName" value="REGISTRATION_REPLACE_EXISTING" />
        <property name="beans">
            <map>
                <entry key="SpringBeans:name=hibernateStatisticsMBean"
                    value-ref="hibernateStatisticsMBean" />
                <entry key="SpringBeans:name=ehCacheManagerMBean" value-ref="ehCacheManagerMBean" />
            </map>
        </property>
    </bean>
</beans>
DataNucleus
  • 15,497
  • 3
  • 32
  • 37
Jacob
  • 14,463
  • 65
  • 207
  • 320

1 Answers1

1

Spring closes connections when the transaction is committed of rollbacked. But the connections are pooled connections, so closing them simply puts them back into the pool of connections, and doen't physically closes them.

The first goal is to be able to get a new connection from the pool extremely quickly, without having to recreate a new physical connection each time, because it's a costly operation.

The other goal is to be able to put a limit on the number of opened connections, to avoid bringing the database to its knees.

Note that the pool of connections, in your case, isn't handled by Spring, but by your application server, which makes it available from JNDI.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thanks for your comments. Is it better to handle the connections by Spring or Apps Server? If it is by Spring how can I do that? In my small application I do have a few database functions which I am calling from application and these functions return sys_refcursor. So in these cases how does Spring handle these? Thanks – Jacob Feb 22 '13 at 18:01
  • 1
    If you need the connections to participate to distributed transactions (XA), then use the app server. Otherwise, it doesn't matter, but you'd have to include a connection pool like C3P0 to do it with Spring. So it's redundant with the pool already provided by the appserver. Regarding your database function, I assume you're talking about stored procedures. You'll have to use JDBC to call them, but I have no experience with them. – JB Nizet Feb 22 '13 at 18:04
  • As you have mentioned Spring closes connection when transaction is closed what will happen when I am calling a Stored procedure or database function, does Spring closes these connections or sessions? – Jacob Feb 22 '13 at 18:44
  • As long as you use DataSOurceUtils to get a connection, everything should be fine. See http://stackoverflow.com/questions/6777419/how-to-configure-spring-to-make-jpa-hibernate-and-jdbc-jdbctemplate-or-mybati – JB Nizet Feb 22 '13 at 22:34