【问题标题】:Spring Boot - External Tomcat - JNDI Data SourceSpring Boot - 外部 Tomcat - JNDI 数据源
【发布时间】:2018-03-20 14:51:29
【问题描述】:

我有一个在外部 tomcat 服务器上运行良好的 spring 应用程序。现在我在DatabaseConfig java 文件中硬编码数据库配置。我想将此配置从我的项目移动到 Tomcat 服务器。 为此,我正在尝试通过JNDI 查找数据源,但无法成功。

这是我正在使用的代码。

DatabaseConfig.java

@Bean
public DataSource dataSource() {
    final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
    dsLookup.setResourceRef(true);
    DataSource dataSource = dsLookup.getDataSource("java:comp/env/jdbc/myDS");
    return dataSource;
}

server.xml Tomcat 服务器文件

<?xml version="1.0" encoding="UTF-8"?>

<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <GlobalNamingResources>
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />

    <Resource name="jdbc/myDS" auth="Container" type="javax.sql.DataSource"
                          username="root" password="Welcome1"
                          url="jdbc:mysql://localhost:3306/rts"
                          driverClassName="com.mysql.jdbc.Driver"
                          initialSize="5" maxWait="5000"
                          maxActive="120" maxIdle="5"
                          validationQuery="select 1"
                          poolPreparedStatements="true"/>  
  </GlobalNamingResources>
  <Service name="Catalina">
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
    <Engine name="Catalina" defaultHost="localhost">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>
</Server>

context.xml Tomcat 服务器文件

<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
    <ResourceLink name="jdbc/rtsDS"
              global="jdbc/rtsDS"
              type="javax.sql.DataSource" />
</Context>

我得到的错误是

org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException: Failed to look up JNDI DataSource with name 'java:comp/env/jdbc/rtsDS'; nested exception is javax.naming.NameNotFoundException: Name [jdbc/rtsDS] is not bound in this Context. Unable to find [jdbc].

【问题讨论】:

    标签: java maven spring-boot jdbc jndi


    【解决方案1】:

    看来我找到了解决办法。

    我不得不删除 Eclipse 上现有的 tomcat 实例并创建一个新实例。 在我创建新的 tomcat 实例后,jndi 查找能够找到数据源。

    【讨论】:

      猜你喜欢
      • 2015-12-11
      • 2021-01-12
      • 1970-01-01
      • 2014-04-01
      • 2020-03-06
      • 1970-01-01
      • 2017-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多