gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:36:32 24 4
gpt4 key购买 nike

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

但是,如果我直接从 Eclipse 运行应用程序,则一切正常。我的项目是 Maven 项目,我只需右键单击该项目并选择 Run As -> Run On server

这是我的 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;
}

错误如下:

"Error occurred during deployment: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactoryBean' defined in class sunwell.pos.service.config.RootContextConfiguration: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean sunwell.pos.service.config.RootContextConfiguration.entityManagerFactoryBean()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springJpaDataSource' defined in class sunwell.pos.service.config.RootContextConfiguration: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource sunwell.pos.service.config.RootContextConfiguration.springJpaDataSource()] threw exception; nested exception is org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException: Failed to look up JNDI DataSource with name 'java:app/pos'; nested exception is org.springframework.jndi.TypeMismatchNamingException: Object of type [class com.sun.enterprise.naming.impl.SerialContext] available at JNDI location [java:app/pos] is not assignable to [javax.sql.DataSource]. Please see server.log for more details."

编辑:

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

所以底线是这个问题只发生在 Spring war 部署期间。解决方案是您需要在命名空间后面为 jndi 名称添加前缀 jdbc或其他什么。

最佳答案

解决了这个问题

在 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");

但是,我还是不明白为什么指定java:app/pos如果我从 Eclipse 运行它,则 glassfish-resources.xml 和 springJpaDataSource 都可以工作,但如果我真正部署它,则不行,而且在 JEE 应用程序中,我可以将 jndi-name 指定为 java:app/pos在 glassfish-resources.xml 中并在 persistence.xml 中引用它,如下所示 <jta-data-source>java:app/pos</jta-data-source>

关于java - Spring JNDIDataSourceLookup 无法在 Glassfish 中查找 JNDSI 数据源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49398268/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com