cancel
Showing results for 
Search instead for 
Did you mean: 

Can't connect to HANA database inside SCP using Java.

0 Kudos

Dears,

I've spent my whole day trying to find out the root cause of an issue, and I truly believe you can help me on it!

I have a Java application which uses Spring and Hibernate to connect to a HANA database. I'm running it inside SCP, and have configured a binding of my application to my database:

This is how I have configured the JNDI lookup inside my application:

Spring configuration file:

@Profile({"dev","qa","prod"})

@Bean@Resource(name = "jdbc/DefaultDB")

public DataSource dataSource() throws GeneralException { 
  try {      
       final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();           dsLookup.setResourceRef(true);      
       DataSource dataSource = dsLookup.getDataSource("java:comp/env/jdbc/DefaultDB");      
       return dataSource;   
      } catch (Exception e) {      
              String errorDetails = "Error loading datasource (dev,qa,prod profile) - " + SpringConfiguration.class.getName();      
               logger.error(errorDetails, e); 
                throw new GeneralException(errorDetails, e);  
 }}

src/webapp/WEB-INF/web.xml:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://xmlns.jcp.org/xml/ns/javaee"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"id="WebApp_ID"; version="3.1">   

 <resource-ref>        
    <res-ref-name>jdbc/DefaultDB </res-ref-name>        
    <res-type>javax.sql.DataSource </res-type>   
 </resource-ref>

</web-app>

src/main/resources/META_INF/context.xml:

<?xml version="1.0" encoding="UTF-8"?>

<Context  path="">    

<Resource name="jdbc/DefaultDB"auth="Container"type="javax.sql.DataSource"factory="com.sap.jpaas.service.persistence.core.JNDIDataSourceFactory"/>    

<ResourceLink name="jdbc/DefaultDB"global="jdbc/DefaultDB"type="javax.sql.DataSource" />

</Context>

When I try to run it on SCP, i keep getting this error:

org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException: Failed to look up JNDI DataSource with name 'java:comp/env/jdbc/DefaultDB'; nested exception is javax.naming.NameNotFoundException: Name [jdbc/DefaultDB] is not bound in this Context. Unable to find [jdbc].

Do someone have some idea of what might be happening? I'm really running out of options here.

Accepted Solutions (0)

Answers (1)

Answers (1)

Ivan-Mirisola
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi Victor,

Have you created the binding with the application runnning? If so, you have to restart the application so it can pick the binding during startup. Otherwise, the binding wont be available.

Please check your code with the following reference application.

I've just tested the sample above by replacing the postgresql driver dependency with the following in my pom.xml:

<dependency>
  <groupId>com.sap.cloud.db.jdbc</groupId>
  <artifactId>ngdbc</artifactId>
  <version>2.3.48</version>
</dependency>

I also renamed the application-neo.properties with application.properties so it picks the correct neo profile.

The app now opens correctly after deploying it, binding to my hana MDC and restarting it.

2018 08 31 19:30:45#+00#INFO#com.sap.hana.cloud.samples.springboot.NeoConfig##anonymous#localhost-startStop-1#na#XXXXXXXXXXXtrial#root#web##na#na#na#na#
Driver Name: HDB
Driver Version: 2.3.48-9bf9583a293e9fb8015c75991bf33038ae4f6bab
URL: jdbc:sap://xxxxxxxx.nwtrial.od.sap.biz:XXXXXX
DB Name: HDB
Username: SYSTEM

The main page shows:

Regards,
Ivan