How to implement transaction and transaction queue in weed3-5
This article is to share with you about how to achieve transactions and transaction queues in weed3-5. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.
1. Transactions (mainly for a single library)
/ / demo1:: / / transaction group / / make 4 inserts in a transaction / / if there is an error, automatically roll back DbUserApi dbUserApi = XmlSqlProxy.getSingleton (DbUserApi.class); db.tran ((t)-> {/ the operation within this expression will automatically join the transaction / sql interface db.sql ("insert into test (txt) values (?)", "cc") .insert () Db.sql ("update test set txt='1' where id=1"). Execute (); / / call interface db.call ("user_del"). Set ("_ user_id", 10). Execute (); / / table () interface db.table ("a_config"). Set ("cfg_id", 1). Insert (); / / xml mapper dbUserApi.user_add (12); / / everyone uses a unified transaction mode})
two。 Transaction queue (mainly used in the case of multiple libraries)
/ / demo2:: / / transaction queue / if you want to operate across two databases (one transaction object is not available) / / DbContext db = DbConfig.pc_user;DbContext db2 = DbConfig.pc_base;// create a transaction queue (different from the traditional queue) DbTranQueue queue = new DbTranQueue () / / transaction of database 1 db.tran (). Join (queue). Execute ((t) = > {/ in this expression, things / / db.sql ("insert into test (txt) values (?)", "cc") .execute (); db.sql ("insert into test (txt) values (?)", "dd") .execute () Db.sql ("insert into test (txt) values (?)", "ee"). Execute ();}); / / transaction db2.tran (). Join (queue). Execute ((t) = > {/ in this expression, things / / db2.sql ("insert into test (txt) values (?)", "gg"). Execute ();}) / / queue group completion (that is, start running transaction) queue.complete ()
3. An enhanced version of the transaction queue to run transactions across functions or modules
Public void test_main () {DbTranQueue queue = new DbTranQueue (); test1 (queue); test2 (queue);} public void test1 (DbTranQueue queue) {DbTran tran = DbConfig.db1.tran () / / generate a transaction object tran.join (queue). Execute ((t)-> {/ in this expression, things / / t.db () .sql ("insert into $.test (txt) values (?)", "cc") .insert () T.db () .sql ("insert into $.test (txt) values (?)", "dd") .execute (); t.db () .sql ("insert into $.test (txt) values (?)", "ee") .execute (); t.result = t.db () .sql ("select name from $.user_info where user_id=3"). GetValue (");}) } public void test2 (DbTranQueue queue) {/ /. Test2 will not write} the above is how to implement transactions and transaction queues in weed3-5. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.