转贴:JBOSS的hibernate-service配置


Hibernate Tip: 使用JBOSS MBEAN时,Session会在事务提交后自动关闭!   文章指数:0  CSDN Blog推出文章指数概念,文章指数是对Blog文章综合评分后推算出的,综合评分项分别是该文章的点击量,回复次数,被网摘收录数量,文章长度和文章类型;满分100,每月更新一次。

做了一个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,xml,jboss,cache,JUnit)