【问题标题】:Weblogic 10.3.6 and spring-web 4.0.3 issueWeblogic 10.3.6 和 spring-web 4.0.3 问题
【发布时间】:2019-12-17 02:15:52
【问题描述】:

我目前正在开发一个使用 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)

【问题讨论】:

标签: java spring jndi weblogic-10.x


【解决方案1】:

application.properties

     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);
        }

【讨论】:

    猜你喜欢
    • 2014-07-10
    • 1970-01-01
    • 1970-01-01
    • 2015-12-02
    • 2017-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-22
    相关资源
    最近更新 更多