先写一个最最简单的插入例子来探讨Spring事务监控.
model
@Data
public class User implements Serializable {
private long id;
private String name;
private int age;
}
dao(此处数据库有一个user表,3个字段,id自增)
@Mapper
public interface UserDao {
@Options(useGeneratedKeys = true, keyProperty = "id")
@Insert("insert into user (name,age) values (#{name},#{age})")
void insert(User user);
}
service
public interface UserService {
public void insert(User user);
}
@Transactional
@Service
public class UserServiceImpl implements UserService {
@Autowired
private ApplicationEventPublisher publisher;
@Autowired
private UserDao userDao;
@Override
public void insert(User user) {
userDao.insert(user);
//重点在于此处对事务进行侦听
publisher.publishEvent(user);
}
}
listener(对Spring事务侦听,并进行处理)
@Component
public class UserTransactionEventListener {
@TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT)
public void beforeCommit(PayloadApplicationEvent<User> event) {
System.out.println("before commit, id: " + event.getPayload().getId());
}
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
public void afterCommit(PayloadApplicationEvent<User> event) {
System.out.println("after commit, id: " + event.getPayload().getId());
}
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMPLETION)
public void afterCompletion(PayloadApplicationEvent<User> event) {
System.out.println("after completion, id: " + event.getPayload().getId());
}
@TransactionalEventListener(phase = TransactionPhase.AFTER_ROLLBACK)
public void afterRollback(PayloadApplicationEvent<User> event) {
System.out.println("after rollback, id: " + event.getPayload().getId());
}
}
controller
@RestController
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/save")
public Map<String,Object> save(@RequestBody User user) {
Map<String,Object> result = new HashMap();
userService.insert(user);
result.put("code","200");
result.put("body",user);
result.put("msg","success");
return result;
}
}
运行结果
{
"msg": "success",
"code": "200",
"body": {
"id": 1,
"name": "miky",
"age": 21
}
}
日志片段
2018-09-17 14:42:44.384 INFO 10696 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2018-09-17 14:42:44.387 INFO 10696 --- [ main] com.guanjian.TransactionApplication : Started TransactionApplication in 3.987 seconds (JVM running for 5.122)
2018-09-17 14:45:46.915 INFO 10696 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-09-17 14:45:46.915 INFO 10696 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2018-09-17 14:45:46.930 INFO 10696 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 15 ms
2018-09-17 14:45:47.047 INFO 10696 --- [nio-8080-exec-1] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} inited
before commit, id: 1
after commit, id: 1
after completion, id: 1
说明侦听成功,可以进行分布式事务的下一步处理,发送MQ消息等等.