gpt4 book ai didi

java - Weblogic 10.3.6 和 spring-web 4.0.3 问题

转载 作者:行者123 更新时间:2023-11-30 12:03:52 34 4
gpt4 key购买 nike

我目前正在开发一个使用 spring (web, jdbc, context, orm) 4.0.3.RELEASE 的 Java 项目。我将其打包为 .war 并将其部署在 Jetty 9 上。现在我必须在 Weblogic 10.3.6 上部署我们的应用程序,我遇到了一些奇怪的问题:有时部署会失败,因为我无法获取我的 DataSource:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [it/my/server/spring/SpringConfiguration.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean it.my.server.spring.SpringConfiguration.entityManagerFactory()] threw exception; nested exception is java.lang.IllegalArgumentException: DataSource must not be null
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:597)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1094)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:989)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:973)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:750)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
...

奇怪的是,重新启动 Weblogic 可能会导致应用程序正常部署。并非总是一开始就重新启动,有时我必须重新启动两次或更多次。这是(相关部分)我尝试获取数据源的地方:

@Autowired
private Environment environment;

...

@Bean
private DataSource createWeblogicDatasource() throws NamingException {
DataSource dataSource = null;
Context ctx = null;
String contextFactory = environment.getProperty("jndiDriver");
String dsServer = environment.getProperty("dsServer");
String dsPort = environment.getProperty("dsPort");
String dsName = environment.getProperty("dsName");
String contextUrl = "t3://" + dsServer + ":" + dsPort;
Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY, contextFactory);
ht.put(Context.PROVIDER_URL, contextUrl);
ctx = new InitialContext(ht);
dataSource = (DataSource) ctx.lookup(dsName);
return dataSource;
}

你有什么提示吗?

更新再回到这个问题。深入挖掘,我发现有一个 jndi 名称查找异常导致 DataSource 为空:

javax.naming.NameNotFoundException: Unable to resolve 'dummy.myDB'. Resolved 'dummy' [Root exception is javax.naming.NameNotFoundException: Unable to resolve 'dummy.myDB'. Resolved 'dummy']; remaining name 'myDB'
at weblogic.rjvm.ResponseImpl.unmarshalReturn(ResponseImpl.java:234)
at weblogic.rmi.cluster.ClusterableRemoteRef.invoke(ClusterableRemoteRef.java:348)
at weblogic.rmi.cluster.ClusterableRemoteRef.invoke(ClusterableRemoteRef.java:259)
at weblogic.jndi.internal.ServerNamingNode_1035_WLStub.lookup(Unknown Source)
at weblogic.jndi.internal.WLContextImpl.lookup(WLContextImpl.java:423)
at weblogic.jndi.internal.WLContextImpl.lookup(WLContextImpl.java:411)
at javax.naming.InitialContext.lookup(Unknown Source)

最佳答案

应用程序属性

     spring.datasource.jndi-name: jdbc/DS0

JPA

if //// condition for tomcat 
DataSource dataSource = null;
Context ctx = null;
String contextFactory = environment.getProperty("jndiDriver");
String dsServer = environment.getProperty("dsServer");
String dsPort = environment.getProperty("dsPort");
String dsName = environment.getProperty("dsName");
String contextUrl = "t3://" + dsServer + ":" + dsPort;
Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY, contextFactory);
ht.put(Context.PROVIDER_URL, contextUrl);
ctx = new InitialContext(ht);
dataSource = (DataSource) ctx.lookup(dsName);
else //// weblogic
JndiDataSourceLookup jndiDataSourceLookup = new JndiDataSourceLookup();
jndiDataSourceLookup.setResourceRef(true);

try
{
if(environment.getRequiredProperty("spring.datasource.jndi-name") != null)
{
dataSource =jndiDataSourceLookup.getDataSource(environment.getRequiredProperty("spring.datasource.jndi-name"));
}
}
catch(Exception e)
{
System.out.println("Error "+e);
}

关于java - Weblogic 10.3.6 和 spring-web 4.0.3 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57428342/

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