轉帳案例
1)參見圖<<轉帳各類的交互圖示>>

2)項目中,事務可能在dao層,也可能在service層,不論在哪一層,都必須確保使用的都是同一個connection
3)為了確保在Service和Dao層中用到的Connection一致,你可以使用如下方案解決:
a)將Service中的Connection傳入Dao中
設計缺點:
Service和Dao代碼過分藕合
在Service中引用了非業務邏輯操作
b)將JdbcUtil類中的Connection作成單例/態
c)使用ThreadLocale將每個線程和自已的Connection綁定在一起,每個線程修改自已的Connection,
不會影響其它線程的Connection
4)在分層結構中,關閉Connection會推遲到Service層,但一定要關閉Connection對象
2)項目中,事務可能在dao層,也可能在service層,不論在哪一層,都必須確保使用的都是同一個connection
3)為了確保在Service和Dao層中用到的Connection一致,你可以使用如下方案解決:
a)將Service中的Connection傳入Dao中
設計缺點:
Service和Dao代碼過分藕合
在Service中引用了非業務邏輯操作
b)將JdbcUtil類中的Connection作成單例/態
c)使用ThreadLocale將每個線程和自已的Connection綁定在一起,每個線程修改自已的Connection,
不會影響其它線程的Connection
4)在分層結構中,關閉Connection會推遲到Service層,但一定要關閉Connection對象
Accont.sqldrop table account;create table account(id int primary key auto_increment,name varchar(20) not null,salary float);insert into account(name,salary) values('aaa',3000);insert into account(name,salary) values('bbb',3000);select * from account;Account.java//帳戶public class Account {private int id;//帳號private String name;//用戶名private float salary;//薪水public Account(){}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public float getSalary() {return salary;}public void setSalary(float salary) {this.salary = salary;}}TransferDao.javapublic class TransferDao {//根據ID號查詢帳戶public Account findAccountById(int id) throws SQLException{Account account = null;Connection conn = null;PreparedStatement pstmt = null;ResultSet rs = null;String sql = "select * from account where id = ?";try {conn = JdbcUtil.getMySqlConnection();pstmt = conn.prepareStatement(sql);pstmt.setInt(1,id);rs = pstmt.executeQuery();if(rs.next()){account = new Account();account.setId(id);account.setName(rs.getString("name"));account.setSalary(rs.getFloat("salary"));}} catch (Exception e) {e.printStackTrace();}finally{JdbcUtil.close(rs);JdbcUtil.close(pstmt);//JdbcUtil.close(conn);}return account;}//根據ID號更新帳戶public void updateAccountById(Account newAccount) throws SQLException{Connection conn = null;PreparedStatement pstmt = null;ResultSet rs = null;String sql = "update account set salary = ? where id = ?";try {conn = JdbcUtil.getMySqlConnection();//conn=123pstmt = conn.prepareStatement(sql);pstmt.setFloat(1,newAccount.getSalary());pstmt.setInt(2,newAccount.getId());pstmt.executeUpdate();} catch (Exception e) {e.printStackTrace();}finally{JdbcUtil.close(rs);JdbcUtil.close(pstmt);//JdbcUtil.close(conn);}}}public class TransferService {//轉帳public void transfer(int sid,int tid,float money) throws Exception{//NO1:判段轉入和轉出帳號是否存在TransferDao transferDao = new TransferDao();Account sAccount = transferDao.findAccountById(sid);Account tAccount = transferDao.findAccountById(tid);if(sAccount!=null && tAccount!=null){//NO2:判段轉出帳號是否有足夠的余額if(sAccount.getSalary()-money >= 0){//進行轉帳操作sAccount.setSalary(sAccount.getSalary() - money);tAccount.setSalary(tAccount.getSalary() + money);try {//事務開始JdbcUtil.begin();//conn=123transferDao.updateAccountById(sAccount);//int i = 10/0;transferDao.updateAccountById(tAccount);//事務提交JdbcUtil.commit();} catch (Exception e) {e.printStackTrace();try {//事務回滾JdbcUtil.rollback();//事務提交JdbcUtil.commit();} catch (Exception e1) {}throw e;}finally{//關閉Connection對象JdbcUtil.closeConnection();}}}}//取款public void withdraw(int sid, float money)throws Exception{TransferDao transferDao = new TransferDao();Account sAccount = transferDao.findAccountById(sid);if(sAccount!=null){if(sAccount.getSalary()-money >= 0){sAccount.setSalary(sAccount.getSalary() - money);try {transferDao.updateAccountById(sAccount);} catch (Exception e) {e.printStackTrace();}finally{JdbcUtil.closeConnection();}}else{throw new NoMoneyException();}}else{throw new NoAccountException();}}}
看文倉www.kanwencang.com網友整理上傳,為您提供最全的知識大全,期待您的分享,轉載請注明出處。
歡迎轉載:http://www.kanwencang.com/bangong/20170205/98066.html
文章列表