SPRING-DATA-JPA connection Mycat to realize the application of read-write separation

Source: Internet
Author: User

Pick up

Master-slave replication

Read and write separation

This article describes the use of SPRING-DATA-JPA connection Mycat to implement the application of read-write separation.

Original address: SPRING-DATA-JPA connection Mycat Implementation of the application of read and write separation system environment spring-boot 1.4.3-release jdk1.8 get to the point APPLICATION.YML configuration file

This is very traditional, it is OK to specify MySQL and datasource.

In particular: Mycat andapplications are independent of each other, mycat MySQL cluster behind the application is equivalent to a separate MySQL server, so don't tangle with the application of what database connection tool

Spring:
  application:
    name:mycat-jpa
  DataSource:
    platform:mysql
    Type: Com.zaxxer.hikari.HikariDataSource
    driver-class-name:com.mysql.jdbc.driver
    url:jdbc:mysql://localhost :8066/mall?useunicode=true&characterencoding=utf-8&autoreconnect=true&failoverreadonly=false& Usessl=false
    username:root
    password:jiabin
  JPA:
    hibernate:
      ddl-auto:none
    Show-sql:true
Jpatransactionconfig class file

This configuration class is the focus.

You need to turn off the SPRING-DATA-JPA default transaction management mechanism when using MYCAT.
The reason for this is as follows: Mycat for queries that open transactions, inserts, and so on, will go to the main library SPRING-DATA-JPA the default transaction management mechanism performs read-only transactions on query operations, saying that read-only transactions are also transactions.

For the above two reasons. We have to use Enabledefaulttransactions = False to turn off the SPRING-DATA-JPA default transaction management mechanism.

Well, now that we've turned off the default transaction management mechanism, we have to use @transactional to turn on declarative transactions. Haha, that is to say, We need to implement transaction management with the hard coding method of adding @transactional annotations. See Salesmanservice

Package jpa.mycat;

Import org.springframework.context.annotation.Configuration;
Import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

/**
 * Created by Ssab on 17-1-4.
 * *
@EnableJpaRepositories (basepackages = "Jpa.mycat", Enabledefaulttransactions = False)
@Configuration Public
class Jpatransactionconfig {
}

Understanding the two configurations, the rest is simple. Salesman entity classes

Package jpa.mycat;

Import Org.hibernate.annotations.GenericGenerator;

Import javax.persistence.*;
 /** * Created by Ssab on 17-1-5. */@Entity @Table (name = "Salesman") public class salesman {@Id @GenericGenerator (name = "Idgen", strategy = "Increme NT ") @GeneratedValue (generator =" Idgen ") @Column (name =" id ") Private Long id;//salesman ID private String usernum;//worker Number private string truename;//real name private string address;//address private string avatarpicurl;//avatar picture address private Str ING telephone;//fixed Tel: Area code-number private string mobile;//mobile number private int disabled;//whether 0 No 1 is private string remark;//standby
  Note Private String appuserid;//standby userid field (field app) public Long getId () {return id;
  public void SetId (Long id) {this.id = ID;
  Public String Getusernum () {return usernum;
  } public void Setusernum (String usernum) {this.usernum = Usernum;
  Public String Gettruename () {return truename; } public void Settruename (String truename) {this.truename = Truename;
  Public String getaddress () {return address;
  public void setaddress (String address) {this.address = address;
  Public String Getavatarpicurl () {return avatarpicurl;
  } public void Setavatarpicurl (String avatarpicurl) {this.avatarpicurl = Avatarpicurl;
  Public String Gettelephone () {return telephone;
  } public void Settelephone (String telephone) {this.telephone = telephone;
  Public String Getmobile () {return mobile;
  public void Setmobile (String mobile) {this.mobile = mobile;
  public int getdisabled () {return disabled;
  The public void setdisabled (int disabled) {this.disabled = disabled;
  Public String Getremark () {return remark;
  } public void Setremark (String remark) {This.remark = remark;
  Public String Getappuserid () {return appuserid; } public void Setappuserid (String appuserid) {This.appuserid = AppuseRId; @Override public String toString () {return "salesman{" + "id=" + ID + ", usernum= '" + usernum + ' + ' + ', truename= ' + truename + ' + ' + ', address= ' + address + ' + ' + ', avatarpicurl= ' + Avatarpicurl + ' + ' + ', telephone= ' + telephone + ' \ ' + ', mobile= ' + mobile + ' \ ' + ', Disab
  Led= "+ disabled +", remark= ' + remark + ' \ ' + ', appuserid= ' "+ appuserid + ' + ' + '} '; }
}
salesmanrepository
Package jpa.mycat;

Import org.springframework.data.jpa.repository.JpaRepository;
Import Org.springframework.data.jpa.repository.Query;

Import java.util.List;

/**
 * Created by Ssab on 17-1-5.
 *
/public interface salesmanrepository extends Jparepository<salesman, long> {
  salesman save salesman Salesman);

  @Query (value = "SELECT * from salesman limit 0,18", Nativequery = True)
  list<salesman> findall ();
}
Salesmanservice
Package jpa.mycat;

Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.stereotype.Service;
Import org.springframework.transaction.annotation.Propagation;
Import org.springframework.transaction.annotation.Transactional;

Import java.util.List;

/**
 * Created by Ssab on 17-1-5.
 * *
@Service public
class Salesmanservice

  {@Autowired
  private salesmanrepository repository;

  @Transactional (propagation = propagation.required) public
  salesman Save (salesman salesman) {
    return Repository.save (salesman);
  }

  @Transactional (propagation = propagation.not_supported) public
  list<salesman> FindAll () {
    return Repository.findall ();
  }

Test salesmanservicetest
Package jpa.mycat;

Import Org.junit.Test;
Import Org.junit.runner.RunWith;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.boot.test.context.SpringBootTest;
Import Org.springframework.test.context.junit4.SpringRunner;

/**
 * Created by Ssab on 17-1-5.
 *
/@SpringBootTest @RunWith (springrunner.class) public
class Salesmanservicetest {
  @Autowired
  private salesmanservice service;

  @Test public
  void Testfindall () {
    service.findall (). ForEach (Salesman-> System.out.println ( Salesman.tostring ());
  }

  @Test public
  void Testsave () {
    salesman salesman = new Salesman ();

    Salesman.setusernum ("3333333");
    Salesman.settruename ("Ssab");
    Salesman.setaddress ("Laiwu, Shandong Province");
    Salesman.setmobile ("152222222");
    salesman.setdisabled (0);

    Service.save (salesman);
  }
run the Testfindall method to view the Mycat log

Look at the contents of the red box in the picture

All right, we're sure to go. from the library. run the Testsave method to view the Mycat log

From the contents of the red box in the figure, we found that the main library was gone. Summary

The focus of using SPRING-DATA-JPA connection Mycat is to turn off the default transaction management mechanism for SPRING-DATA-JPA and use @transactional annotations for declarative transaction management.

Just pay attention to this. Code

SPRING-BOOT-JPA connection Mycat realize read-write separation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.