Cons: The service plane completes the DAO-level Open transaction operation
1. Create your own C3p0uti,account database and import the jar package
2.Dao level
Interface:
Packagecom.learning. DAO;ImportCom.learning.domain.Account; Public InterfaceAccountdao {/*** Transfer *@paramfromname Transfer out user *@paramtoname Transfer to User *@paramMoney transfer Amount*/@Deprecated Public voidUpdateaccount (String fromname,string ToName,DoubleMoneythrowsException; /*** Change Amount according to account information *@paramaccout*/ Public voidUpdateaccout (account Accout)throwsException; /*** Find account information by user name *@paramname *@return * @throwsException*/ PublicAccount Findaccountbyname (String name)throwsException;}
Implementation class:
Packagecom.learning. Dao.impl;Importjava.sql.Connection;Importjava.sql.SQLException;ImportOrg.apache.commons.dbutils.QueryRunner;ImportOrg.apache.commons.dbutils.handlers.BeanHandler;Importcom.learning. Dao. Accountdao;Importcom.learning. Domain. account;Importcom.learning. util. C3p0util; Public classAccountdaoimplImplementsAccountdao {PrivateConnection Conn; PublicAccountdaoimpl (Connection conn) { This. conn =Conn; } Public voidUpdateaccount (String fromname, String toname,DoubleMoneythrowsException {//Create a Queryrunner objectQueryrunner QR =NewQueryrunner (C3p0util.getdatasource ()); Qr.update ("Update account set money=money-?" where Name=? ", Money,fromname); Qr.update ("Update account set money=money+?" where Name=? ", Money,toname); } Public voidUpdateaccout (account account)throwsException {queryrunner qr=NewQueryrunner (); Qr.update (Conn,"Update account set money=?" where Name=? ", Account.getmoney (), Account.getname ()); } PublicAccount Findaccountbyname (String name)throwsException {queryrunner qr=NewQueryrunner (); returnQr.query (conn, "select * from account where name=?",NewBeanhandler<account> (account.class), name); }}
3.Service level
Interface:
Package com.learning. Service; Public Interface Accountservice { /** * transfer @param fromname transfer out of the user @param toname transferred to user @param money transfer amount * /publicvoid transfer (String fromname,string toname,double Money );}
Implementation class:
Packagecom.learning. Service.impl;Importjava.sql.Connection;Importjava.sql.SQLException;Importcom.learning. Dao. Accountdao;Importcom.learning. Dao.impl.AccountDaoImpl;Importcom.learning. Domain. account;Importcom.learning. Service. Accountservice;Importcom.learning. util. C3p0util; Public classAccountserviceimplImplementsAccountservice { Public voidTransfer (String fromname, String toname,DoubleMoney ) { //Ad.updateaccount (FromName, ToName, money);Connection conn =c3p0util.getconnection (); Accountdao AD=NewACCOUNTDAOIMPL (conn); Try{conn.setautocommit (false);//begin//get transferred out and transferred to the account object separatelyAccount Fromaccount =Ad.findaccountbyname (FromName); Account Toaccount=Ad.findaccountbyname (ToName); //Modify the respective amount of the accountFromaccount.setmoney (Fromaccount.getmoney ()-Money ); Toaccount.setmoney (Toaccount.getmoney ()+Money ); //Complete the transfer operationad.updateaccout (fromaccount);//int i = 10/0;ad.updateaccout (Toaccount); Conn.commit ();//Commit a transaction}Catch(Exception e) {Try{conn.rollback ();//rolling back a transaction}Catch(SQLException E1) {e1.printstacktrace (); } }finally{ Try{conn.close (); } Catch(SQLException e) {e.printstacktrace (); }//Close } }}
Transfer Example (ii): Service level implementation (this example uses Queryrunner to execute SQL statements with Data source C3P0)