Spring JNDIDataSourceLookup无法在Glassfish中查找JNDSI数据源?

时间:2018-03-21 04:36:00

标签: java spring jpa glassfish jndi

每当我尝试将Spring war应用程序部署到Glassfish服务器(使用Glassfish 4和5)时,我都会收到错误:

但是,如果我直接从Eclipse运行应用程序,一切都会好的。我的项目是Maven项目,我只需右键单击projeck并选择Run As - >在服务器上运行

这是我的glassfish-resources.xml,它位于WEB-INF文件夹中并且包含在部署中(我已经验证了)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
 <resources>
    <jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="mysql_pos_rootPool" non-transactional-connections="false" pool-resize-quantity="2" res-type="javax.sql.DataSource" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false">
        <property name="serverName" value="localhost"/>
        <property name="portNumber" value="3306"/>
        <property name="databaseName" value="pos"/>
        <property name="User" value="root"/>
        <property name="Password" value="root"/>
        <property name="URL" value="jdbc:mysql://localhost:3306/pos?zeroDateTimeBehavior=convertToNull"/>
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
    </jdbc-connection-pool>
    <jdbc-resource enabled="true" jndi-name="java:app/pos" object-type="user" pool-name="mysql_pos_rootPool"/>
</resources>

我的RootContextConfiguration类中的bean:

    @Bean
    public DataSource springJpaDataSource()
    {
        JndiDataSourceLookup lookup = new JndiDataSourceLookup();
        return lookup.getDataSource("java:app/pos");
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean()
    {
        Map<String, Object> properties = new Hashtable<>();
        properties.put("javax.persistence.schema-generation.database.action",
                "none");

        HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
        adapter.setDatabasePlatform("org.hibernate.dialect.MySQL5InnoDBDialect");

        LocalContainerEntityManagerFactoryBean factory =
                new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(adapter);
        factory.setDataSource(this.springJpaDataSource());
        factory.setPackagesToScan("my.package.entity");
        factory.setSharedCacheMode(SharedCacheMode.ENABLE_SELECTIVE);
        factory.setValidationMode(ValidationMode.NONE);
        factory.setJpaPropertyMap(properties);
        return factory;
    }

错误如下:

  

“部署期间发生错误:加载应用程序时出现异常:java.lang.IllegalStateException:ContainerBase.addChild:start:org.apache.catalina.LifecycleException:org.springframework.beans.factory.BeanCreationException:使用名称创建bean时出错类sunwell.pos.service.config.RootContextConfiguration中定义的'entityManagerFactoryBean':bean的实例化失败;嵌套异常是org.springframework.beans.factory.BeanDefinitionStoreException:工厂方法[public org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean sunwell.pos .service.config.RootContextConfiguration.entityManagerFactoryBean()]抛出异常;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'springJpaDataSource'的bean时出错在类sunwell.pos.service.config.RootContextConfiguration中定义:实例化bean失败;嵌套异常是org.springframework.beans.factory.BeanDefinitionStoreException:工厂方法[public jav ax.sql.DataSource sunwell.pos.service.config.RootContextConfiguration.springJpaDataSource()]抛出异常;嵌套异常是org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException:无法查找名为'java:app / pos'的JNDI DataSource;嵌套异常是org.springframework.jndi.TypeMismatchNamingException:JNDI位置[java:app / pos]上可用的[class com.sun.enterprise.naming.impl.SerialContext]类型的对象不能赋予[javax.sql.DataSource] 。有关详细信息,请参阅server.log。“

编辑:

我发现在部署期间,jndi名称必须以jdbc为前缀,或者命名空间后面的内容,例如:java:app/jdbc/pos而不只是java:app/pos,但我不确定为什么即使没有jdbc前缀我也可以使用eclipse运行它,在JEE应用程序中我也可以像<jta-data-source>java:app/pos</jta-data-source>一样在持久性中引用jndi名称。

所以底线是这个问题只发生在春季战争部署期间。解决方案是您需要在名称空间后面加上jdbc或其他内容的jndi名称前缀。

1 个答案:

答案 0 :(得分:0)

解决了这个

在glassfish-resources.xml中,

jndi-name="java:app/pos"更改为jndi-name="jdbc/pos"

在springJpaDataSource()方法中,

更改return lookup.getDataSource("java:app/pos");

return lookup.getDataSource("java:app/jdbc/pos");

但是,我仍然不明白为什么如果我从Eclipse运行它而在glassfish-resources.xml和springJpaDataSource中都指定java:app/pos但是如果我真的部署它,并且在JEE中也是如此我可以在glassfish-resources.xml中将jndi-name命名为java:app/pos,并在persistence.xml中引用它,如<jta-data-source>java:app/pos</jta-data-source>