Java Transaction (ii)-Pass connection

Source: Internet
Author: User

I. Why transfer connection?

As we know in the previous overview, the JDBC transaction is connection, so you want to control the operation within the same transaction,

We have to pass connection to make sure we are using the same connection.


Two. How to pass connection?

Example of using this example: transfer from a account 100 to B account, which requires two Update table operation

1. Code structure diagram:


2. Build the Table statement:

DROP TABLE IF EXISTS ' account '; CREATE TABLE ' account ' (  ' id ' int (ten) not null,  ' name ' varchar () ' is not null ',  ' money ' int () ' is not NULL,  PRI MARY KEY  (' id ')) INSERT INTO ' account ' values (' 1 ', ' Lucy ', ' + '); INSERT into ' account ' values (' 2 ', ' Lili ', ' 1000 ') ;

3. Entity classes:

public class Account {private int id;private String name;private int money;//getter and setter}

4. C3P0 Connection Pool configuration:


5. JDBC Tool class:

public class Jdbcutils {private static DataSource datasource;static {//Load C3P0 connection Pool DataSource = new Combopooleddatasource () ;} public static DataSource Getdatasource () {return DataSource;} public static Connection getconnection () throws SQLException {return datasource.getconnection ();}

6. Business Logic class:

/** * Business Logic Layer */public class Accountservice {public void transfer (account Outaccount, account Inaccount, int. money) throws S qlexception {//open transaction Connection conn = Jdbcutils.getconnection (); Conn.setautocommit (false);//Query two accounts Accountdao Accountdao = new Accountdao (); outaccount = Accountdao.findaccountbyid (Outaccount.getid ()); Inaccount = Accountdao.findaccountbyid (Inaccount.getid ());//Transfer-Modify the original account amount Outaccount.setmoney (Outaccount.getmoney ()-money); Inaccount.setmoney (Inaccount.getmoney () + money); try {//Update account amount, note: here to DAO layer Pass connection Accountdao.update (OUTACCOUNT, conn); /int x = 1/0; Accountdao.update (INACCOUNT, conn);//Transfer successful, COMMIT Transaction Conn.commit ();} catch (Exception e) {//Transfer failed, ROLLBACK transaction conn.rollback (); E.printstacktrace ();}}}

7. DAO Class:

/** * DAO Layer: CRUD */public class Accountdao {//Query account public accounts findaccountbyid (int id) throws SQLException {String sql = "SELECT * from account where id =?"; O Bject[] params = {ID}; Queryrunner Queryrunner = new Queryrunner (Jdbcutils.getdatasource ()); return queryrunner.query (SQL, new Beanhandler <Account> (Account.class), params);}  Update account: Accept pass-through connection public void update (accounts account, Connection conn) throws SQLException {String sql = "Update accounts set Name =?, Money =? WHERE id =? "; O Bject[] params = {account.getname (), Account.getmoney (), Account.getid ()}; Queryrunner Queryrunner = new Queryrunner () queryrunner.update (conn, SQL, params);}}

8. Test class:

public class Transfertest {@Testpublic void Transfertest () throws SQLException {account out = new account (); Out.setid (1); A Ccount in = new account (); In.setid (2); Accountservice accountservice = new Accountservice (); Accountservice.transfer (out, in, 100);}}


Three. Summary:

The method of passing the connection object above can accomplish the purpose of the transaction, but it is ugly because: in order to accomplish the purpose of transaction processing,

We need to pass an underlying connection class between the service layer and the DAO layer, and the DAO layer method needs to accept the connection object, which is a typical API pollution.




Java Transaction (ii)-Pass connection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.