微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

多数据库连接JPA

如何解决多数据库连接JPA

我正在尝试使用Spring Boot JPA连接两个数据库

Application.properties:

应用程序配置。

server.port=8102

# Hibernate configuration.
spring.jpa.show-sql = true
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
#spring.jpa.hibernate.ddl-auto = create-drop
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.sqlServer2012Dialect
app.datasource.cardHolder.url=jdbc:sqlserver://server:22;databaseName=DB1
app.datasource.cardHolder.username=name
app.datasource.cardHolder.password=psw
app.datasource.cardHolder.driverClassName=com.microsoft.sqlserver.jdbc.sqlServerDriver

server.path=F:\\

app.datasource.card.url=jdbc:sqlserver://server:22;databaseName=DB2
app.datasource.card.username=uname
app.datasource.card.password=psw
app.datasource.card.driverClassName=com.microsoft.sqlserver.jdbc.sqlServerDriver

spring.jpa.open-in-view=false

# Logging configuration.
logging.level.com.springboot.storedprocedure=DEBUG
logging.pattern.console= %d{yyyy-MM-dd HH:mm:ss} - %msg%n

com.app.configuration

CardConfig

@Configuration
@EnableJpaRepositories(basePackages = "com.app.repository",entityManagerFactoryRef = "cardEntityManagerFactory",transactionManagerRef= "cardTransactionManager")
public class CardDataSourceConfiguration {

    @Bean
    @Primary
    @ConfigurationProperties("app.datasource.card")
    public DataSourceProperties cardDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Primary
    @Bean
    @ConfigurationProperties("app.datasource.card.configuration")
    public DataSource cardDataSource() {
        return cardDataSourceProperties().initializeDataSourceBuilder()
                .type(BasicDataSource.class).build();
    }

//    @PersistenceContext(unitName = "first")
    @Primary
    @Bean(name = "cardEntityManagerFactory")
    public LocalContainerEntityManagerfactorybean cardEntityManagerFactory(
            EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(cardDataSource())
                .packages(Card.class)
                .build();
    }

    @Primary
    @Bean
    public PlatformTransactionManager cardTransactionManager(
            final @Qualifier("cardEntityManagerFactory") LocalContainerEntityManagerfactorybean cardEntityManagerFactory) {
        return new JpaTransactionManager(cardEntityManagerFactory.getobject());
    }

}

CardHolder:

@Configuration
@EnableJpaRepositories(basePackages = "com.app.repository2",entityManagerFactoryRef = "cardHolderEntityManagerFactory",transactionManagerRef= "cardHolderTransactionManager")
public class CardHolderDataSourceConfiguration {

    @Bean
    @ConfigurationProperties("app.datasource.cardholder")
    public DataSourceProperties cardHolderDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @ConfigurationProperties("app.datasource.cardholder.configuration")
    public DataSource cardholderDataSource() {
        return cardHolderDataSourceProperties().initializeDataSourceBuilder()
                .type(BasicDataSource.class).build();
    }
    
//    @PersistenceContext(unitName = "second")
    @Bean(name = "cardHolderEntityManagerFactory")
    public LocalContainerEntityManagerfactorybean cardHolderEntityManagerFactory(
            EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(cardholderDataSource())
                .packages(CardHolder.class)
                .build();
    }

    @Bean
    public PlatformTransactionManager cardHolderTransactionManager(
            final @Qualifier("cardHolderEntityManagerFactory") LocalContainerEntityManagerfactorybean cardHolderEntityManagerFactory) {
        return new JpaTransactionManager(cardHolderEntityManagerFactory.getobject());
    }
}

CardConfi正确设置了JPARepository并基于DB2返回数据,但是与我在CardHolderRepository中添加Entitymanger的方式相同,如下所示,

@Entity
@NamedStoredProcedureQueries(value= {
        @NamedStoredProcedureQuery(name= "callSP",procedureName= "cardmssp",parameters= {
                @StoredProcedureParameter(mode= ParameterMode.IN,name= "Name",type= String.class)
        })
})

public class CardHolder implements Serializable {

 //getter setter
}

com.app.repository2

@Repository
public class CardHolderDao {
    
    @Autowired
    SPRepository spRep;
    @PersistenceContext
    private EntityManager em;

@SuppressWarnings("unchecked")
    public List<CardHolder> gecardtHolderList (String input) {
     
     return em.createNamedStoredProcedureQuery("cardmssp").setParameter("Name",input).getResultList();
}

}

com.app.repository2

public interface SPRepository extends JpaRepository<CardHolder,Long>,JpaSpecificationExecutor<CardHolder>{

    
}

问题是EntityManager em始终指向DB2,而不是DB1。我在哪里做错了?

解决方法

您应该设置EntityManagerFactoryBuilder#persistenceUnit

@Bean(name = "cardHolderEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean cardHolderEntityManagerFactory(
    EntityManagerFactoryBuilder builder) {
    return builder
           .dataSource(cardholderDataSource())
           .packages(CardHolder.class)
           .persistenceUnit("cardHolderEntityManager") // !!!
           .build();
}

然后

@PersistenceContext(unitName = "cardHolderEntityManager")
EntityManager em;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。