在容器外定义jta数据源 [英] Defining a jta Datasource outside of the container

查看:84
本文介绍了在容器外定义jta数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的应用程序当前使用的是JBoss standalone.xml中定义的数据源,基本上我们需要在应用程序中定义它,而不是在容器中定义一段时间。我们目前的设置是;

Our application currently uses a datasource which is defined in the JBoss standalone.xml, and basically we need to have this be defined within the app rather than in the container for a while. Our current setup is;

application-context.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:

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:

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文档后,我找不到答案。

to read from datasource.xml rather than go the container (JBoss). It seems like it would be simple but after reading some Spring and Oracle docs I couldn't find an answer.

推荐答案

是的,您可以使用符合JTA的事务管理器,如Atomikos或Bitronix。他们各自的站点都有关于如何使用Spring配置它们的文档。通常,您必须按照下面给出的步骤(如果使用Atomikos):

Yes, you could use a JTA compliant transaction manager like Atomikos or Bitronix. Their respective sites have documentation on how to configure them with Spring. In general, you will have to follow the steps given below (if using Atomikos):


  1. 保留现有的XA数据源( rtsDatasource 在您的情况下)或创建一个(如果尚未使用)(例如,如果某人有非XA数据源,则必须首先将该数据源转换为XA数据源)。

  2. 将XA数据源包装在 AtomikosDataSourceBean 中。

  3. 指向 EntityManagerFactory 在新的 AtomikosDataSourceBean 实例。

  4. 声明XA事务管理器和XA用户事务。

  5. 将XA事务管理器包装在Spring JtaTransactionManager 中。

  6. 使用Spring JtaTransactionManager

  1. Retain your existing XA data source (rtsDatasource in your case) or create one if not already using (for example, if someone has a non-XA data source, that data source must be converted to an XA data source first).
  2. Wrap the XA data source in an AtomikosDataSourceBean.
  3. Point your EntityManagerFactory at the new AtomikosDataSourceBean instance.
  4. Declare an XA transaction manager and an XA user transaction.
  5. Wrap the XA transaction manager in a Spring JtaTransactionManager.
  6. Use the Spring JtaTransactionManager.

使用H2数据库,Hibernate 4,Spring 4的简短配置片段和Atomikos 4如下所示。

A short configuration snippet using H2 database, Hibernate 4, Spring 4 and Atomikos 4 is shown below.

<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"/>

有关详细信息,您可以看到这个应用程序

For details, you can see this app.

这篇关于在容器外定义jta数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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