简体   繁体   English

从应用程序获取休眠会话计数

[英]Getting Hibernate Session Count from application

I created simple Java application to learn Spring and hibernate integration. 我创建了简单的Java应用程序来学习Spring和休眠集成。 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. Sessionfactory类中,我们有“ getStatistics”方法用于检索休眠统计数据,但这对我没有帮助。 It gives me also Zero. 它也给我零。 please find below pic. 请在下面的图片中找到。

在此处输入图片说明

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. 因此,有什么方法可以找出或获取Spring + hibernate会话计数。

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 "> 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>

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..! 希望这能解决您的问题..!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM