FreezY
FreezY

Reputation: 1671

Weblogic jndi NameNotFoundException occur with java config

I been searching again for this issue where I cannot locate the jndi database by using java config. Before this I use xml and its work perfectly but in java config it cause an issue;

Xml code:

     <!-- Jndi database connection -->
     <jee:jndi-lookup id="dbDataSource" jndi-name="${db.jndi}"
     resource-ref="true" />

     <beans:bean id="jdbcTemplate"
     class="org.springframework.jdbc.core.JdbcTemplate" >
     <beans:property name="dataSource" ref="dbDataSource"></beans:property>
     </beans:bean>

Java config now:

@Bean(name = "dbDataSource")
public DataSource dataSource(@Value("${db.jndi}") String jndiName) 
{
    JndiDataSourceLookup lookup = new JndiDataSourceLookup();
    return lookup.getDataSource(jndiName);
}

@Bean
public JdbcTemplate jdbcTemplate(DataSource ds) { 
    return new JdbcTemplate(ds);
}

Properties file:

db.jndi=jndi/myData

JNDI name in weblogic:

jndi/myData

After change to java config, sometimes the system can read the database but rarely occur, until I clean and restart my computer then it can find the database, but usually its always trigger:

javax.naming.NameNotFoundException: Unable to resolve 'jndi.myData'. Resolved 'jndi'; remaining name 'myData'

Why the application cannot find the database correctly? Thanks!!!

Upvotes: 4

Views: 8140

Answers (3)

Barış &#214;zdemir
Barış &#214;zdemir

Reputation: 419

I've had the same issue and I solved problem. I used to jndi datasource on weblogic. After I restart application, I notice my jndi datasource remove from Weblogic's JNDI Tree. Xml configuration works successfuly but java configuration don't work.

My old spring version: 4.1.6.RELEASE Upgrade to 4.3.9.RELEASE

Xml configuration like this;

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName">
              <value>${db-jndi.name}</value>
        </property>
</bean>

Java configuration like this;

@Bean(name = "dataSource")
public DataSource dataSource() throws IllegalArgumentException, NamingException 
{
    JndiTemplate jndiTemplate = new JndiTemplate();
    DataSource dataSource = (DataSource) jndiTemplate.lookup(env.getProperty("db-jndi.name"));
    logger.info("DataSource initialized in jndi ");          
    return dataSource;
}

Then i changed

@Bean(name = "dataSource")

to

@Bean(name = "dataSource", destroyMethod = "")

And it's works successfuly.

Upvotes: 2

Tiago Nobrega
Tiago Nobrega

Reputation: 535

I've had the same issue. If you're using 4.x version of spring that's probably the cause.

You should also check Weblogic's JNDI Tree. If your data source disapears from the tree after rebuilding the project, that's another symptom

If that's the case, what's happening is:

Your Datasource implements Closeable (and therefore AutoCloseable) and the context will always invoke the shutdown method regardless of your Bean definition

as seen here : SPR-12551: Document how to prevent a JNDI DataSource retrieved using JavaConfig to be removed on shutdown of the context

It's been marked as a documentation issue as this is the "expected" behaviour:

This issue was solely about documentation since we decided not to implement anything at the framework level

the solution, is to define the destroy method of the bean as empty, such as:

@Bean(name = "dbDataSource", destroyMethod="")
   public DataSource dataSource(@Value("${db.jndi}") String jndiName) 
{
JndiDataSourceLookup lookup = new JndiDataSourceLookup();
   return lookup.getDataSource(jndiName);
}

@Bean
public JdbcTemplate jdbcTemplate(DataSource ds) { 
   return new JdbcTemplate(ds);
}

This is described in this issue (SPR-13022:Destroy callback cannot be disabled for AutoCloseable beans) .

PS: By the way, it seems like on early 4.x version of spring you couldn't override this behaviour by assingning destroyMethod. It apears that this bug was fixed on version 4.2 RC1.

Upvotes: 7

BurakE
BurakE

Reputation: 1

It looks like your datasource hasn't been deployed. You should look for JNDI tree for the server you tried to deploy datasource. (https://docs.oracle.com/cd/E12839_01/apirefs.1111/e13952/taskhelp/jndi/ViewObjectsInTheJNDITree.html) If you don't see "jndi.myData" on JNDI tree, you can assume that your datasource haven't been deployed. So you can go to your datasource monitoring tab and test the datasource. (https://docs.oracle.com/cd/E17904_01/apirefs.1111/e13952/taskhelp/jdbc/jdbc_datasources/TestDataSources.html)

Upvotes: 0

Related Questions