2

I'm currently working on a Java project that uses spring (web, jdbc, context, orm) 4.0.3.RELEASE. I pack it as a .war and deploy it on Jetty 9. Now I have to deploy our app on a Weblogic 10.3.6 and I am facing some strange issue: sometimes deploy fails because I can't get my 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)
...

The weird part is that restarting Weblogic may cause application to deploy fine. Not always at first restart, sometimes I have to do it twice or more. Here is (the relevant part) where I try to get a DataSource:

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

Have you any hint?

Update Back on this issue again. Digging deeper I found there is a jndi name lookup exception that causes the DataSource to be null:

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)
Francesco
  • 1,742
  • 5
  • 44
  • 78

1 Answers1

0

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);
        }
Vikram R
  • 776
  • 3
  • 8
  • 26