Summary of support for spring container management Services

Source: Internet
Author: User

1. Problem Java code
  1. Connection conn =
  2. Datasourceutils.getconnection ();
  3. //Open transaction
  4. Conn.setautocommit (false);
  5. try {
  6. Object RetVal =
  7. Callback.doinconnection (conn);
  8. Conn.commit (); //Commit a transaction
  9. return retVal;
  10. }catch (Exception e) {
  11. Conn.rollback (); //ROLLBACK TRANSACTION
  12. throw E;
  13. }finally {
  14. Conn.close ();
  15. }
Java code
  1. Session session = null;
  2. Transaction Transaction = null;
  3. try {
  4. Session = Factory.opensession ();
  5. //Open transaction
  6. Transaction = Session.begintransaction ();
  7. Transation.begin ();
  8. Session.save (user);
  9. Transaction.commit (); //Commit a transaction
  10. } catch (Exception e) {
  11. E.printstacktrace ();
  12. Transaction.rollback (); //ROLLBACK TRANSACTION
  13. return false;
  14. }finally{
  15. Session.close ();
  16. }
Cons: Inconsistent transaction management, especially when multiple method invocations need to be within the same transaction; 2. High-level Solution Java code
    1. Public interface Platformtransactionmanager {
    2. Transactionstatus gettransaction (transactiondefinition definition)
    3. throws TransactionException;
    4. void commit (Transactionstatus status) throws TransactionException;
    5. void rollback (transactionstatus status) throws TransactionException;
    6. }
Java code
  1. 1. Get the transaction manager
  2. Platformtransactionmanager Txmanager = (Platformtransactionmanager)
  3. Ctx.getbean ("Txmanager");
  4. 2. Defining Transaction Properties
  5. Defaulttransactiondefinition td = new Defaulttransactiondefinition ();
  6. Td.setisolationlevel (transactiondefinition.isolation_read_committed);
  7. 3 open transaction, get transaction status
  8. Transactionstatus status = Txmanager.gettransaction (TD);
  9. try {
  10. //4. Performing Database Operations
  11. System.out.println (Jdbctempate.queryforint ("SELECT count (*) from Tbl_doc"));
  12. //5, commit a transaction
  13. Txmanager.commit (status);
  14. }catch (Exception e) {
  15. //6, rolling back transactions
  16. Txmanager.rollback (status);
  17. }
There are too many duplicate codes, and you must manually open/release (commit/Rollback) transactions. 3. High-level template solution Java code
  1. 1. Get the transaction manager
  2. Platformtransactionmanager Txmanager = (Platformtransactionmanager)
  3. Ctx.getbean ("Txmanager");
  4. 2. Define a template for transaction management
  5. Transactiontemplate transactiontemplate = new Transactiontemplate (Txmanager);
  6. 3. Defining Transaction Properties
  7. Transactiontemplate.
  8. Setisolationlevel (transactiondefinition.isolation_read_committed);
  9. 4. Callback, perform real database operation, if need return value need to return in callback
  10. Transactiontemplate.execute (new Transactioncallback () {
  11. @Override
  12. Public Object dointransaction (transactionstatus status) {
  13. //5. Performing Database Operations
  14. System.out.println (Jdbctempate.queryforint ("SELECT count (*) from Tbl_doc"));
  15. return null;
  16. }
  17. });
To write the template code, we know that the transaction is actually a facet, so we solve the 4, AOP solution through AOP-a declarative transaction scenario Nspring Framework provides a consistent transaction management abstraction, which brings the following benefits: 1: Provides a consistent programming model for complex transactional APIs, such as JTA, JDBC, Hibernate, JPA, and JDO2: Supports declarative transaction Management 3: Provides simpler, easier-to-use programmatic transaction management API4 than complex transactional APIs such as JTA: the steps to integrate spring's various data access abstractions to implement transactions 1 , definition (resources) datasource/sessionfactory ... 2, define the transaction manager (manage the transaction of the Resource) 3, define the transaction notification: defines how to implement the transaction (the method name and the corresponding transaction property of the implementation transaction), needs to use the transaction manager to manage the transaction, defines how to select the target object's method and the implementation of the transaction properties 4, Define Advisor (Pointcuts and transaction Notifications): Pointcuts Select the target object (which must be the business logic layer) that needs to implement the transaction 5, spring weaves the transaction notification to the target object (AOP proxy)



Summary of support for spring container management Services

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.