java分布式事务

1.现有方案

a.atomikos

b.jotm

说明:spring3.0已将jotm的支持踢掉

2.使用atomikos时的pom.xml内容

<!-- 分布式事务支持-atomikos-begin -->
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.1</version>
        </dependency>                    
        <dependency>
            <groupId>com.atomikos</groupId>
            <artifactId>transactions</artifactId>
            <version>3.9.1</version>
        </dependency>
        <dependency>
            <groupId>com.atomikos</groupId>
            <artifactId>transactions-api</artifactId>
            <version>3.9.1</version>
        </dependency>
        <dependency>
            <groupId>com.atomikos</groupId>
            <artifactId>transactions-jta</artifactId>
            <version>3.9.1</version>
        </dependency>
        <dependency>
            <groupId>com.atomikos</groupId>
            <artifactId>transactions-jdbc</artifactId>
            <version>3.9.1</version>
        </dependency>
        <dependency>
            <groupId>com.atomikos</groupId>
            <artifactId>atomikos-util</artifactId>
            <version>3.9.1</version>
        </dependency>                                                                    
        <!-- 分布式事务支持-atomikos-end --> 

3.使用atomikos时不能使用不支持jta的连接池如c3p0

4.spring配制如下:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
        <property name="location" value="classpath:db.properties"/> 
    </bean>    
    <bean id="dataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close"  lazy-init="true">
         <property name="uniqueResourceName" value="dataSource" />
        <property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
        <property name="xaProperties">
            <props>   
                <prop key="url">${db.url}</prop>
                <prop key="user">${db.username}</prop>
                <prop key="password">${db.password}</prop>
           </props>
        </property>
        <property name="poolSize" value="10"/>
        <property name="maxPoolSize" value="100"/>
    </bean>    
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
    </bean>
        
    <bean id="userTransactionService" class="com.atomikos.icatch.config.UserTransactionServiceImp" init-method="init" destroy-method="shutdownForce">
        <constructor-arg>
            <props>
                <prop key="com.atomikos.icatch.service">com.atomikos.icatch.standalone.UserTransactionServiceFactory</prop>
            </props>
        </constructor-arg>
    </bean>

    <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close" depends-on="userTransactionService">
        <property name="forceShutdown" value="false" />
    </bean>

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

    <bean id="jtaTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" depends-on="userTransactionService">
        <property name="transactionManager" ref="atomikosTransactionManager" />
        <property name="userTransaction" ref="atomikosUserTransaction" />
    </bean>

    <tx:annotation-driven transaction-manager="jtaTransactionManager" />

5.service里采用标签来实现事务如下:

@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
public void TestTransaction() throws Exception{
}

 6.特别说明:

有人提出:使用spring的代理datasource来让spring管理事务,性能问题解决,提高8倍,见下面的连接

http://blog.sina.com.cn/s/blog_53765cf9010176zx.html

我还没有证实,找时间来看一下

你可能感兴趣的:(分布式事务)