Details
Description
Adding
def ds = new org.apache.commons.dbcp.BasicDataSource(
driverClassName:'org.hsqldb.jdbcDriver',
url:'jdbc:hsqldb:mem:testDb',
username:'sa',
password:'')
grails.naming.entries = ['jdbc/myDataSource':ds]
to Config.groovy and accordingly changing DataSource.groovy to
dataSource
{ pooled = false dbCreate = "create-drop" // one of 'create', 'create-drop','update' jndiName = "java:comp/env/jdbc/myDataSource" }leads to the following output:
Running Grails application..
loading security config ...
Cannot create JDBC driver of class '' for connect URL 'null'
java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(DriverManager.java:279)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1143)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:113)
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:79)
at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:280)
at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:320)
at org.codehaus.groovy.grails.orm.hibernate.support.HibernateDialectDetectorFactoryBean.afterPropertiesSet(HibernateDialectDetectorFactoryBean.java:77)
[... and so on ...]
Looking at the tomcat plugin, I noticed, that grails.naming.entries is only used for picking up the basic name and type for the ContextResource instance. Where does the specific configuration get set, e.g. driverClassName and url?
My attached patch modifies the tomcat plugin to explicitly search for a list of properties that appear to be common for BasicDataSource configurations. Those properties are then copied over to the ContextResource instance.
The approach works for me, but it might not be correct or complete. I'm especially unsure whether the approach is generic enough...
I ran into this as well. Our style of development means we expect Tomcat to provide resources when running in-container, whether it's dev or not. Unfortunately the code in the current plugin doesn't seem to work.
I needed something a little more generic than the patch supplied, so have provided one that can define one or many arbitrary resources to be configured, not just DataSources. Seems to work OK for me. Patch attached (fix_tomcat_jndi.patch).
To use it, in Config.groovy you can do something like this to replicate the Tomcat 5.5 examples at http://tomcat.apache.org/tomcat-5.5-doc/jndi-resources-howto.html:
grails.naming.entries = [ "bean/MyBeanFactory": [ auth: "Container", type: "com.mycompany.MyBean", factory: "org.apache.naming.factory.BeanFactory", bar: "23" ], "jdbc/EmployeeDB": [ type: "javax.sql.DataSource", //required auth: "Container", // optional description: "Data source for Foo", //optional driverClassName: "org.hsql.jdbcDriver", url: "jdbc:HypersonicSQL:database", username: "dbusername", password: "dbpassword", maxActive: "8", maxIdle: "4" ], "mail/session": [ type: "javax.mail.Session, auth: "Container", "mail.smtp.host": "localhost" ] ]Hope this is useful