Spring Boot - 外部Tomcat - JNDI数据源

时间:2018-03-20 14:51:29

标签: java maven spring-boot jdbc jndi

我有一个在外部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;
}
Tomcat服务器的

server.xml 文件

<?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>
Tomcat服务器的

context.xml 文件

<?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].

1 个答案:

答案 0 :(得分:0)

看起来我找到了解决方案。

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