Search code examples
herokudatasourcejndi

Configure datasource in Heroku deployment with JNDI lookup


I am currently using JNDI lookup to get my datasource from Tomcat's context.xml for either MySQL or PostgreSQL. My context.xml looks like this:

<Resource name="jdbc/MyDataSource"
          auth="Container"
          type="javax.sql.DataSource"
          username="user"
          password="pass"
          driverClassName="org.postgresql.Driver"
          url="jdbc:postgresql://servername:5432/database"
          maxActive="20"
          maxIdle="10"
          validationQuery="Select 1"
/>

However, one of my clients is using Heroku for deployment and I am wondering how I can set my datasource there and how to read it from my Java web app using JNDI lookup. In Heroku I am using the heroku-deploy plugin with Tomcat


Solution

  • You can pass the context.xml file to the --context-xml option of Webapp-runner, which is used by heroku-deploy. To do so, set a config var like this:

    $ heroku config:set WEBAPP_RUNNER_OPTS="--context-xml context.xml"
    

    Then when you deploy, include the context.xml file like this:

    $ heroku deploy:war --war app.war --includes context.xml
    

    This is described in detail in Configuring WAR Deployment with the Heroku Toolbelt