Hibernate Tip: 使用JBOSS MBEAN时,Session会在事务提交后自动关闭!

2024-04-19 22:58

本文主要是介绍Hibernate Tip: 使用JBOSS MBEAN时,Session会在事务提交后自动关闭!,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

做了一个hibernate app,相关代码如下:

-------------------------------------
VoteServiceImpl.class
public class VoteServiceImpl{
 Session session;
 。。。。
 public void closeSession(){ session.close();}

 public void deleteVote(int voteId) {
      Transaction tx = session.beginTransaction();
      session.delete(getVote(voteId));
      tx.commit();
 }

  public VoteQuestion getVote(int voteId) {
        return (VoteQuestion) session.load(VoteQuestion.class, new Long(voteId));
  }
}
 
调用代码为:
VoteServiceImpl vs=new VoteServiceImpl(session);
vs.deleteVote(1);
vs.getVote(2);

vs.closeSession();
-------------------------------------

junit测试调用上面代码没问题,junit里创建sessionFactory的方式是:
     sessionFactory = new Configuration().configure().buildSessionFactory();

在JBOSS里,原来我使用的是把hibnerate pojo打包成har,并通过hibernate-service.xml来配置hibernate SessionFactory MBean

hibernate-service.xml content
-------------------------------------
<server>
    <mbean code="org.jboss.hibernate.jmx.Hibernate"
           name="jboss.har:service=ETokenHibernate">

        <attribute name="DatasourceName">java:/ETokenDB</attribute>
        <attribute name="Dialect">org.hibernate.dialect.MySQLDialect</attribute>
        <attribute name="SessionFactoryName">java:/hibernate/ETokenSessionFactory</attribute>
        <attribute name="CacheProviderClass">org.hibernate.cache.HashtableCacheProvider</attribute>
       
        <!-- <attribute name="Hbm2ddlAuto">create-drop</attribute> -->
    </mbean>
</server>
-------------------------------------

这种情况下调用上面的代码,就会抛出exception。抛出exception的是在调用
             vs.getVote(2);
这一行时。exception大概的意思就是session已经关闭,无法操作

为什么会出现这种情况:在junit里可以,而在jboss里出错??

原因就在于通过上述配置的JBOSS hibernate SessionFactory MBean的缺省配置居然是设置Session会在事务提交后自动关闭(这和hibernate调用new Configuration().configure().buildSessionFactory()的缺省配置完全相反)。因此当调用vs.deleteVote时,由于tx.commit提交了事务,所以session自动关闭,无法再执行vs.getVote。


怎么解决???

本来想着在上面的hibernate-service.xml里设置session auto close属性为false,但
    <mbean code="org.jboss.hibernate.jmx.Hibernate"
           name="jboss.har:service=ETokenHibernate">

并没有提供这个属性设置

上网查了很久,终于找到解决方案:用jboss-service.xml代替hibernate-service.xml,并在jboss-service.xml里配置下面的hibernate mbean class可能在hibernate-service.xml里使用,但太懒,没试)即可!

jboss-service.xml content
------------------------------------------------
<server>

<mbean code="org.hibernate.jmx.HibernateService"
    name="jboss.jca:service=HibernateFactory,name=HibernateFactory
">

    <!-- 必须的服务 -->
    <depends>jboss.jca:service=RARDeployer</depends>
    <depends>jboss.jca:service=LocalTxCM,name=HsqlDS</depends>

    <!-- 将Hibernate服务绑定到JNDI -->
     <attribute name="JndiName">java:/hibernate/ETokenSessionFactory</attribute> 


    <!-- 数据源设置 -->
    <attribute name="Datasource">java:HsqlDS</attribute>
    <attribute name="Dialect">org.hibernate.dialect.HSQLDialect</attribute>

    <!-- 事务集成 -->
    <attribute name="TransactionStrategy">
        org.hibernate.transaction.JTATransactionFactory</attribute>
    <attribute name="TransactionManagerLookupStrategy">
        org.hibernate.transaction.JBossTransactionManagerLookup</attribute>
    <attribute name="FlushBeforeCompletionEnabled">true</attribute>
    <attribute name="AutoCloseSessionEnabled">false</attribute>

    <!-- 抓取选项 -->
    <attribute name="MaximumFetchDepth">5</attribute>

    <!-- 二级缓存 -->
    <attribute name="SecondLevelCacheEnabled">true</attribute>
    <attribute name="CacheProviderClass">org.hibernate.cache.EhCacheProvider</attribute>
    <attribute name="QueryCacheEnabled">true</attribute>

    <!-- 日志 -->
    <attribute name="ShowSqlEnabled">true</attribute>

    <!-- 映射定义文件 -->
    <attribute name="MapResources">auction/Item.hbm.xml,auction/Category.hbm.xml</attribute>

</mbean>

</server>

------------------------------------------------

比较新旧两个mbean,使用的class是不同的:org.hibernate.jmx.HibernateService可以设置AutoCloseSessionEnabled属性和其他更多的hibernate,而旧的org.jboss.hibernate.jmx.Hibernate不行,这就是关键点

!!注意:使用org.hibernate.jmx.HibernateService mbean必须设置MapResources属性,该属性值是所有hibernate pojo hbm.xml的list,hbm xml之间用逗号隔开。


另外面的jboss-service.xml代码在运行时出错:cache方面的错,应该是缺少某个jar,没空研究了,就使用了简化的配置,见下面:
jboss-service.xml content
------------------------------------------------
<server>
 <mbean code="org.hibernate.jmx.HibernateService"
     name="jboss.jca:service=HibernateFactory,name=HibernateFactory">
 

     <attribute name="JndiName">java:/hibernate/ETokenSessionFactory</attribute> 
     <attribute name="Datasource">java:/ETokenDB</attribute>
     <attribute name="Dialect">org.hibernate.dialect.MySQLDialect</attribute> 
     <attribute name="AutoCloseSessionEnabled">false</attribute>

     <attribute name="MapResources">pojo/JQuizTemplate.hbm.xml,....,pojo/Vote.hbm.xml</attribute>    
 </mbean>
</server>
------------------------------------------------

 


 

这篇关于Hibernate Tip: 使用JBOSS MBEAN时,Session会在事务提交后自动关闭!的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/918666

相关文章

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

MySQL中EXISTS与IN用法使用与对比分析

《MySQL中EXISTS与IN用法使用与对比分析》在MySQL中,EXISTS和IN都用于子查询中根据另一个查询的结果来过滤主查询的记录,本文将基于工作原理、效率和应用场景进行全面对比... 目录一、基本用法详解1. IN 运算符2. EXISTS 运算符二、EXISTS 与 IN 的选择策略三、性能对比

使用Python构建智能BAT文件生成器的完美解决方案

《使用Python构建智能BAT文件生成器的完美解决方案》这篇文章主要为大家详细介绍了如何使用wxPython构建一个智能的BAT文件生成器,它不仅能够为Python脚本生成启动脚本,还提供了完整的文... 目录引言运行效果图项目背景与需求分析核心需求技术选型核心功能实现1. 数据库设计2. 界面布局设计3

SQL Server跟踪自动统计信息更新实战指南

《SQLServer跟踪自动统计信息更新实战指南》本文详解SQLServer自动统计信息更新的跟踪方法,推荐使用扩展事件实时捕获更新操作及详细信息,同时结合系统视图快速检查统计信息状态,重点强调修... 目录SQL Server 如何跟踪自动统计信息更新:深入解析与实战指南 核心跟踪方法1️⃣ 利用系统目录

使用IDEA部署Docker应用指南分享

《使用IDEA部署Docker应用指南分享》本文介绍了使用IDEA部署Docker应用的四步流程:创建Dockerfile、配置IDEADocker连接、设置运行调试环境、构建运行镜像,并强调需准备本... 目录一、创建 dockerfile 配置文件二、配置 IDEA 的 Docker 连接三、配置 Do

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

python使用try函数详解

《python使用try函数详解》Pythontry语句用于异常处理,支持捕获特定/多种异常、else/final子句确保资源释放,结合with语句自动清理,可自定义异常及嵌套结构,灵活应对错误场景... 目录try 函数的基本语法捕获特定异常捕获多个异常使用 else 子句使用 finally 子句捕获所

C++11右值引用与Lambda表达式的使用

《C++11右值引用与Lambda表达式的使用》C++11引入右值引用,实现移动语义提升性能,支持资源转移与完美转发;同时引入Lambda表达式,简化匿名函数定义,通过捕获列表和参数列表灵活处理变量... 目录C++11新特性右值引用和移动语义左值 / 右值常见的左值和右值移动语义移动构造函数移动复制运算符

Python对接支付宝支付之使用AliPay实现的详细操作指南

《Python对接支付宝支付之使用AliPay实现的详细操作指南》支付宝没有提供PythonSDK,但是强大的github就有提供python-alipay-sdk,封装里很多复杂操作,使用这个我们就... 目录一、引言二、准备工作2.1 支付宝开放平台入驻与应用创建2.2 密钥生成与配置2.3 安装ali

Spring Security 单点登录与自动登录机制的实现原理

《SpringSecurity单点登录与自动登录机制的实现原理》本文探讨SpringSecurity实现单点登录(SSO)与自动登录机制,涵盖JWT跨系统认证、RememberMe持久化Token... 目录一、核心概念解析1.1 单点登录(SSO)1.2 自动登录(Remember Me)二、代码分析三、