dal层声明事务管理(applicationContext-dao.xml)
<
bean
id
=
"transactionManager"
class
=
"org.springframework.jdbc.datasource.DataSourceTransactionManager"
>
<
property
name
=
"dataSource"
ref
=
"dataSource"
/>
</
bean
>
|
在biz层的spring文件中增加tx事务拦截。(applicationContext-biz.xml)
xmlns 引入tx
schemaLocation定义
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
1. 申明式事务
定义事务拦截,默认jdk动态代理,只能基于接口。proxy-target-class="true" 为字节码织入
<
tx:annotation-driven
transaction-manager
=
"transactionManager"
proxy-target-class
=
"true"
/>
|
1.1. 代码示例
@Transactional
public
void
insert(Vo vo){
}
|
2. 编程式事务
dal层增加事务模板(applicationContext-dao.xml)
<
bean
id
=
"jdbcTemplate"
class
=
"org.springframework.jdbc.core.JdbcTemplate"
>
<
property
name
=
"dataSource"
ref
=
"dataSource"
/>
</
bean
>
|
2.1. 代码示例
@Autowired
private
TransactionTemplate transactionTemplate;
public
void
update(){
...
return
(Integer) transactionTemplate.execute(
new
TransactionCallback<Integer>() {
@Override
public
Integer doInTransaction(TransactionStatus status) {
return
incomeRecordDOMapper.updateRecord(conIncomeRecordDO, incomeRecordDO);
}
});