Search code examples
springmaventomcatdatasourcejndi

Maven tomcat:run plugin and datasource configuration via JNDI


I am trying to configure DB DataSource by using Spring JndiObjectFactoryBean, so, i have tomcat:run plugin for local test purposes and context.xml file located in src/test/resources/maven-tomcat-plugin/

<plugin>
  <groupId>org.apache.tomcat.maven</groupId>
  <artifactId>tomcat7-maven-plugin</artifactId>
  <version>2.2</version>
  <configuration>
    <contextFile>${basedir}/src/test/resources/maven-tomcat-plugin/context.xml</contextFile>
  </configuration>
</plugin>

Context.xml

<?xml version='1.0' encoding='utf-8'?>
<Context>
<ResourceLink name="jdbc/MyLocalDB" global="jdbc/TestDB" auth="Container" type="javax.sql.DataSource" />
<Resource name="jdbc/MyDB" global="jdbc/MyDB" auth="Container" type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver"
    url="jdbc" username="" password="" maxActive="100" maxIdle="20" minIdle="5"
    maxWait="10000" />
</Context>

but i have: Name [jdbc/TestDB] is not bound in this Context

It looks like contextPath gives nothing.

Does anyone have any idia how to solve it?


Solution

  • The <ResourceLink> element is intended to construct a link to a Global resource which should be declared under your server.xml file and which might look as below:

    <GlobalNamingResources>
      <Resource name="jdbc/DatabaseName" auth="Container" type="javax.sql.DataSource"
              username="dbUsername" password="dbPasswd"
              url="jdbc:postgresql://localhost/dbname"
              driverClassName="org.postgresql.Driver"
              initialSize="5" maxWait="5000"
              maxActive="120" maxIdle="5"
              validationQuery="select 1"
              poolPreparedStatements="true"/>
    </GlobalNamingResources/>
    

    Note that the name attribute must match the global attribute of your ResourceLink element.

    So either declare a matching global resource, or just remove the Resource Link and use the declared resources as you did.