1. Problem Java code
- Connection conn =
- Datasourceutils.getconnection ();
- //Open transaction
- Conn.setautocommit (false);
- try {
- Object RetVal =
- Callback.doinconnection (conn);
- Conn.commit (); //Commit a transaction
- return retVal;
- }catch (Exception e) {
- Conn.rollback (); //ROLLBACK TRANSACTION
- throw E;
- }finally {
- Conn.close ();
- }
Java code
- Session session = null;
- Transaction Transaction = null;
- try {
- Session = Factory.opensession ();
- //Open transaction
- Transaction = Session.begintransaction ();
- Transation.begin ();
- Session.save (user);
- Transaction.commit (); //Commit a transaction
- } catch (Exception e) {
- E.printstacktrace ();
- Transaction.rollback (); //ROLLBACK TRANSACTION
- return false;
- }finally{
- Session.close ();
- }
Cons: Inconsistent transaction management, especially when multiple method invocations need to be within the same transaction; 2. High-level Solution Java code
- Public interface Platformtransactionmanager {
- Transactionstatus gettransaction (transactiondefinition definition)
- throws TransactionException;
- void commit (Transactionstatus status) throws TransactionException;
- void rollback (transactionstatus status) throws TransactionException;
- }
Java code
- 1. Get the transaction manager
- Platformtransactionmanager Txmanager = (Platformtransactionmanager)
- Ctx.getbean ("Txmanager");
- 2. Defining Transaction Properties
- Defaulttransactiondefinition td = new Defaulttransactiondefinition ();
- Td.setisolationlevel (transactiondefinition.isolation_read_committed);
- 3 open transaction, get transaction status
- Transactionstatus status = Txmanager.gettransaction (TD);
- try {
- //4. Performing Database Operations
- System.out.println (Jdbctempate.queryforint ("SELECT count (*) from Tbl_doc"));
- //5, commit a transaction
- Txmanager.commit (status);
- }catch (Exception e) {
- //6, rolling back transactions
- Txmanager.rollback (status);
- }
There are too many duplicate codes, and you must manually open/release (commit/Rollback) transactions. 3. High-level template solution Java code
- 1. Get the transaction manager
- Platformtransactionmanager Txmanager = (Platformtransactionmanager)
- Ctx.getbean ("Txmanager");
- 2. Define a template for transaction management
- Transactiontemplate transactiontemplate = new Transactiontemplate (Txmanager);
- 3. Defining Transaction Properties
- Transactiontemplate.
- Setisolationlevel (transactiondefinition.isolation_read_committed);
- 4. Callback, perform real database operation, if need return value need to return in callback
- Transactiontemplate.execute (new Transactioncallback () {
- @Override
- Public Object dointransaction (transactionstatus status) {
- //5. Performing Database Operations
- System.out.println (Jdbctempate.queryforint ("SELECT count (*) from Tbl_doc"));
- return null;
- }
- });
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