Defining a jta Datasource outside of the container

后端 未结 1 1362
闹比i
闹比i 2021-01-16 09:04

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 con

相关标签:
1条回答
  • 2021-01-16 09:42

    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. 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.

    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.

    0 讨论(0)
提交回复
热议问题