JNDI datasource to oracle with grails 1.3

2019-08-04 01:54发布

I can't get my JNDI datasource running. Followed the official grails doc I set up a datasouce in Config.groovy:

grails.naming.entries = [
            "mydatasource": [
                type: "javax.sql.DataSource",
                auth: "Container", 
                description: "Development Datasource", 
                url: "jdbc:oracle:oci:@mydb",
                username: "user",
                password: "pass",
                maxActive: "8",
                maxIdle: "4"
            ]
        ]

So, my DataSource.groovy looks like:

pooled = false
jndiName = "mydatasource"

I tried for "mydatasource" several different notations like "jdbc/mydatasource" or "java:comp/env/jdbc/mydatasource".

With every configuration I receive this: "javax.naming.NameNotFoundException: Name ... is not bound in this Context".

Also when I set up a global resource in the server.xml of my tomcat 6, the deployed grails-war cannot find the JNDI resource.

Any ideas to get this stuff working?

Thx


Edit:

It works fine! Tomcat (version 6 in my case) adds automatically the prefix "java:comp/env" to your datasource jndi-name. So does the tomcat plugin in grails.

Grails Config.groovy (in my case for development environment):

grails.naming.entries = [
            "jdbc/mydb": [
                type: "javax.sql.DataSource",
                auth: "Container", 
                description: "Development Datasource", 
                            driverClassName: "oracle.jdbc.driver.OracleDriver",
                url: "jdbc:oracle:oci:@mydb",
                username: "user",
                password: "pass",
                maxActive: "8",
                maxIdle: "4"
            ]
        ]

In context.xml (in my case for production environment):

<Resource name="jdbc/mydb" auth="Container"
          type="javax.sql.DataSource" 
          driverClassName="oracle.jdbc.driver.OracleDriver"
          url="jdbc:oracle:oci:@mydb"
          username="user" password="pass" maxActive="50" maxIdle="10"
          maxWait="5000"/>

In DataSource.groovy

pooled = false
jndiName = "java:comp/env/jdbc/mydb"

Edit:

A weird thing if you use the datasource as a global resource. The configuration which works for me:

In server.xml:

<Resource name="java:comp/env/jdbc/mydb" auth="Container"
          type="javax.sql.DataSource" 
          driverClassName="oracle.jdbc.driver.OracleDriver"
          url="jdbc:oracle:oci:@mydb"
          username="user" password="pass" maxActive="50" maxIdle="10"
          maxWait="5000"/>

In context.xml

<ResourceLink name="jdbc/mydb" 
          global="java:comp/env/jdbc/mydb"
          type="javax.sql.DataSource" />

In DataSource.groovy:

pooled = false
jndiName = "java:comp/env/jdbc/mydb"

2条回答
做自己的国王
2楼-- · 2019-08-04 02:26

I had the same problem. I needed JNDI working. I went through the code and found the problem.

Solution: When you are using JNDI do not set the pooled value. I mean don't set it to either true or false. Don't give it any value. For example....

datasource(name: 'swapccpDb') 
{
  domainClasses([com.mycompany.rad.swapsccp.domain.SnapShot])
  dbCreate('update')
  dialect(org.hibernate.dialect.OracleDialect)
  jndiName("java:comp/env/jdbc/SnapShotDB")
  //pooled(true)
  services(['swapccpimport'])
  environments(['development'])
  hibernate 
  {
    cache
    {
      use_second_level_cache = true
      use_query_cache = true
      provider_class = 'net.sf.ehcache.hibernate.EhCacheProvider'
    }
  }
}

The problem is in the datasources 0.5 file DatasourcesGrailsPlugin.groovy file. the following lines are the problem.

if (ds.pooled) 
{
  dsBean.destroyMethod = 'close'
}

The solution (If someone wants to fix the plugin) is

if (ds.pooled && !ds.jndiName) 
{
  dsBean.destroyMethod = 'close'
}
查看更多
forever°为你锁心
3楼-- · 2019-08-04 02:50

The JNDI prefix is java:comp/env/ for Tomcat, so in your case it's

jndiName = "java:comp/env/mydatasource"

in DataSource.groovy.

For the reference: Grails Docs.


Edit: Your Config.groovy is also missing the driverClassName property. I think it is

driverClassName: "oracle.jdbc.driver.OracleDriver",
查看更多
登录 后发表回答