boot jndi数据源 spring_使用JNDI在Spring Boot中配置多个数据源

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

JPA数据的Spring Boot。

我能够为单个数据源配置application.properties:

spring.datasource.jndi-name=jdbc/customers

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

maxTotal="100" maxIdle="30" maxWaitMillis="10000"

username="root" password="root" driverClassName="com.mysql.jdbc.Driver"

url="jdbc:mysql://localhost:3306/customer"/>

一切正常。

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

我可以确定context.xml文件中的配置:

maxTotal="100" maxIdle="30" maxWaitMillis="10000"

username="root" password="root" driverClassName="com.mysql.jdbc.Driver"

url="jdbc:mysql://localhost:3306/customer"/>

maxTotal="100" maxIdle="30" maxWaitMillis="10000"

username="root" password="root" driverClassName="com.mysql.jdbc.Driver"

url="jdbc:mysql://localhost:3306/employee"/>

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

我尝试了以下选项,但均未成功:

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

请让我知道有关使用JNDI进行多个数据源的Spring引导的任何详细信息。我几天以来一直在寻找这种配置。

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服务器正在启动。日志中没有错误打印。

第三审:使用JndiObjectFactoryBean

我有以下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配置:

@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();

}

但仍然出现错误:

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)

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

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

它在客户模式中创建了所有表,但是也尝试从其他模式中查找其他表失败。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值