Skip to Content
0
Aug 04, 2016 at 12:12 PM

Can't connect to HANA DB on HCP from Java application

313 Views

I'm trying to establish a connection to a HANA database from a Java application, both the database and the Java app are hosted on a trial HANA Cloud Platform environment, but I am getting this error:

2016 08 04 11:42:02

#+00

#ERROR

#com.sap.core.services.accessor.tomcat.support.DelegatingObjectFactory

#BC-NEO-RT-JAV

#anonymous

#Timer-5

#com.sap.core.services.accessor

#p**********trial

#connectivity

#web

#p**********trial

#Exception is thrown by facroty [com.sap.jpaas.service.persistence.core.JNDIDataSourceFactory@680452da] during retrieving object instance. Exception is: javax.naming.NamingException: Data source 'jdbc/default' not available.

I have set a database-level data source binding with the Java application:

Does anyone know why this happens?

Are there any necessary roles to add to the database user used to connect? Is this a limitation of the HCP trial account? Or is there anything wrong with my database connection code?

I have included parts of my code below:

InitialContext ctx = null;

DataSource ds = null;

Connection con = null;

//Establish connection with a HANA database using a JDBC DataSource

//DataSource is defined in web.xml

try {

ctx = new InitialContext();

ds = (DataSource) ctx.lookup("java:comp/env/jdbc/default");

con = ds.getConnection("********","********");

if (!con.isClosed()) {

String db_url = con.getMetaData().getURL();

String db_product = con.getMetaData().getDatabaseProductName();

String db_maxcon = con.getMetaData().getMaxConnections() + "";

System.out.println("db info: " + db_url + " " + db_product + " " + db_maxcon);

PreparedStatement query = con.prepareStatement(

"SELECT * FROM ********");

ResultSet rs = query.executeQuery();

System.out.println("Result: " + rs.toString());

}

} catch (NamingException e) {

System.out.println("Data source error");

} catch (SQLException e) {

System.out.println("SQL Error");

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

} finally {

if (con != null) {

try {con.close();}

catch (SQLException e) {e.printStackTrace();}

}

}

  • web.xml file:

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

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

id="WebApp_ID" version="2.5">

<display-name>Connectivity</display-name>

<welcome-file-list>

<welcome-file>ConnectivityServlet</welcome-file>

</welcome-file-list>

<servlet>

<description></description>

<display-name>ConnectivityServlet</display-name>

<servlet-name>ConnectivityServlet</servlet-name>

<servlet-class>********.ConnectivityServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>ConnectivityServlet</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

<resource-ref>

<res-ref-name>jdbc/default</res-ref-name>

<res-type>javax.sql.DataSource</res-type>

</resource-ref>

<resource-ref>

...

</resource-ref>

<resource-ref>

...

</resource-ref>

<resource-ref>

...

</resource-ref>

</web-app>

Thanks,

Jan M

Attachments