spring data jpa+eclipselink+jta+atomikos的配置(全网独一无二的研究结果)

前后两天花了7个多小时,搜索了整个网络的心血研究成果,基于hibernate的很好找,但基于eclipselink的资料少之又少,我最终解决了也是看了大量资料的灵机一动,废话不说了,也不想做解释了,明白人一看就明白。我可以肯定地说,到今天为止,网上还没有一个可用的配置文件,我这是蝎子耙耙独一份儿,哈哈。参照下面的例子,你也会解决的。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">

    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!--<jee:jndi-lookup jndi-name="test" id="dataSource" resource-ref="true"/>-->

    <bean abstract="true" id="baseDS" class="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" lazy-init="true">
        <property name="pinGlobalTxToPhysicalConnection" value="true" />
        <property name="user" value="root" />
        <property name="password" value="root" />
    </bean>

    <bean id="dataSource1" parent="baseDS">
        <property name="url">
            <value>${jdbc1.url}</value>
        </property>
    </bean>

    <bean id="dataSource2" parent="baseDS">
        <property name="url">
            <value>${jdbc2.url}</value>
        </property>
    </bean>

    <bean id="baseJTADS" class="com.atomikos.jdbc.AtomikosDataSourceBean" abstract="true" init-method="init" destroy-method="close">
        <property name="poolSize" value="3" />
    </bean>

    <bean id="jtaDs1" parent="baseJTADS">
        <property name="uniqueResourceName" value="XA1DBMS" />
        <property name="xaDataSource" ref="dataSource1" />
    </bean>

    <bean id="jtaDs2" parent="baseJTADS">
        <property name="uniqueResourceName" value="XA2DBMS" />
        <property name="xaDataSource" ref="dataSource2" />
    </bean>

    <bean abstract="true" id="baseEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter"/>
        </property>
        <property name="packagesToScan" value="org.fxbird.springjpa.model"/>
        <property name="jpaProperties">
            <props>
                <prop key="databasePlatform">org.eclipse.persistence.platform.database.MySQLPlatform</prop>
                <prop key="eclipselink.jdbc.cache-statements">true</prop>
                <prop key="eclipselink.weaving">false</prop>
                <prop key="eclipselink.logging.level">FINEST</prop>
                <prop key="eclipselink.allow-zero-id">true</prop>
                <prop key="eclipselink.target-server">com.atomikos.eclipselink.platform.AtomikosTransactionController</prop>
                <prop key="eclipselink.external-transaction-controller">true</prop>
            </props>
        </property>
    </bean>

    <bean id="emfCompany" parent="baseEmf">
        <property name="jtaDataSource" ref="jtaDs1"/>
        <property name="persistenceUnitName" value="emfC"/>
    </bean>

    <bean id="emfEmp" parent="baseEmf">
        <!--<property name="persistenceUnitName" value="employee" />-->
        <property name="jtaDataSource" ref="jtaDs2"/>
        <property name="persistenceUnitName" value="emfE"/>
    </bean>

    <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
        <property name="transactionTimeout" value="300"/>
    </bean>

    <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init"
          destroy-method="close">
        <property name="forceShutdown" value="true"/>
        <property name="transactionTimeout" value="300"/>
    </bean>

    <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
        <property name="transactionManager" ref="atomikosTransactionManager"/>
        <property name="userTransaction" ref="atomikosUserTransaction"/>
        <!--<property name="allowCustomIsolationLevels" value="true"/>-->
    </bean>

    <context:component-scan base-package="org.fxbird.springjpa.service"/>

    <jpa:repositories base-package="org.fxbird.springjpa.repo.company"
                      entity-manager-factory-ref="emfCompany" transaction-manager-ref="transactionManager">
    </jpa:repositories>

    <jpa:repositories base-package="org.fxbird.springjpa.repo.emp"
                      entity-manager-factory-ref="emfEmp" transaction-manager-ref="transactionManager">
    </jpa:repositories>

    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />

    <aop:aspectj-autoproxy proxy-target-class="true" />

    <context:annotation-config/>

</beans>

猜你喜欢

转载自theoffspring.iteye.com/blog/2201722