spring 配置只讀事務
我們之前的設置可以正常工作。 我們現在要做的是進一步發展,并配置兩個單獨的實體管理器,而不會影響我們之前實現的功能。
第一步是將默認實體管理器配置設置為主要配置。
這是第一步
package com.gkatzioura.springdatareadreplica.config; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; @Configuration public class PrimaryEntityManagerConfiguration { @Value ( "${spring.datasource.username}" ) private String username; @Value ( "${spring.datasource.password}" ) private String password; @Value ( "${spring.datasource.url}" ) private String url; @Bean @Primary public DataSource dataSource() throws Exception { return DataSourceBuilder.create() .url(url) .username(username) .password(password) .driverClassName( "org.postgresql.Driver" ) .build(); } @Bean @Primary public LocalContainerEntityManagerFactoryBean entityManagerFactory( EntityManagerFactoryBuilder builder, @Qualifier ( "dataSource" ) DataSource dataSource) { return builder.dataSource(dataSource) .packages( "com.gkatzioura.springdatareadreplica" ) .persistenceUnit( "main" ) .build(); } }
如果您使用此配置運行您的應用程序,它將像之前的應用程序一樣運行。
現在是時候配置只讀實體管理器了。
package com.gkatzioura.springdatareadreplica.config; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Primary; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; @Configuration public class ReadOnlyEntityManagerConfiguration { @Value ( "${spring.datasource.username}" ) private String username; @Value ( "${spring.datasource.password}" ) private String password; @Value ( "${spring.datasource.readUrl}" ) private String readUrl; @Bean public DataSource readDataSource() throws Exception { return DataSourceBuilder.create() .url(readUrl) .username(username) .password(password) .driverClassName( "org.postgresql.Driver" ) .build(); } @Bean public LocalContainerEntityManagerFactoryBean readEntityManagerFactory( EntityManagerFactoryBuilder builder, @Qualifier ( "readDataSource" ) DataSource dataSource) { return builder.dataSource(dataSource) .packages( "com.gkatzioura.springdatareadreplica" ) .persistenceUnit( "read" ) .build(); } }
另外,我將向控制器添加方法以保存模型。
package com.gkatzioura.springdatareadreplica.controller; import java.util.List; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; import com.gkatzioura.springdatareadreplica.entity.Employee; import com.gkatzioura.springdatareadreplica.repository.EmployeeRepository; @RestController public class EmployeeContoller { private final EmployeeRepository employeeRepository; public EmployeeContoller(EmployeeRepository employeeRepository) { this .employeeRepository = employeeRepository; } @GetMapping ( "/employee" ) public List<Employee> getEmployees() { return employeeRepository.findAll(); } @PostMapping ( "/employee" ) @ResponseStatus (HttpStatus.CREATED) @ResponseStatus (HttpStatus.CREATED) public void addEmployee( @RequestBody Employee employee) { employeeRepository.save(employee); } }
如果您確實嘗試使用控制器添加員工,然后查詢讀取的數據庫,您將看到根本沒有添加任何條目。
因此,我們已啟動并運行了主要的實體管理器,而我們還擁有了輔助的實體管理器。 第二個尚未使用。 下一個博客著重介紹使用輔助只讀實體管理器。
翻譯自: https://www.javacodegeeks.com/2019/10/read-replicas-and-spring-data-configuring-two-entity-managers.html
spring 配置只讀事務
總結
以上是生活随笔為你收集整理的spring 配置只读事务_只读副本和Spring Data第3部分:配置两个实体管理器的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。