演示不使用事務出現異常情況 Dao層兩個方法lessMoney()和moreMoney() Service層調用兩個方法 但是兩個操作減與加之間,如果出現異常,則會導致轉賬錢已經轉了,但對方卻沒有到賬的bug,可能伺服器突然故障等引起 解決添加事務,出現異常進行回滾操作 下麵使用配置文件的方法進行事 ...
演示不使用事務出現異常情況
Dao層兩個方法lessMoney()和moreMoney()
package com.swift; import org.springframework.jdbc.core.JdbcTemplate; public class AccountDao { private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } //少錢的方法 public void lessMoney(String from,double number) { String sql="update account set money=money-? where username=?"; jdbcTemplate.update(sql, number,from); } //多錢的方法 public void moreMoney(String to,double number) { String sql="update account set money=money+? where username=?"; jdbcTemplate.update(sql, number,to); } }
Service層調用兩個方法
package com.swift; public class AccountService { private AccountDao accountDao; public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; } public void moneyTransfer(String from,String to,double number) { accountDao.lessMoney(from,number); int i=10/0; accountDao.moreMoney(to,number); } }
但是兩個操作減與加之間,如果出現異常,則會導致轉賬錢已經轉了,但對方卻沒有到賬的bug,可能伺服器突然故障等引起
解決添加事務,出現異常進行回滾操作
下麵使用配置文件的方法進行事務管理
沒有改變的測試類
package com.swift; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; @WebServlet("/test") public class ServletTest extends HttpServlet { private static final long serialVersionUID = 1L; public ServletTest() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); response.getWriter().append("Served at: ").append(request.getContextPath()); //使用JdbcTemplat的queryForObject方法 ApplicationContext context=new ClassPathXmlApplicationContext("c3p0.xml"); AccountService accountService= (AccountService) context.getBean("accountService"); accountService.moneyTransfer("傭兵組織", "高揚", 100000); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
使用配置文件進行事務處理步驟如下:
<?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: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.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- c3p0連接池得到dataSource --> <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/sw_database"></property> <property name="user" value="root"></property> <property name="password" value="root"></property> </bean> <!-- 第一步 配置事務管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 註入dataSource --> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 第二步 配置事務增強 --> <tx:advice id="txadvice" transaction-manager="transactionManager"> <!-- 做事務操作 --> <tx:attributes> <!-- 事務操作的方法匹配規則 --> <tx:method name="money*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!-- 第三步 配置切麵 --> <aop:config> <!-- 切入點 --> <aop:pointcut expression="execution(* com.swift.AccountService.*(..))" id="pointcut1"/> <!-- 切麵 --> <aop:advisor advice-ref="txadvice" pointcut-ref="pointcut1"/> </aop:config> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <bean id="accountDao" class="com.swift.AccountDao"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> <bean id="accountService" class="com.swift.AccountService"> <property name="accountDao" ref="accountDao"></property> </bean> </beans>
分三個步驟 管理 增強和切麵
雖然還是會有異常,但是資料庫中不會出錯,不會錢轉出了,卻沒有到賬
工具類Account
package com.swift; public class Account { private int id; private String username; private String money; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getMoney() { return money; } public void setMoney(String money) { this.money = money; } public Account(int id, String username, String money) { this.id = id; this.username = username; this.money = money; } public Account() { super(); // TODO Auto-generated constructor stub } @Override public String toString() { return "Account [id=" + id + ", username=" + username + ", money=" + money + "]"; } }