In this blog, I'm going to show you, how to consume data from a MySQL-database running on-premise in a Java Application in SAP Cloud Platform (we will use neo). This will require using the Cloud Connector and one of it's recent features.
To make the example more realistic and less ugly to look at, we will display the data in a small UI5 App instead of looking at raw json data.
The SAP Cloud Connector is a great tool for consuming data from legacy systems and other on-premise applications in the cloud. Previously this required the application to use either the HTTP, RFC or LDAP protocol for data exchange. While these protocols (especially HTTP) cover a high percentage of use-cases, there are some applications which require other protocols. One example is connecting to a database, as many databases are using their own protocols, which are normally based on TCP. Since Cloud Connector version 188.8.131.52 TCP connections are also supported by the Connectivity Services.
As a proof-of-concept, I created a Java application which executes SQL queries using the MySQL Connector/J where the Java application is deployed in the cloud and the database runs locally. I thought of creating the same example except using PostgreSQL instead of MySQL, let me know in the comments, whether you're interested. For the application, I'm using the "world dataset" which is available for download in the Documentation of MySQL. I'm calculating the fraction of people living in the city compared to the whole population of a given country, the result is not very accurate (e.g. the fraction calculated for Singapore is greater than 1) as it's only a dataset meant for testing, but that's good enough for a POC.
The blog is split into three parts:
Technical Background you can skip this part if you're already familiar with TCP and SOCKS.
How to set up at home How to set up the Cloud Connector and MySQL, import the dataset and deploy the application.
Code Review briefly explaining the Java Code.
2. Technical Background
The Connectivity Services provides a SOCKS-Proxy which allows your application to connect to a TCP-based system using its virtual hostname and port.
Have you ever heard of "Port 80", "Port 8080", "Port 21", "Port 443", etc.? That's TCP.1
TCP or Transmission Control Protocol basically has four main tasks:
Guarantees the order of packages/bytes sent.
Automatic retransmission in case of package loss.
Flow control (controlling the speed of data transmission according to the available bandwidth and the ability of the receiver to process the data).
Delivering the data to the correct "port" (meaning the correct program and socket).
The TCP protocol is settled lower in the protocol stack than the other protocols supported by the Cloud Connector (Layer 4 of the OSI model). Actually the all the other protocols are based on TCP. This implies two things:
Starting with the good one: As almost any protocol used in business applications (without a proof, sorry) is based on TCP the Cloud Connector now supports (almost) any protocol, you'll ever want to use.
Now the downside: As the protocol operates on a lower level (possibly even forwarding encrypted data) it is not possible to provide the same fine granular access control as it's possible e.g. for HTTP (path) and RFC (function name). Which does NOT make it insecure to use, but - as stated - affects granularity. So be aware of that.
You possibly already came across this term in the proxy settings of your Brower, Operating System, IDE or EMail-Client. SOCKS is a proxy protocol based directly on TCP and supports proxying raw TCP connections (and more), which makes it a perfect fit for the Cloud Connector TCP use case. To get a more detailed overview of the protocol, I can recommend the Wikipedia article about SOCKS. We are using SOCKS5, so concentrate on that parts of the article, if you're going to read it.
3. How to set up at home
To set up my POC you need three things:
A MySQL Server with the sample data.
A Cloud Connector instance connected to your Cloud Platform Subaccount.
The Webapp deployed to your Cloud Platform Subaccount.
The next subchapters will help you set up everything. Each subchapter contains a quick summary as well as a more detailed video tutorial. If you already an advanced Cloud Platform / Cloud Connector user feel free to skip the videos.
The biggest class in the code is the SocketFactory. It might look a bit complex for a POC, but it's just a copy of the StandardSocketFactory provided by the MySQL Connector/J with a small change. This is necessary because the default implementation does a DNS lookup before connecting to the proxy instead of relying on the DNS resolver of the proxy (a feature introduced with SOCKS5) and fails in our case because virtual hosts are not real DNS names. There is already a confirmed bug report in the bug tracking to of MySQL (MySQL Bug #77924). Hopefully, this workaround can be removed soon.
The Driver is loaded in the DBConnection class. There is nothing special in the class, just a very basic JDBC driver instantiation, except for line 19 where the SocketFactory is configured.