Bitronix + Spring + Hibernate +持久性 [英] Bitronix + Spring + Hibernate + Persistence

查看:166
本文介绍了Bitronix + Spring + Hibernate +持久性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的persistence.xml文件是:

 < persistence-unit name =org.drools.persistence.jpa
transaction-type =JTA>
< provider> org.hibernate.ejb.HibernatePersistence< / provider>
< jta-data-source> jdbc / testDS1< / jta-data-source>
< class> org.drools.persistence.session.SessionInfo< / class>
< class> org.jbpm.persistence.processinstance.ProcessInstanceInfo< / class>
< class> org.drools.persistence.processinstance.WorkItemInfo< / class>

< exclude-unlisted-classes> true< / exclude-unlisted-classes>
<属性>
< property name =hibernate.dialectvalue =org.hibernate.dialect.Oracle10gDialect/>
< property name =hibernate.connection.autocommitvalue =false/>
< property name =hibernate.max_fetch_depthvalue =3/>
< property name =hibernate.jndi.classvalue =bitronix.tm.jndi.BitronixInitialContextFactory/>
< property name =hibernate.show_sqlvalue =true/>
< property name =hibernate.transaction.manager_lookup_class
value =org.hibernate.transaction.BTMTransactionManagerLookup/>
< / properties>
< / persistence-unit>

在spring的applicationContext.xml中,我添加了:

 < bean id =dataSourceclass =bitronix.tm.resource.jdbc.PoolingDataSourceinit-method =initdestroy-method =close> 
< property name =classNamevalue =oracle.jdbc.xa.client.OracleXADataSource/>
< property name =uniqueNamevalue =jdbc / testDS1/>
< property name =minPoolSizevalue =1/>
< property name =maxPoolSizevalue =5/>
< property name =driverProperties>
<道具>
< prop key =URL> myURL< / prop>
< prop key =user>用户名< / prop>
< prop key =password>密码< / prop>
< /道具>
< / property>
< / bean>

< bean id =txManagerclass =org.springframework.transaction.jta.JtaTransactionManager>
< property name =transactionManagerref =bitronixTransactionManager/>
< property name =userTransactionref =bitronixTransactionManager/>
< / bean>

< bean id =bitronixTransactionManagerfactory-method =getTransactionManager
class =bitronix.tm.TransactionManagerServicesdepends-on =dataSource,txManager
destroy -method = 关机/>

然而,当我运行时:

  EntityManagerFactory emf = Persistence.createEntityManagerFactory(org.drools.persistence.jpa); 

我得到一个异常:



<$ p $由于:org.hibernate.HibernateException:找不到数据源:jdbc / testDS1

Hibernate infra文件中的 ds =(DataSource)NamingHelper.getInitialContext(props).lookup(jndiName); 异常。


  1. 有什么问题?

  2. spring txManager bean?
  3. >

    您的持久性提供者在jndi上进行查找。 Spring应用程序上下文中定义的数据源不绑定到jndi。因此,持久性提供者对数据源的查找尝试失败,因为没有绑定到jndi的这样的数据源。



    您可能需要检查 http://forum.springsource.org/showthread.php?t=13984



    你可以尝试在服务器上下文中定义你的数据源,并用他们的jndi名字在你的spring应用程序中查找它们吗?

    I am trying to create transaction manager and use it with Hibernate for Oracle.

    My persistence.xml file is:

    <persistence-unit name="org.drools.persistence.jpa"
            transaction-type="JTA">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <jta-data-source>jdbc/testDS1</jta-data-source>
            <class>org.drools.persistence.session.SessionInfo</class>
            <class>org.jbpm.persistence.processinstance.ProcessInstanceInfo</class>
            <class>org.drools.persistence.processinstance.WorkItemInfo</class>
    
            <exclude-unlisted-classes>true</exclude-unlisted-classes>
            <properties>
                <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
                <property name="hibernate.connection.autocommit" value="false" />
                <property name="hibernate.max_fetch_depth" value="3" />
                <property name="hibernate.jndi.class" value="bitronix.tm.jndi.BitronixInitialContextFactory"/> 
                <property name="hibernate.show_sql" value="true" />
                <property name="hibernate.transaction.manager_lookup_class"
                    value="org.hibernate.transaction.BTMTransactionManagerLookup" />
            </properties>
        </persistence-unit>
    

    In applicationContext.xml of spring I added:

    <bean id="dataSource" class="bitronix.tm.resource.jdbc.PoolingDataSource" init-method="init" destroy-method="close"> 
               <property name="className" value="oracle.jdbc.xa.client.OracleXADataSource" /> 
               <property name="uniqueName" value="jdbc/testDS1" /> 
               <property name="minPoolSize" value="1" /> 
               <property name="maxPoolSize" value="5" /> 
               <property name="driverProperties">
                <props>
                    <prop key="URL">myURL</prop>
                    <prop key="user">username</prop>
                    <prop key="password">password</prop>
                </props>
            </property>       
        </bean> 
    
        <bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager"> 
            <property name="transactionManager" ref="bitronixTransactionManager"/> 
            <property name="userTransaction" ref="bitronixTransactionManager"/> 
        </bean> 
    
        <bean id="bitronixTransactionManager" factory-method="getTransactionManager" 
              class="bitronix.tm.TransactionManagerServices" depends-on="dataSource,txManager" 
              destroy-method="shutdown"/>
    

    However, when I run:

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("org.drools.persistence.jpa");
    

    I get an exception:

    Caused by: org.hibernate.HibernateException: Could not find datasource: jdbc/testDS1
    

    The exception is on ds = (DataSource ) NamingHelper.getInitialContext(props).lookup(jndiName); of Hibernate infra file.

    1. What could be the problem?

    2. How does Hibernate persistence knows to refer to spring txManager bean?

    解决方案

    Your persistence provider does its lookups on jndi. Data sources defined in Spring application context are not bound to jndi. Hence, persistence provider's lookup attempt for the data source fails as there is no such data source bound to jndi.

    You may want to check http://forum.springsource.org/showthread.php?t=13984.

    Can you try defining your data sources in the server context and looking them up in your spring application by their jndi names?

    这篇关于Bitronix + Spring + Hibernate +持久性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆