2
votes

I created simple Java application to learn Spring and hibernate integration. I have done all the setup and application also working fine.

Everything works fine. But, I want to see how much sessions are currently my application using like open connections and closed hibernate connections.

In Sessionfactory classs we have "getStatistics" method used to retrieve hibernate statistics data but this is not helping me. It gives me also Zero. please find below pic.

enter image description here

I want to ensure my application, using single session for entire application operations or not.

So is there any way to find out or to get Spring+hibernate sessions count.

My hibernate configuration file;

<?xml version="1.0" encoding="UTF-8"?>

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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
    <property name="url" value="jdbc:oracle:thin:@localhost:1521:XE" />
    <property name="username" value="rocky" />
    <property name="password" value="rocky" />
</bean>

<bean id="mySessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource" />
    <property name="mappingResources">
        <list>
            <value>orders.hbm.xml</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <map>
            <entry>
                <key>
                    <value>hibernate.show_sql</value>
                </key>
                <value>true</value>
            </entry>
            <entry>
                <key>
                    <value>hibernate.dialect</value>
                </key>
                <value>org.hibernate.dialect.OracleDialect</value>
            </entry>
            <entry>
                <key>
                    <value>hibernate.query.factory_class</value>
                </key>
                <value>org.hibernate.hql.classic.ClassicQueryTranslatorFactory</value>
            </entry>
            <entry>
                <key>
                    <value>connection.autocommit</value>
                </key>
                <value>true</value>
            </entry>
        </map>
    </property>
</bean>

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

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

<bean id="transactionProxy"
    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager" ref="txManager" />
    <property name="transactionAttributes">
        <props>
            <prop key="*">PROPAGATION_REQUIRED</prop>
        </props>
    </property>
    <property name="target" ref="myServiceClass">
    </property>
</bean>

<tx:annotation-driven transaction-manager="txManager" />

<bean id="myServiceClass" class="myServiceClass">
    <property name="sessionFactory" ref="mySessionFactory" />
</bean>

1

1 Answers

0
votes

Looking at your code It seams that you have not enable staticstics of your session factory

Step to unable session factory statics

SessionFactory sessionFactory = getSessionFactoryForApplication();
Statistics stats = sessionFactory.getStatistics();
stats.setStatisticsEnabled(true);

Taken from this article

hope this will solve your problem..!