1.手动配置事务的方式是
applicationContext.xml
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring"></property> <property name="user" value="root"></property> <property name="password" value="123456"></property> </bean> <bean id="accountDao" class="com.test.spring.aop.tansation.example.dao.AccountDaoImpl"> <property name="dataSource" ref="dataSource"></property> </bean> <bean id="accountService" class="com.test.spring.aop.tansation.example.service.AccountServiceImpl"> <property name="accountDao" ref="accountDao"></property> <property name="transactionTemplate" ref="transationTemplate"></property> </bean> <!-- 手动方式 --> <bean id="transationTemplate" class="org.springframework.transaction.support.TransactionTemplate"> <property name="transactionManager" ref="txManage"></property> </bean> <bean id="txManage" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> </beans>
dao层(接口类这里不写)
package com.test.spring.aop.tansation.example.dao; import org.springframework.jdbc.core.support.JdbcDaoSupport; public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao { public void in(String inner, int money) { this.getJdbcTemplate().update("update account set money = money -? where username = ?", money, inner); } public void out(String outter, int money) { this.getJdbcTemplate().update("update account set money = money +? where username = ?", money, outter); } }
service层
package com.test.spring.aop.tansation.example.service; import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.support.TransactionCallbackWithoutResult; import org.springframework.transaction.support.TransactionTemplate; import com.test.spring.aop.tansation.example.dao.AccountDao; public class AccountServiceImpl implements AccountService { private AccountDao accountDao; private TransactionTemplate transactionTemplate; public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; } public void setTransactionTemplate(TransactionTemplate transactionTemplate) { this.transactionTemplate = transactionTemplate; } public void tansfer(final String inner, final String outter, final int money) { //手动方式配置 transactionTemplate.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { accountDao.out(outter, money); //遇到异常时,事务回滚 int i = 1/0; accountDao.in(inner, money); } }); } }
测试
package com.test.spring.aop.tansation.example; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.test.spring.aop.tansation.example.service.AccountService; public class TestExample { @Test public void test() { ApplicationContext ctx = new ClassPathXmlApplicationContext("com/test/spring/aop/tansation/example/applicationContext.xml"); AccountService as = ctx.getBean("accountService",AccountService.class); as.tansfer("jack", "peter", 200); } }
2.半自动,使用代理的方式
这里只需要修改一下service层和配置就可以了
applicationContext.xml
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring"></property> <property name="user" value="root"></property> <property name="password" value="123456"></property> </bean> <bean id="accountDao" class="com.test.spring.aop.tansation.example.dao.AccountDaoImpl"> <property name="dataSource" ref="dataSource"></property> </bean> <bean id="accountService" class="com.test.spring.aop.tansation.example.service.AccountServiceImpl"> <property name="accountDao" ref="accountDao"></property> <property name="transactionTemplate" ref="transationTemplateProxy"></property> </bean> <!-- 半自动方式 --> <!-- 事务代理工厂类 参数1:接口 参数2:实现类 参数3:事务管理器 参数4:事务属性(事务详情) prop.key:确认那些方法使用事务 prop.value:事务详情 具体参数信息可以查阅TransactionDefinition类 格式:Propagation,Isolation,readOnly,-Exception,+Exception Propagation:传播行为 Isolation:隔离级别 readOnly:连接是否只读 -Exception:当遇到异常时,回滚 +Exception:当遇到异常时,提交 例子1:默认传播行为和隔离级别 <prop key="transfer">PROPAGATION_REQUIRED,ISOLATION_DEFAULT</prop> 例子2:只读,连接只读,不能进行update操作 <prop key="transfer">PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly</prop> 例子3:当遇到指定异常时,提交仍然提交事务,以为着之前执行的都将提交 <prop key="transfer">PROPAGATION_REQUIRED,ISOLATION_DEFAULT,+java.lang.ArithmeticException</prop> --> <bean id="transationTemplateProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="proxyInterfaces" value="com.test.spring.aop.tansation.example.service.AccountService"></property> <property name="target" ref="accountService"></property> <property name="transactionManager" ref="txManage"></property> <property name="transactionAttributes"> <props> <prop key="transfer"></prop> </props> </property> </bean> <bean id="txManage" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> </beans>
service层
package com.test.spring.aop.tansation.example.service; import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.support.TransactionCallbackWithoutResult; import org.springframework.transaction.support.TransactionTemplate; import com.test.spring.aop.tansation.example.dao.AccountDao; public class AccountServiceImpl implements AccountService { private AccountDao accountDao; private TransactionTemplate transactionTemplate; public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; } public void setTransactionTemplate(TransactionTemplate transactionTemplate) { this.transactionTemplate = transactionTemplate; } public void tansfer(final String inner, final String outter, final int money) { accountDao.out(outter, money); int i = 1/0; accountDao.in(inner, money); } }
3. 使用spring自动的方式
根据上面的一大串配置,会是很头疼,现在就是用全自动,简化上面繁琐的配置,除了修改配置外,其余的都不需要修改
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring"></property> <property name="user" value="root"></property> <property name="password" value="123456"></property> </bean> <bean id="accountDao" class="com.test.spring.aop.tansation.example.dao.AccountDaoImpl"> <property name="dataSource" ref="dataSource"></property> </bean> <bean id="accountService" class="com.test.spring.aop.tansation.example.service.AccountServiceImpl"> <property name="accountDao" ref="accountDao"></property> <!-- <property name="transactionTemplate" ref="transationTemplate"></property> --> <property name="transactionTemplate" ref="transationTemplateProxy"></property> </bean> <!-- 全自动 这里分为3块,具体的流程是,先有一个事务管理器,然后有一个事务管理通知,最后一个aop的切面配置, 对切面配置中的切入点进行事务管理,当遇到与事务管理通知配置中相符合的类名时,进行与之相符的配置行为,例如只读,或者异常提交等 --> <!-- 1.事务管理器 --> <bean id="txManage" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 2.事务通知 tx:method 就是指定那个方法,需要使用事务,而事务的详情就由里面的几个属性类决定 --> <tx:advice id="txAdvice" transaction-manager="txManage"> <tx:attributes> <tx:method name="transfer" propagation="REQUIRED" isolation="DEFAULT"/> </tx:attributes> </tx:advice> <!-- 3.aop切面类 --> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.test.spring.aop.tansation.example.service.AccountServiceImpl.*(..))"/> </aop:config> </beans>
原文地址:https://www.cnblogs.com/oscar1987121/p/10960570.html
时间: 2024-10-08 14:37:55