如何在Grails 3.x中配置JNDI数据源?

时间:2015-10-14 17:53:33

标签: grails datasource jndi

Grails 3(3.0.9)应用程序无法在TC服务器上启动,其中使用其每个JNDI数据源的另外2个Grails 2应用程序正常运行。

似乎在Grails 3中复制我的Grails 2 JNDI设置不起作用(我将其他所有内容保留为默认值,并为我的prod数据源设置指定了driverClassName,dbCreate和jndiName)。

应用程序直接通过JDBC连接到MySQL数据库没有问题。只有JNDI使其崩溃,并出现以下异常(片段)......

ERROR grails.boot.GrailsApp - Application startup failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManagerPostProcessor': Initialization of bean failed; nested exception is org.spr
ingframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property
'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Cannot resolve reference to bean 'dat
aSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Cannot r
esolve reference to bean 'dataSourceLazy' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with
name 'dataSourceLazy': Cannot resolve reference to bean 'dataSourceUnproxied' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreation
Exception: Error creating bean with name 'dataSourceUnproxied': Invocation of init method failed; nested exception is javax.naming.NoInitialContextException: Need to specify class
name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:547) ~[spring-beans-4.1.7.RELEASE.jar:4
.1.7.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) ~[spring-beans-4.1.7.RELEASE.jar:4.1
.7.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE
]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
        at org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors(PostProcessorRegistrationDelegate.java:199) ~[spring-context-4.1.7.RELEA
SE.jar:4.1.7.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:615) ~[spring-context-4.1.7.RELEASE.jar:4.1.7.R
ELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:465) ~[spring-context-4.1.7.RELEASE.jar:4.1.7.RELEASE]
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.2.5.RELEASE.jar:1.2.5.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686) ~[spring-boot-1.2.5.RELEASE.jar:1.2.5.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:320) ~[spring-boot-1.2.5.RELEASE.jar:1.2.5.RELEASE]
        at grails.boot.GrailsApp.run(GrailsApp.groovy:52) [grails-core-3.0.7.jar:3.0.7]
        at grails.boot.GrailsApp.run(GrailsApp.groovy:330) [grails-core-3.0.7.jar:3.0.7]
        at grails.boot.GrailsApp.run(GrailsApp.groovy:319) [grails-core-3.0.7.jar:3.0.7]
        at grails.boot.GrailsApp$run.call(Unknown Source) [grails-core-3.0.7.jar:3.0.7]
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48) [groovy-2.4.4.jar:2.4.4]
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113) [groovy-2.4.4.jar:2.4.4]
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133) [groovy-2.4.4.jar:2.4.4]
        at miron3.Application.main(Application.groovy:8) [main/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while sett
ing bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Cannot resolve refe
rence to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dat
aSource': Cannot resolve reference to bean 'dataSourceLazy' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error c
reating bean with name 'dataSourceLazy': Cannot resolve reference to bean 'dataSourceUnproxied' while setting constructor argument; nested exception is org.springframework.beans.fa
ctory.BeanCreationException: Error creating bean with name 'dataSourceUnproxied': Invocation of init method failed; nested exception is javax.naming.NoInitialContextException: Need
 to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEA
SE]
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108) ~[spring-beans-4.1.7.RELEASE.jar:4.1.
7.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1477) ~[spring-beans-4.1.7.RELEA
SE.jar:4.1.7.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1222) ~[spring-beans-4.1.7.RELEASE.jar:

2 个答案:

答案 0 :(得分:2)

你正确的上面没有工作。我按照以下步骤操作,现在正在为我工​​作。

  1. tomcat context.xml文件定义如下:

    Resource name="grailsDB"
    
  2. application.yml添加了这个生产数据源。

    production:
        dataSource:
            dbCreate: update
            jndiName: java:comp/env/grailsDB
    
  3. 根据https://grails.github.io/grails-doc/3.0.x/ref/Plug-ins/dataSource.html文档创建了grails-app / conf / DataSource.groovy文件。

    dataSource {
        jndiName = "java:comp/env/grailsDB"
    }
    
  4. 使用grails war命令创建war文件。

  5. 在tomcat中部署并通过添加删除样本数据对其进行测试。

    以下是我测试过的软件版本。

  6. APPLICATION STATUS
        Environment: production
        App profile: web
        App version: 0.1
        Grails version: 3.0.3
        Groovy version: 2.4.3
        JVM version: 1.8.0_65
    

    希望这有帮助。

    感谢 -SR

答案 1 :(得分:0)

application.yml 中,我用此值替换了 url ,它对我有用。你可以尝试一样。

QueueProcessor