Spring’s transaction control can be divided into programmatic transaction control and Declarative transaction control.
Programmatic
Developers directly couple transaction code and business code together, which is not used in actual development.
Declarative
Developers use configuration to achieve transaction control, decoupling business code and transaction code, and using AOP ideas.
PlatformTransactionManager interface is spring's transaction manager interface, which provides our commonly used methods for operating transactions.
##2.2TransactionDefinitionThe TransactionDefinition interface provides transaction definition information (transaction isolation level, transaction propagation behavior, etc.)(1) Transaction isolation level
Setting the isolation level can solve problems caused by transaction concurrency, such as dirty reads, unavailable Repeated reading and virtual reading (phantom reading).Note: Use the database default level. If the database is mysql, the default is repeatable read, oracle is read committed.
ISOLATION_DEFAULT Use database default level
ISOLATION_READ_UNCOMMITTED Read uncommitted
ISOLATION_READ_COMMITTED Read committed ( Can solve dirty read problem)
ISOLATION_REPEATABLE_READ Repeatable read (can solve dirty read and non-repeatable read problem)
ISOLATION_SERIALIZABLE Serialization
Can be solved:
(2) Transaction propagation behavior
Transaction propagation behavior refers to What is involved is how transaction control should be performed when a business method is called by another business method.Key points:
(Whether it is read-only): It is recommended to set it to read-only when querying
(timeout time): The default value is -1, there is no timeout limit. If so, set it in seconds
You can simply understand the relationship between the three: the transaction manager performs transaction management by reading the transaction definition parameters, and then generates a series of transaction states.
Transaction control in Spring is mainly implemented through these three APIs
PlatformTransactionManageris responsible for the management of transactions. It is an interface and its subclasses are responsible for specific work
Defines some relevant parameters of the transaction
Represents a real-time status of the transaction runningUnderstand the relationship between the three:
performs transaction management by reading transaction definition parameters, and then generates a series of transaction status. 3. XML-based declarative transaction control [Key points]
Core business code (target object) (Who is the entry point?)
Transaction enhancement code (Spring Transaction manager has been provided)) (Who is notified?)
Aspect configuration (How to configure the aspect?) (Aspect = pointcut notification)
3.1 Quick Start
1.Introduce the tx namespace
2.Transaction manager notification configuration
3.Transaction manager AOP configuration
4. Test transaction control transfer business code
(1)Introduce tx namespace
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd ">
<!--事务管理器对象--> <!--<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean>--> // 通知增强 <tx:advice id="txAdvice" transaction-manager="transactionManager"> //定义事务的一些属性 * 表示当前任意名称的方法都走默认配置 <!-- name: 切点方法名称 isolation:事务的隔离级别 propagation:事务的传播行为 read-only:是否只读 timeout:超时时间 --> <tx:attributes> <tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" timeout="-1"/> //CRUD常用配置 <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="find*" read-only="true"/> <tx:method name="*"/> </tx:attributes> </tx:advice>
//aop配置:配置切面 <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.lagou.servlet.impl.AccountServiceImpl.*(..))"/> </aop:config>-->
##
name
:切点方法名称
isolation
:事务的隔离级别
propogation
:事务的传播行为
timeout
:超时时间
read-only
:是否只读
步骤:
修改service层,增加事务注解
修改spring核心配置文件,开启事务注解支持
@Service public class AccountServiceImpl implements AccountService { @Autowired private AccountDao accountDao; @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, timeout = -1, readOnly = false) @Override public void transfer(String outUser, String inUser, Double money) { accountDao.out(outUser, money); int i = 1 / 0; accountDao.in(inUser, money); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w2.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--省略之前datsSource、jdbcTemplate、组件扫描配置--> <!--事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!--事务的注解支持--> <tx:annotation-driven/> </beans
核心配置类:
@Configuration // 声明该类为核心配置类 @ComponentScan("com.lagou") // 包扫描 @Import(DataSourceConfig.class) //导入其他配置类 @EnableTransactionManagement //事务的注解驱动 public class SpringConfig { @Bean public JdbcTemplate getJdbcTemplate(@Autowired DataSource dataSource){ JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); return jdbcTemplate; } @Bean public PlatformTransactionManager getPlatformTransactionManager(@Autowired DataSource dataSource){ DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(dataSource); return dataSourceTransactionManager; } }
数据源配置类:
@PropertySource("classpath:jdbc.properties") //引入properties文件 public class DataSourceConfig { @Value("${jdbc.driverClassName}") private String driver; @Value("${jdbc.url}") private String url; @Value("${jdbc.username}") private String username; @Value("${jdbc.password}") private String password; @Bean //会把当前方法的返回值对象放进IOC容器中 public DataSource getDataSource(){ DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setDriverClassName(driver); druidDataSource.setUrl(url); druidDataSource.setUsername(username); druidDataSource.setPassword(password); return druidDataSource; } }
The above is the detailed content of What are the two types of transactions in Java Spring?. For more information, please follow other related articles on the PHP Chinese website!