spring 2.5以后,spring 删除了JotmFactoryBean ,spring不再提供对jotm提供支持
spring atomikos 集成
atomikos需要的jar
atomikos-util-3.9.1.jar
transactions-3.9.1.jar
transactions-api-3.9.1.jar
transactions-jdbc-3.9.1.jar
transactions-jta-3.9.1.jar
db.properties
- datasource.type=oracle
- datasource.driverClassName=oracle.jdbc.driver.OracleDriver
- datasource.url.read=jdbc\:oracle\:thin\:@172.16.20.129\:1521\:read
- datasource.username.read=system
- datasource.password.read=password
- datasource.url.write=jdbc\:oracle\:thin\:@172.16.20.129\:1521\:write
- datasource.username.write=system
- datasource.password.write=password
applicationContext.xml
- <?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:p="http://www.springframework.org/schema/p"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
- xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
- <!-- 数据源定义文件 -->
- <context:property-placeholder location="classpath:db.properties" />
- <!-- 设置数据源属性 -->
- <bean id="dataSourceRead" class="com.atomikos.jdbc.AtomikosDataSourceBean"
- init-method="init" destroy-method="close">
- <property name="uniqueResourceName" value="read_resource" />
- <property name="xaDataSourceClassName" value="com.alibaba.druid.pool.xa.DruidXADataSource" />
- <property name="xaProperties">
- <props>
- <prop key="url">${datasource.url.read}</prop>
- <prop key="username">${datasource.username.read}</prop>
- <prop key="password">${datasource.password.read}</prop>
- </props>
- </property>
- </bean>
- <bean id="jdbcTemplateRead" class="org.springframework.jdbc.core.JdbcTemplate">
- <property name="dataSource" ref="dataSourceRead" />
- </bean>
- <!-- 设置数据源属性 -->
- <bean id="dataSourceWrite" class="com.atomikos.jdbc.AtomikosDataSourceBean"
- init-method="init" destroy-method="close">
- <property name="uniqueResourceName" value="write_resource" />
- <property name="xaDataSourceClassName" value="com.alibaba.druid.pool.xa.DruidXADataSource" />
- <property name="xaProperties">
- <props>
- <prop key="url">${datasource.url.write}</prop>
- <prop key="username">${datasource.username.write}</prop>
- <prop key="password">${datasource.password.write}</prop>
- </props>
- </property>
- </bean>
- <bean id="jdbcTemplateWrite" class="org.springframework.jdbc.core.JdbcTemplate">
- <property name="dataSource" ref="dataSourceWrite" />
- </bean>
- <!-- atomikos事务管理器 -->
- <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
- init-method="init" destroy-method="close">
- <property name="forceShutdown">
- <value>true</value>
- </property>
- </bean>
- <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
- <property name="transactionTimeout" value="300" />
- </bean>
- <!-- spring 事务管理器 -->
- <bean id="jtaTransactionManager"
- class="org.springframework.transaction.jta.JtaTransactionManager">
- <property name="transactionManager" ref="atomikosTransactionManager"/>
- <property name="userTransaction" ref="atomikosUserTransaction" />
- <property name="allowCustomIsolationLevels" value="true"/>
- </bean>
- <!-- 配置事务传播特性 -->
- <tx:advice id="transactionAdvice" transaction-manager="jtaTransactionManager">
- <tx:attributes>
- <tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/>
- <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
- <tx:method name="move*" propagation="REQUIRED" rollback-for="Exception"/>
- <tx:method name="*" propagation="SUPPORTS" read-only="true"/>
- </tx:attributes>
- </tx:advice>
- <!-- 配置相关类的方法参与事务 -->
- <aop:config>
- <aop:advisor pointcut="execution(* service.*.*(..))"
- advice-ref="transactionAdvice" />
- </aop:config>
- <!--spring会按照这个包路径进行扫描 -->
- <context:component-scan base-package="*" />
- <!-- 使用注解方式定义事务 -->
- <tx:annotation-driven transaction-manager="jtaTransactionManager" />
- <!-- 使用AspectJ方式配置AOP -->
- <aop:aspectj-autoproxy proxy-target-class="true" />
- </beans>
transactions.properties
- # SAMPLE PROPERTIES FILE FOR THE TRANSACTION SERVICE
- # THIS FILE ILLUSTRATES THE DIFFERENT SETTINGS FOR THE TRANSACTION MANAGER
- # UNCOMMENT THE ASSIGNMENTS TO OVERRIDE DEFAULT VALUES;
- # Required: factory implementation class of the transaction core.
- # NOTE: there is no default for this, so it MUST be specified!
- #
- com.atomikos.icatch.service=com.atomikos.icatch.standalone.UserTransactionServiceFactory
- # Set base name of file where messages are output
- # (also known as the 'console file').
- #
- # com.atomikos.icatch.console_file_name = tm.out
- # Size limit (in bytes) for the console file;
- # negative means unlimited.
- #
- # com.atomikos.icatch.console_file_limit=-1
- # For size-limited console files, this option
- # specifies a number of rotating files to
- # maintain.
- #
- # com.atomikos.icatch.console_file_count=1
- # Set the number of log writes between checkpoints
- #
- # com.atomikos.icatch.checkpoint_interval=500
- # Set output directory where console file and other files are to be put
- # make sure this directory exists!
- #
- # com.atomikos.icatch.output_dir = ./
- # Set directory of log files; make sure this directory exists!
- #
- # com.atomikos.icatch.log_base_dir = ./
- # Set base name of log file
- # this name will be used as the first part of
- # the system-generated log file name
- #
- # com.atomikos.icatch.log_base_name = tmlog
- # Set the max number of active local transactions
- # or -1 for unlimited.
- #
- # com.atomikos.icatch.max_actives = 50
- # Set the default timeout (in milliseconds) for local transactions
- #
- # com.atomikos.icatch.default_jta_timeout = 10000
- # Set the max timeout (in milliseconds) for local transactions
- #
- # com.atomikos.icatch.max_timeout = 300000
- # The globally unique name of this transaction manager process
- # override this value with a globally unique name
- #
- # com.atomikos.icatch.tm_unique_name = tm
- # Do we want to use parallel subtransactions? JTA's default
- # is NO for J2EE compatibility
- #
- # com.atomikos.icatch.serial_jta_transactions=true
- # If you want to do explicit resource registration then
- # you need to set this value to false.
- #
- # com.atomikos.icatch.automatic_resource_registration=true
- # Set this to WARN, INFO or DEBUG to control the granularity
- # of output to the console file.
- #
- # com.atomikos.icatch.console_log_level=WARN
- # Do you want transaction logging to be enabled or not?
- # If set to false, then no logging overhead will be done
- # at the risk of losing data after restart or crash.
- #
- # com.atomikos.icatch.enable_logging=true
- # Should two-phase commit be done in (multi-)threaded mode or not?
- # Set this to false if you want commits to be ordered according
- # to the order in which resources are added to the transaction.
- #
- # NOTE: threads are reused on JDK 1.5 or higher.
- # For JDK 1.4, thread reuse is enabled as soon as the
- # concurrent backport is in the classpath - see
- # http://mirrors.ibiblio.org/pub/mirrors/maven2/backport-util-concurrent/backport-util-concurrent/
- #
- # com.atomikos.icatch.threaded_2pc=false
- # Should shutdown of the VM trigger shutdown of the transaction core too?
- #
- # com.atomikos.icatch.force_shutdown_on_vm_exit=false
dao
- package dao.impl;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.jdbc.core.JdbcTemplate;
- import org.springframework.stereotype.Repository;
- import bean.User;
- @Repository("userDaoImplRead")
- public class UserDaoImplRead extends UserDaoAbs{
- @Autowired
- JdbcTemplate jdbcTemplateRead;
- //方法
- }
- package dao.impl;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.jdbc.core.JdbcTemplate;
- import org.springframework.stereotype.Repository;
- import bean.User;
- @Repository("userDaoImplWrite")
- public class UserDaoImplWrite extends UserDaoAbs{
- @Autowired
- JdbcTemplate jdbcTemplateWrite;
- //方法
- }
service 就不写了