Programmatic Transactions Overview
The so-called programmatic transaction refers to the implementation of transactions by encoding, that is, similar to JDBC programming for transaction management.
The spring framework provides a consistent transaction abstraction, so that both JDBC and JTA transactions are programmed using the same API.
Java code: View Copy to clipboard print connection conn = null; usertransaction tx = null; try { tx = getusertransaction (); //1. Get Transactions tx.begin ();              //2. Open JTA Transaction conn = getdatasource (). getconnection (); //3. Get jdbc //4. Disclaimer sql String sql = "Select * from information_schema. System_tables "; preparedstatement pstmt = conn.preparestatement (SQL);//5. Precompiled sql resultset rs = pstmt.executequery (); //6. Performing sql process (RS) ; //7. Working with result sets closeresultset (RS);    //8. Releasing result sets tx.commit (); //7. Commit Transaction } catch (exception e) { tx.rollback (); //8. Rolling back transactions throw e; } finally { conn.close (); //Closing Connections }
Here you can see that the control transactions are performed using UserTransaction instead of the connection connection, so that the JDBC transaction and JTA transactions are programmed with different APIs, and the JTA and JDBC Transaction management exceptions are not the same.
For transaction management using JTA programming, refer to the Tranditionaltransactiontest class under the Cn.javass.spring.chapter9 package.
In spring, a consistent transaction abstraction is used for control and consistent exception control, that is, for Platformtransactionmanager interface programming to control transactions. Spring support for programmatic transactions
The transactions in spring are divided into physical and logical transactions, physical transactions: transactional support provided by the underlying database, such as those provided by JDBC or JTA, logical transactions: Spring-managed transactions, and logical transactions that provide richer control than physical transactions. And if you want to get the benefits of spring transaction management, you must use logical transactions, so in spring if you do not specifically emphasize logical transactions;
Logical transactions support very low levels of control, as well as high-level solutions: low-tier solutions:
Tool classes: Use the tool class to get connections (sessions) and release connections (sessions), such as using the Connectionutils class in the Org.springframework.jdbc.datasource package to get and release connections with logical transaction functionality. Of course, the integrated third-party ORM Framework also provides similar tool classes, such as the Sessionfactoryutils tool class for Hibernate, the JPA entitymanagerfactoryutils, etc., and other tool classes that use similar * * * Utils name;
Java code: View Copy to clipboard print//Get connection Datasourceutils with spring transaction (logical transaction) management function. Getconnection (DataSource DataSource)//Releases the connection Datasourceutils with the spring transaction (logical transaction) management feature. Releaseconnection (Connection con, DataSource DataSource)
Transactionawaredatasourceproxy: Using this data source proxy class wrapper requires a data source supported by spring transaction management, which must be at the outermost layer, Used primarily for legacy projects where it is possible to use data sources directly to get connections and release connection support, or to use a variety of persistence frameworks in spring, which actually uses the Connectionutils tool class to acquire and release a true connection;
Java code: View Copy to clipboard Print <!--Use this way to wrap the data source, you must be at the outermost, Targetdatasource know the target data source--<bean id= "datasourceproxy" class= "org. Springframework.jdbc.datasource. Transactionawaredatasourceproxy "> <property name=" targetdatasource "ref=" da Tasource "/> </bean>
By wrapping a data source as above, you can use physical transaction encoding in your project to obtain support for logical transactions, that is, support for getting connections and freeing connections directly from DataSource, and those connections automatically support spring logical transactions; high-level solutions:
Template classes: Use the template classes provided by spring, such as the JdbcTemplate, hibernatetemplate, and jpatemplate template classes, which actually use the tool classes in a low-level solution to manage connections or sessions;
Spring provides two programmatic transaction support: Implement and use the Transactiontemplate template class directly using Platformtransactionmanager to support logical transaction management.
It is recommended to use Transactiontemplate template classes and high-level solutions when using programmatic transactions. using Platformtransactionmanager
Let's start by looking at how to use the Platformtransactionmanager implementation for transaction management:
1, the data source definition, here Use the 7th chapter of the configuration file, namely "Chapter7/applicationcontext-resources.xml" file.