user542103 user542103 - 21 days ago 6
Groovy Question

Grails 3 JNDI datasource for Tomcat, Weblogic, Glassfish - Vs Grails 2

I'm experimenting with migrating from Grails 2 to Grails 3.

In Grails 2, I used this as my JNDI name within datasource.groovy file, within the Production-env

jndiName = "${(System.getProperty('catalina.home') && (System.getProperty('java.class.path')).trim().toLowerCase().indexOf('tomcat') > 0 ) ? 'java:comp/env/' : ''}jdbc/myGrails"


I found that worked well for me for Glassfish, Weblogic, and Tomcat.

However, when i try this in Grails 3, within the application.yml:

jndiName: ${(System.getProperty('catalina.home') && (System.getProperty('java.class.path')).trim().toLowerCase().indexOf('tomcat') > 0 ) ? 'java:comp/env/' : ''}jdbc/myGrails


I get this error when running "grails run-app":

| Error Error occurred running Grails CLI: mapping values are not allowed here
in 'reader', line 123, column 169:
... mcat') > 0 ) ? 'java:comp/env/' : ''}jdbc/traxGrails
^
(Use --stacktrace to see the full trace)


If I do use this in Grails 3:

jndiName: java:/comp/env/jdbc/myGrails


Then it works fine when using Grails run-app, and also works fine to deploy the WAR to Tomcat.

Can someone help me in getting my "jndi expression" from Grails 2 to work in Grails 3?

Answer

It looks like you're trying to use groovy syntax in a yml file, which does not work. You can create an application.groovy file (which will be merged with the application.yml file) if you want to use groovy syntax.

Also take a look at the many ways spring-boot allows you to inject properties via env variables, properties, yml, etc. http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html