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

查看:29
本文介绍了在容器外定义一个 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>

持久性.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>

数据源.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>

基本上我想要的就是这条线

Basically all I want is for the line

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

详情可以查看这个应用.

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

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