1 transaction manager
1.1 description
Spring abstracts a complete set of transaction management mechanism from different transaction management API s, which makes the transaction management code independent of specific transaction technology. Developers manage transactions through configuration without knowing how the underlying layer is implemented.
The core transaction management abstraction of Spring is platform transaction manager, which encapsulates a set of technology independent methods for transaction management. No matter which transaction management strategy (programmatic or declarative) Spring uses, the transaction manager is required.
1.2 main implementation classes
Data source transaction manager: only one data source needs to be processed in the application and accessed through JDBC.
JTA transaction manager: use JTA (Java Transaction API) for transaction management on Java EE application server.
Hibernate transaction manager: use hibernate framework to access database.
2 annotation based use
2.1 configuration file
The transaction manager needs to be configured and a data source is required.
If annotation based transactions need to be started, they can be started with tx namespace.
<!-- Configure the scanner. --> <context:component-scan base-package="com" /> <!-- Configure external resources. --> <context:property-placeholder location="classpath:jdbc.properties" /> <!-- Configure the data source. --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${mysql.user}" /> <property name="password" value="${mysql.password}" /> <property name="jdbcUrl" value="${mysql.jdbcUrl}" /> <property name="driverClass" value="${mysql.driverClass}" /> </bean> <!-- to configure JdbcTemplate An example of a framework. --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource" /> </bean> <!-- Configure the multi transaction manager. --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- use tx Namespace enables annotation based transaction management, transaction-manager Represents the transaction manager when the id by transactionManager It can be omitted when. --> <tx:annotation-driven transaction-manager="transactionManager" />
2.2 using annotations on transaction methods
Annotate the transaction control method that requires multiple operations.
@Transactional public void checkout(Book book) { bookDao.updateSales(book.getSales() + 1, book.getId()); bookDao.updateStock(book.getStock() - 1, book.getId()); }
3 XML based usage
The transaction manager needs to be configured and a data source is required.
Methods and entry points for transaction management need to be configured.
<!-- Configure the scanner. --> <context:component-scan base-package="com" /> <!-- Configure external resources. --> <context:property-placeholder location="classpath:jdbc.properties" /> <!-- Configure the data source. --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${jdbc.user}" /> <property name="password" value="${jdbc.password}" /> <property name="jdbcUrl" value="${jdbc.jdbcUrl}" /> <property name="driverClass" value="${jdbc.driverClass}" /> </bean> <!-- to configure JdbcTemplate An example of a framework. --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource" /> </bean> <!-- Configure the multi transaction manager. --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- use tx Tag configuration transaction recommendations, transaction-manager Represents the transaction manager. --> <tx:advice transaction-manager="transactionManager" id="bookAdvice"> <tx:attributes> <!-- Configure methods and their transaction properties, name Represents the matching method name. --> <tx:method name="*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <!-- Configure transactions and associate pointcuts with transaction suggestions. --> <aop:config> <!-- Specify pointcuts and pointcut expressions. --> <aop:pointcut expression="execution(* com.tx.BookService.*(..))" id="bookPoint" /> <!-- Specify the recommendations to use. --> <aop:advisor advice-ref="bookAdvice" pointcut-ref="bookPoint" /> </aop:config>
4 set the isolation level of transactions
4.1 annotation based use
You can set the propagation behavior of transactions in the isolation attribute of Transactional.
@Transactional(isolation=Isolation.READ_COMMITTED)
4.2 XML based usage
You can use the isolation property setting in the method tag of the tx namespace.
<tx:method name="*" isolation="READ_COMMITTED" />
5. Set the propagation behavior of the transaction
5.1 annotation based use
It can be set in the propagation property of Transactional.
@Transactional(propagation=Propagation.REQUIRED)
5.2 XML based usage
You can use the propagation property setting in the method tag of the tx namespace.
<tx:method name="*" propagation="REQUIRED" />
6. Set the exception that triggers transaction rollback
By default, it is rolled back when RuntimeException or Error is caught, while it is not rolled back when compile time exception is caught.
6.1 setting exceptions for transaction rollback
6.1.1 annotation based use
It can be set in the rollbackFor property of Transactional, and multiple are separated by commas.
@Transactional(rollbackFor={SQLException.class, ArithmeticException.class})
6.1.2 XML based usage
You can set the rollback for attribute in the method tag of the tx namespace, with multiple separated by commas.
<tx:method name="*" rollback-for="java.sql.SQLException, java.lang.ArithmeticException" />
6.2 set the exception that the transaction is not rolled back
6.2.1 annotation based use
It can be set in the noRollbackFor property of Transactional, and multiple are separated by commas.
@Transactional(noRollbackFor={SQLException.class, ArithmeticException.class})
6.2.2 XML based usage
You can set the no rollback for attribute in the method tag of the tx namespace, with multiple separated by commas.
<tx:method name="*" no-rollback-for="java.sql.SQLException, java.lang.ArithmeticException" />
7 set the read-only attribute of the transaction
Spring's JdbcTemplate makes some optimizations for read-only operations.
7.1 annotation based use
It can be set in the readOnly property of Transactional. true means read-only.
@Transactional(readOnly=true)
7.2 XML based usage
You can use read in the method tag of the tx namespace_ Set the only property. true means read-only.
<tx:method name="*" read-only="true" />
8 set the timeout attribute of the transaction
8.1 annotation based use
It can be set in the timeout attribute of Transactional. The default is - 1, which means never timeout.
@Transactional(timeout=30)
8.2 XML based usage
You can set the timeout property in the method tag of tx namespace. The default value is - 1, which means never timeout.
<tx:method name="*" timeout="30" />