使用JNDI在Spring Boot中配置多个DataSource

编程入门 行业动态 更新时间:2024-10-13 06:16:03
本文介绍了使用JNDI在Spring Boot中配置多个DataSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想使用您的Application Server内置功能管理多个 DataSource,并使用JNDI访问它。我正在使用带有Spring JPA数据的Spring启动。

I want to manage mutiple DataSource using your Application Servers built-in features and access it using JNDI. I am using Spring boot with Spring JPA data.

我可以为单个数据源配置application.properties: -

I am able to configure the application.properties for single datasource:-

spring.datasource.jndi-name=jdbc/customers

我在context.xml文件中的配置如下: -

And my configuration in context.xml file as below:-

<Resource name="jdbc/customer" auth="Container" type="javax.sql.DataSource" maxTotal="100" maxIdle="30" maxWaitMillis="10000" username="root" password="root" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/customer"/>

一切正常。

但是当我无法配置两个数据源。

But when I am unable to configure for two datasource.

我确信context.xml文件中的配置: -

I am sure on the configuration in context.xml file:-

<Resource name="jdbc/customer" auth="Container" type="javax.sql.DataSource" maxTotal="100" maxIdle="30" maxWaitMillis="10000" username="root" password="root" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/customer"/> <Resource name="jdbc/employee" auth="Container" type="javax.sql.DataSource" maxTotal="100" maxIdle="30" maxWaitMillis="10000" username="root" password="root" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/employee"/>

我对application.properties文件配置有疑问。

我尝试了以下选项但没有成功: -

I tried the below options with no success:-

spring.datasource.jndi-name=jdbc/customers,jdbc/employee

请告诉我有关Spring启动的任何详细信息使用JNDI获取多个数据源。我好几天都在寻找这种配置。任何形式的建议或评论都会受到高度赞赏。

Please let me know any details on Spring boot with JNDI for mutiple data source. I was looking for this configuration for days now. Any kind of suggestion or comments is hight appreciated.

第二次试用按 Spring Boot Documentation

spring.datasource.primary.jndi-name=jdbc/customer spring.datasource.secondary.jndi-name=jdbc/project

配置类。

@Bean @Primary @ConfigurationProperties(prefix="datasource.primary") public DataSource primaryDataSource() { return DataSourceBuilder.create().build(); } @Bean @ConfigurationProperties(prefix="datasource.secondary") public DataSource secondaryDataSource() { return DataSourceBuilder.create().build(); }

应用程序无法启动。虽然tomcat服务器正在启动。日志中不会打印错误。

The application does not get started. Though the tomcat server is getting started. No errors are printed in the log.

第三次审判: - 使用JndiObjectFactoryBean

我有以下application.properties

I have the below application.properties

spring.datasource.primary.expected-type=javax.sql.DataSource spring.datasource.primary.jndi-name=jdbc/customer spring.datasource.primary.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect spring.datasource.primary.jpa.show-sql=false spring.datasource.primary.jpa.hibernate.ddl-auto=validate spring.datasource.secondary.jndi-name=jdbc/employee spring.datasource.secondary.expected-type=javax.sql.DataSource spring.datasource.secondary.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect spring.datasource.secondary.jpa.show-sql=false spring.datasource.secondary.jpa.hibernate.ddl-auto=validate

以及下面的java配置: -

And the below java configuration:-

@Bean(destroyMethod="") @Primary @ConfigurationProperties(prefix="spring.datasource.primary") public FactoryBean primaryDataSource() { return new JndiObjectFactoryBean(); } @Bean(destroyMethod="") @ConfigurationProperties(prefix="spring.datasource.secondary") public FactoryBean secondaryDataSource() { return new JndiObjectFactoryBean(); }

但仍然收到错误: -

But still getting error:-

Related cause: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'primaryDataSource' defined in class path resource [com/web/initializer/MvcConfig.class]: Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name [jdbc/customer] is not bound in this Context. Unable to find [jdbc]. Related cause: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'secondaryDataSource' defined in class path resource [com/web/initializer/MvcConfig.class]: Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name [jdbc/employee] is not bound in this Context. Unable to find [jdbc]. at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:474) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686) at org.springframework.boot.SpringApplication.run(SpringApplication.java:320) at org.springframework.boot.context.web.SpringBootServletInitializer.run(SpringBootServletInitializer.java:117) at org.springframework.boot.context.web.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:108) at org.springframework.boot.context.web.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:68) at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:175)

更新: - 使用以下属性文件进行试用: -

Update:- Trial using the below properties file:-

spring.datasource.primary.expected-type=javax.sql.DataSource spring.datasource.primary.jndi-name=java:comp/env/jdbc/customer spring.datasource.secondary.jndi-name=java:comp/env/jdbc/employee spring.datasource.secondary.expected-type=javax.sql.DataSource spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect spring.jpa.show-sql=false spring.jpa.hibernate.ddl-auto=validate

它创建了客户架构中的所有表,但也未能尝试查找其他表。(来自第二个架构)

It creates all the tables in customer schema , but fails trying to find the other tables also.(from the second schema)

推荐答案

这是您的第三次试用的解决方案稍作修改。 考虑这个解决方案(Spring Boot 1.3.2):

This is the solution for your third trial a little bit modified. Consider this solution (Spring Boot 1.3.2):

application.properties文件:

application.properties file:

spring.datasource.primary.jndi-name=java:/comp/env/jdbc/SecurityDS spring.datasource.primary.driver-class-name=org.postgresql.Driver spring.datasource.secondary.jndi-name=java:/comp/env/jdbc/TmsDS spring.datasource.secondary.driver-class-name=org.postgresql.Driver spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL9Dialect spring.jpa.show-sql=false

配置:

@Configuration@ EnableConfigurationProperties public class AppConfig { @Bean@ ConfigurationProperties(prefix = "spring.datasource.primary") public JndiPropertyHolder primary() { return new JndiPropertyHolder(); } @Bean@ Primary public DataSource primaryDataSource() { JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup(); DataSource dataSource = dataSourceLookup.getDataSource(primary().getJndiName()); return dataSource; } @Bean@ ConfigurationProperties(prefix = "spring.datasource.secondary") public JndiPropertyHolder secondary() { return new JndiPropertyHolder(); } @Bean public DataSource secondaryDataSource() { JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup(); DataSource dataSource = dataSourceLookup.getDataSource(secondary().getJndiName()); return dataSource; } private static class JndiPropertyHolder { private String jndiName; public String getJndiName() { return jndiName; } public void setJndiName(String jndiName) { this.jndiName = jndiName; } } }

然后你可以按照指南 http:// docs .spring.io / spring-data / jpa / docs / 1.3.0.RELEASE / reference / html / jpa.repositories.html 将您的数据源与jpa存储库一起使用。

And then you can follow guide docs.spring.io/spring-data/jpa/docs/1.3.0.RELEASE/reference/html/jpa.repositories.html to use your datasources with jpa repositories.

更多推荐

使用JNDI在Spring Boot中配置多个DataSource

本文发布于:2023-10-26 18:24:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1530978.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   Spring   JNDI   DataSource   Boot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!