我正在尝试创建事务管理器,并将其与Hibernate for Oracle一起使用。
我的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> <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>
在spring的applicationContext.xml中,我添加了:
<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"/>
但是,当我运行时:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("org.drools.persistence.jpa");
我有一个例外:
Caused by: org.hibernate.HibernateException: Could not find datasource: jdbc/testDS1
ds = (DataSource ) NamingHelper.getInitialContext(props).lookup(jndiName);Hibernate基础文件文件除外。
ds = (DataSource ) NamingHelper.getInitialContext(props).lookup(jndiName);
可能是什么问题呢?
Hibernate持久性如何知道引用Spring txManagerbean?
txManager
持久性提供程序在jndi上进行查找。在Spring应用程序上下文中定义的数据源未绑定到jndi。因此,持久性提供程序对数据源的查找尝试失败,因为没有绑定到jndi的数据源。
您可能要检查http://forum.springsource.org/showthread.php?t=13984。
您可以尝试在服务器上下文中定义数据源,并在其Spring应用程序中按其jndi名称查找它们吗?