当对数据库的操作出现问题时保证回滚----事务管理

事务

事务的原理见事务

声明式事务

  • 通过XML配置,声明某方法的事务特征
  • 或通过注解,声明某方法的事务特征

代码

@Service
public class AlphaService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DiscussPostMapper discussPostMapper;
    
    // Propagation.REQUIRED : support a current transaction(outside transaction), create a new one if none exists.
    // Propagation.REQUIRES_NEW : create a new transaction and suspend the current transaction.
    // Propagation.NESTED : execute within a nested transaction if a current transaction exists,
  	@Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
    // when a new user signs up, the system will automatically post for him (newbie)
    public Object save1(){
        // add a new user
        User user = new User();
        user.setUsername("alpha");
        user.setSalt(CommunityUtil.generateUUID().substring(0, 5));
        user.setEmail("[email protected]");
        user.setPassword(CommunityUtil.md5(user.getSalt()));
        user.setHeaderUrl("http://image.nowcoder.com/head/99t.png");
        user.setCreateTime(new Date());
        userMapper.insertUser(user);

        // post
        DiscussPost post = new DiscussPost();
        post.setUserId(user.getId());
        post.setTitle("hello");
        post.setContent("newbie report");
        post.setCreateTime(new Date());
        discussPostMapper.insertDiscussPost(post);

        Integer.valueOf("abc");

        return "ok";
    }
}
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = MyCommunityApplication.class)
public class TransactionTest {

    @Autowired
    private AlphaService alphaService;

    @Test
    public void testSavel1(){
        Object obj = alphaService.save1();
        System.out.println(obj);
    }
}

可以发现数据库中并没有插入新用户

编程式事务

  • 通过Transaction Template管理事务
  • 通过它执行数据库的操作
public class AlphaService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DiscussPostMapper discussPostMapper;
    
    @Autowired
    private TransactionTemplate transactionTemplate;
    
	public Object save2(){
        transactionTemplate.setIsolationLevel(TransactionDefinition.ISOLATION_READ_UNCOMMITTED);
        transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);

        return transactionTemplate.execute(new TransactionCallback() {
            @Override
            public Object doInTransaction(TransactionStatus status){
                // add a new user
                User user = new User();
                user.setUsername("alpha");
                user.setSalt(CommunityUtil.generateUUID().substring(0, 5));
                user.setEmail("[email protected]");
                user.setPassword(CommunityUtil.md5(user.getSalt()));
                user.setHeaderUrl("http://image.nowcoder.com/head/99t.png");
                user.setCreateTime(new Date());
                userMapper.insertUser(user);

                // post
                DiscussPost post = new DiscussPost();
                post.setUserId(user.getId());
                post.setTitle("hello");
                post.setContent("newbie report");
                post.setCreateTime(new Date());
                discussPostMapper.insertDiscussPost(post);

                Integer.valueOf("abc");

                return "ok";
            }
        });
    }
}
 
  

                            
                        
                    
                    
                    

你可能感兴趣的:(javaweb项目,数据库,java,开发语言)