美文网首页
jpa事务管理

jpa事务管理

作者: hemingkung | 来源:发表于2019-11-17 16:52 被阅读0次

1注解
2代码

package com.imooc.example.springdtx.web;

import com.imooc.example.springdtx.dao.CustomerRepository;
import com.imooc.example.springdtx.domain.Customer;
import com.imooc.example.springdtx.service.CustomerServiceTxInAnnotation;
import com.imooc.example.springdtx.service.CustomerServiceTxInCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by mavlarn on 2018/1/20.
 */
@RestController
@RequestMapping("/api/customer")
public class CustomerResource {
    private static final Logger LOG = LoggerFactory.getLogger(CustomerResource.class);

    @Autowired
    private CustomerServiceTxInAnnotation customerService;
    @Autowired
    private CustomerServiceTxInCode customerServiceInCode;
    @Autowired
    private CustomerRepository customerRepository;

    @PostMapping("/annotation")
    public Customer createInAnnotation(@RequestBody Customer customer) {
        LOG.info("CustomerResource create in annotation create customer:{}", customer.getUsername());
        return customerService.create(customer);
    }

    @PostMapping("/code")
    public Customer createInCode(@RequestBody Customer customer) {
        LOG.info("CustomerResource create in code create customer:{}", customer.getUsername());
        return customerServiceInCode.create(customer);
    }

    @GetMapping("")
    public List<Customer> getAll() {
        return customerRepository.findAll();
    }

}

1注解

package com.imooc.example.springdtx.service;

import com.imooc.example.springdtx.dao.CustomerRepository;
import com.imooc.example.springdtx.domain.Customer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by mavlarn on 2018/1/24.
 */
@Service
public class CustomerServiceTxInAnnotation {

    private static final Logger LOG = LoggerFactory.getLogger(CustomerServiceTxInAnnotation.class);

    @Autowired
    private CustomerRepository customerRepository;

    @Transactional
    public Customer create(Customer customer) {
        LOG.info("CustomerService In Annotation create customer:{}", customer.getUsername());
        if (customer.getId() != null) {
            throw new RuntimeException("用户已经存在");
        }
        customer.setUsername("Annotation:" + customer.getUsername());
        return customerRepository.save(customer);
    }
}

2代码

package com.imooc.example.springdtx.service;

import com.imooc.example.springdtx.dao.CustomerRepository;
import com.imooc.example.springdtx.domain.Customer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

/**
 * Created by mavlarn on 2018/1/24.
 */
@Service
public class CustomerServiceTxInCode {
    private static final Logger LOG = LoggerFactory.getLogger(CustomerServiceTxInCode.class);

    @Autowired
    private CustomerRepository customerRepository;
    @Autowired
    private PlatformTransactionManager transactionManager;

    public Customer create(Customer customer) {
        LOG.info("CustomerService In Code create customer:{}", customer.getUsername());
        if (customer.getId() != null) {
            throw new RuntimeException("用户已经存在");
        }
        DefaultTransactionDefinition def = new DefaultTransactionDefinition();
        def.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
        def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        def.setTimeout(15);
        TransactionStatus status = transactionManager.getTransaction(def);
        try {
            customer.setUsername("Code:" + customer.getUsername());
            customerRepository.save(customer);
            transactionManager.commit(status);
            return customer;
        } catch (Exception e) {
            transactionManager.rollback(status);
            throw e;
        }
    }
}


相关文章

  • [五]spring事务实例

    Spring事务实例 - JPA事务实例 代码方式,标签(注解)方式实现事务 JPA事务管理 使用H2数据库(支持...

  • jpa事务管理

    1注解2代码 1注解 2代码

  • spring-boot-jpa 使用

    Jpa是非常简单好用的ORM框架,基于Hibernate。参考:Spring Boot中的事务管理Spring D...

  • SpringBoot的事务管理你会了么?

    Springboot内部提供的事务管理器是根据autoconfigure来进行决定的。 比如当使用jpa的时候,也...

  • JPA和事务管理

    1 事务 1.1 事务管理方式 spring支持编程式事务管理和声明式事务管理两种方式。 编程式事务管理使用Tra...

  • Spring Boot & Spring Data &a

    :) 本篇会分别介绍 Spring Data ,JPA ,Spring Data JPA JPA简介 jpa全称是...

  • JPA工程搭建(入门篇)

    目录: 一、什么是JPA 二、JPA优势? 三、JPA简单工程搭建 一、JPA是什么? 用一句概括:JPA(JAV...

  • Spring-事务机制

    一、Spring事务 事务管理概述 Spring事务管理分为编程式事务管理和声明式事务管理两种 编程式事务:允许用...

  • Spring事务管理方式

    编程式事务管理声明式事务管理

  • 事务管理

    编程式事务管理: 声明式事务管理:

网友评论

      本文标题:jpa事务管理

      本文链接:https://www.haomeiwen.com/subject/jtnpictx.html