Question

I've some problem to make work the JndiDataSourceLookup with tomcat...
I cloned a tomcat with all it's configuration and applications from a vm to another. The original installation works fine, but when I tried to start the new one it fails saying:

Caused by: javax.naming.NameNotFoundException: Name [jdbc/sipreDs] is not bound in this Context. Unable to find [jdbc].

So I tried to make some debug and write my factory method as:

@Bean
@SneakyThrows
public DataSource dataSource(){
    Context context = new InitialContext();
    NamingEnumeration<NameClassPair> names = context.list("java:comp/env/jdbc");
    System.out.println("========================================");
    System.out.println("           CONTEXT JNDI");
    System.out.println("========================================");
    while (names.hasMore()){
        NameClassPair name = names.next();
        System.out.println(name.getName());
    }
    System.out.println("=========================================");
    final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
    dsLookup.setResourceRef(true);
    return dsLookup.getDataSource("jdbc/sipreDs");
}

The output is:

========================================
       CONTEXT JNDI
========================================
sipreDs
=========================================

The datasource is define in the conf/context.xml file:

 <Resource name="jdbc/sipreDs" auth="Container" type="javax.sql.DataSource"
           maxActvive="100" maxIdle="30" maxWait="10000"
           username="sipre" password="sipre" driverClassName="org.gjt.mm.mysql.Driver"
           url="jdbc:mysql://localhost:3306/sipre"/>

Why it prints the datasource with the normal jndi but using spring throws an exception?

Was it helpful?

Solution

Try the following

Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
return (DataSource) envCtx.lookup("jdbc/sipreDs");

It seems that you are using jdbc in the lookup of the context and the datasource

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top