事务
事务异常
解决方法
一般将@transactional加在多次修改业务层的方法上
事务属性—回滚
事务属性—传播行为
记录修改事务的日志
@Transactional默认情况下是Required需要事务的,
因为delete中有一个事务了,
因此这个事务就会加到Log里的Transactional中去
因此程序中就只有一个事务
导致Log中的事务也会回滚,
最终删除部门和部门下的员工这个操作就没有保存下来
解决方法
小结
DeptLog
package com.itheima.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class DeptLog {
private Integer id;
private LocalDateTime createTime;
private String description;
}
DeptLogService
package com.itheima.service;
import com.itheima.pojo.DeptLog;
public interface DeptLogService {
void insert(DeptLog deptLog);
}
DeptLogServiceImpl
package com.itheima.service.impl;
import com.itheima.mapper.DeptLogMapper;
import com.itheima.pojo.DeptLog;
import com.itheima.service.DeptLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Service
public class DeptLogServiceImpl implements DeptLogService {
@Autowired
private DeptLogMapper deptLogMapper;
@Transactional(propagation = Propagation.REQUIRES_NEW)
@Override
public void insert(DeptLog deptLog) {
deptLogMapper.insert(deptLog);
}
}
DeptLogMapper
package com.itheima.mapper;
import com.itheima.pojo.DeptLog;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface DeptLogMapper {
@Insert("insert into dept_log(create_time,description) values(#{createTime}, #{description})")
void insert(DeptLog log);
}