首页 > 图灵资讯 > 技术篇>正文

Apache_DBUtils框架教程学习笔记(三)_事务处理

2023-04-16 09:27:13

  4.2、在业务层(BusinessService)处理事务

  由于上述AccountDao存在具体的业务处理方法,AccountDao的职责不够单一。让我们改造AccountDao,让AccountDao的职责只做CRUD操作,把事务处理转移到业务层面(BusinessService),AccountDao改造如下: package wkcto.com.dao; import java.sql.Connection; import java.sql.SQLException; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import wkcto.com.domain.Account; import wkcto.com.util.JdbcUtils; /*account测试表 create table account( id int primary key auto_increment, name varchar(40), money float )character set utf8 collate utf8_general_ci; insert into account(name,money) values('A',1000); insert into account(name,money) values('B',1000); insert into account(name,money) values('C',1000); */ /** * @ClassName: AccountDao * @Description: CRUD针对Account对象 * */ public class AccountDao { ///接收service层传输的Conection对象 private Connection conn = null; public AccountDao(Connection conn){ this.conn = conn; } public AccountDao(){ } /** * @Method: update * @Description:更新 * @param account * @throws SQLException */ public void update(Account account) throws SQLException{ QueryRunner qr = new QueryRunner(); String sql = "update account set name=?,money=?,money=? where id=?"; Object params[] = {account.getName(),account.getMoney(),account.getId()}; ///使用service层传输的Conection对象操作数据库 qr.update(conn,sql, params); } /** * @Method: find * @Description:查找 * @param id * @return * @throws SQLException */ public Account find(int id) throws SQLException{ QueryRunner qr = new QueryRunner(); String sql = "select * from account where id=?"; ///使用service层传输的Conection对象操作数据库 return (Account) qr.query(conn,sql, id, new BeanHandler(Account.class)); } }

  然后改造Accountservice(业务层)中的transfer方法,在业务层面(BusinessService)中处理事务 package wkcto.com.service; import java.sql.Connection; import java.sql.SQLException; import wkcto.com.dao.AccountDao; import wkcto.com.domain.Account; import wkcto.com.util.JdbcUtils; /** * @ClassName: AccountService * @Description: 业务逻辑处理层 */ public class AccountService { /** * @Method: transfer * @Description:该方法用于处理两个用户之间的转账业务 * @param sourceid * @param tartgetid * @param money * @throws SQLException */ public void transfer(int sourceid,int tartgetid,float money) throws SQLException{ Connection conn = null; try{ //获取数据库连接 conn = JdbcUtils.getConnection(); //开始事务 conn.setAutoCommit(false); ///将获得的Conection传递给Accountdao,确保dao层使用相同的Conection对象操作数据库 AccountDao dao = new AccountDao(conn); Account source = dao.find(sourceid); Account target = dao.find(tartgetid); source.setMoney(source.getMoney()-money); target.setMoney(target.getMoney()+money); dao.update(source); ///模拟程序异常导致事务回滚 int x = 1/0; dao.update(target); ///提交事务 conn.commit(); }catch (Exception e) { e.printStackTrace(); ///出现异常后,回滚事务 conn.rollback(); }finally{ conn.close(); } } }

  经过改造,AccountDao只负责CRUD,没有具体的业务处理方法,责任单一,AccountService负责具体的业务逻辑和事务处理。当需要操作数据库时,调用AccountDao层提供的CRUD方法操作数据库,正好迎合程序“高内聚,低耦合”开发原则。

  Apache_DBUTils框架教程学习笔记(4)_事务处理

  Apache_dbutils框架教程学习笔记(5)_filter处理事务

上一篇 Apache_DBUtils框架教程学习笔记(四)_事务处理
下一篇 Apache_DBUtils框架教程学习笔记(二)_事务处理

文章素材均来源于网络,如有侵权,请联系管理员删除。