无法在Spring Boot应用程序中查找JNDI数据源 [英] Failed to look up JNDI DataSource In Spring Boot Application
问题描述
我已经完成了下面提到的使用外部tomcat的Spring Boot Application的JNDI数据源连接的配置.
I have done the bellow mentioned configuration for JNDI DataSource connection for Spring Boot Application that uses an external tomcat.
类似于Tomcat的server.xml配置
Tomcat's server.xml configuration like
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="jdbc/MyPostgresDB"
global="jdbc/MyPostgresDB"
auth="Container"
type="javax.sql.DataSource"
driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://localhost:5432/postgres"
username="postgres"
password="postgres"
maxActive="100"
maxIdle="20"
minIdle="5"
maxWait="10000"/>
Context.xml之类的
Context.xml like
<Context>
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<ResourceLink name="jdbc/MyPostgresDB"
global="jdbc/MyPostgresDB"
auth="Container"
type="javax.sql.DataSource" />
以正确的方式(如
spring.datasource.jndi-name=java:comp/env/jdbc/MyPostgres
更新了web.xml之类的
Updated the web.xml like
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<resource-ref>
<description>JNDI LookUp</description>
<res-ref-name>jdbc/MyPostgresDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<!-- <res-sharing-scope>Shareable</res-sharing-scope> -->
</resource-ref>
</web-app>
仍然收到
org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException: 无法使用名称查找JNDI数据源 'java:comp/env/jdbc/MyPostgres'
org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException: Failed to look up JNDI DataSource with name 'java:comp/env/jdbc/MyPostgres'
(并非在所有给出的位置都给出完整的Stacktrace)
(Full Stacktrace not given as every where it is given)
那是什么解决方案,或者为什么我会收到此异常.
So what is the solution or why I am getting this exception.
不要带有用于春季启动的嵌入式tomcat服务器的想法. 请帮助我.
Don't come with the idea of embedded tomcat server for spring boot. Please help me.
推荐答案
我们应该在server.xml的Resource标签中添加factory ="org.apache.tomcat.jdbc.pool.DataSourceFactory",并确保在扩展SpringBootServletInitializer类的类
we should add factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" in Resource tag of server.xml and make sure to set JndiDatasource in the class where you extends SpringBootServletInitializer class
有关详细答案,请参考这里
For elaborate answer please refer here
这篇关于无法在Spring Boot应用程序中查找JNDI数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!