我们的应用程序当前使用的数据源是在JBoss standalone.xml中定义的,基本上我们需要在应用程序而不是容器中定义它。我们当前的设置是;
application-context.xml;
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" /> <property name="persistenceUnitName" value="rtsPersistenceUnit" /> <property name="packagesToScan"> ... </property> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="database" value="ORACLE" /> <property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect"/> </bean> </property> </bean>
persistance.xml:
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> <persistence-unit name="rtsPersistenceUnit" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <jta-data-source>java:/jdbc/RTSdb</jta-data-source> <class>...</class> <exclude-unlisted-classes>true</exclude-unlisted-classes> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" /> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.hbm2ddl.auto" value="validate" /> <property name="hibernate.transaction.flush_before_completion" value="true" /> </properties> </persistence-unit> </persistence>
datasource.xml:
<bean id="rtsDatasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="oracle.jdbc.OracleDriver"/> <property name="url" value="..."/> <property name="username" value="..."/> <property name="password" value="..."/> </bean>
基本上我想要的只是生产线
<jta-data-source>java:/jdbc/RTSdb</jta-data-source>
从datasource.xml读取而不是转到容器(JBoss)。看起来似乎很简单,但是在阅读了一些Spring和Oracle文档之后,我找不到答案。
是的,您可以使用符合JTA的事务管理器,例如Atomikos或Bitronix。他们各自的站点都有关于如何使用Spring进行配置的文档。通常,您必须遵循以下步骤(如果使用Atomikos):
rtsDatasource
AtomikosDataSourceBean
EntityManagerFactory
JtaTransactionManager
下面显示了一个使用H2数据库,Hibernate 4,Spring 4和Atomikos 4的简短配置片段。
<bean class="org.h2.jdbcx.JdbcDataSource" id="originalDataStore" lazy-init="true">...</bean> <bean class="com.atomikos.jdbc.AtomikosDataSourceBean" id="dataSource" init-method="init" destroy-method="close"> <property name="uniqueResourceName" value="xaDS"/> <property name="xaDataSource" ref="originalDataStore"/> <property name="poolSize" value="3"/> </bean> <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory"> <property name="dataSource" ref="dataSource"/> <property name="jpaProperties"> <props> <prop key="hibernate.transaction.jta.platform">com.atomikos.icatch.jta.hibernate4.AtomikosPlatform</prop> ... </props> </property> </bean> <bean class="org.springframework.transaction.jta.JtaTransactionManager" id="transactionManager"> <property name="transactionManager"> <bean class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close"> <property name="forceShutdown" value="false"/> </bean> </property> <property name="userTransaction"> <bean class="com.atomikos.icatch.jta.J2eeUserTransaction"> <property name="transactionTimeout" value="300"/> </bean> </property> <property name="allowCustomIsolationLevels" value="true"/> </bean> <transaction:annotation-driven transaction-manager="transactionManager"/>
有关详细信息,您可以查看此应用程序。