cancel
Showing results for 
Search instead for 
Did you mean: 

Is it possible to create JDBC Persistence with ongoing web-socket server?

0 Kudos

I am using web-socket to receive and send data continuously between server and the client. I am also trying to add some of the data into Hana Database and it cannot use http connection, because websocket is already using it.

I tried to use JDBC Persistence with my websocket application, but it doesn't find the dataSource

2019 03 18 13:49:35#+00#ERROR#java.lang.Throwable##anonymous#https-jsse-nio-8041-exec-5#na#a009b7635#websockettomcat8#web##na#na#na#na#java.lang.NullPointerException: while trying to invoke the method javax.sql.DataSource.getConnection() of a null object loaded from static field persistence.ws.TemperatureDAO.dataSource

I have been trying to get this work for 5 hours now and it almost seems like you can't use JDBC Persistence while having ongoing http websocket connection. I hope I am wrong and hopefully someone who is more familiar in this topic could help me out.

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

Are you doing this on Cloud Foundry or Neo?

0 Kudos

ivan.mirisola I am doing this on Neo environment.

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

It is my undertanding that websocket implementation and jdbc have no interconnection.

Could you please share more details on how you are trying to connect to your jdbc while implementing your websocket app? Which ORM library are you using - or are you simply trying to connect with string?

0 Kudos

ivan.mirisola WebSocket: I will be using is tomcat 8 server which will be running in SCP as Java Application. Inside the same Java application I am trying to insert some data into hana table via JDBC Persistence. There is no ORM library used, but instead javax.sql.DataSource to grab the location of Hana database.

After I deploy the application into SCP I have to use Data binding, so it knows where the hana database is located at. And then simple statement string to insert the datas into hanabase.

PreparedStatement pstmt = connection
                    .prepareStatement("INSERT INTO WS_DEMO.TEMPS (ID, TIMESTAMP, DEVICE_ID, NAME, TEMPERATURE) VALUES (?, ?, ?, ?, ?)");<br>

However it seems that it doesn't find the Hana Database even with databinding.

0 Kudos

I just realized that in the jdbc persistense demo servlet there is init method:

public void init() throws ServletException {
        try {
            InitialContext ctx = new InitialContext();
            DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/DefaultDB");
            personDAO = new PersonDAO(ds);
        } catch (SQLException e) {
            throw new ServletException(e);
        } catch (NamingException e) {
            throw new ServletException(e);
        }
    }

So this must be the key to get the right hana database accross the app. I tried this however but got this time different errors. We're close to the solution, I'll just need to figure out how to do this on tomcat server. Seems like tomcat uses different methods.

Part of the error log:

com.sap.cloud.runtime.kotyo.persistence.client.trace.TraceableBase$ProxyInvocationHandler.invoke(TraceableBase.java:87) |
2019 03 20 11:27:41#+00#ERROR#java.lang.Throwable##anonymous#https-jsse-nio-8041-exec-3#na#a009b7635#websockettempapp#web##na#na#na#na#	at com.sun.proxy.$Proxy13.executeUpdate(Unknown Source) |
2019 03 20 11:27:41#+00#ERROR#java.lang.Throwable##anonymous#https-jsse-nio-8041-exec-3#na#a009b7635#websockettempapp#web##na#na#na#na#	at com.sap.cloud.runtime.kotyo.persistence.client.trace.TraceablePreparedStatement$2.call(TraceablePreparedStatement.java:67) |
2019 03 20 11:27:41#+00#ERROR#java.lang.Throwable##anonymous#https-jsse-nio-8041-exec-3#na#a009b7635#websockettempapp#web##na#na#na#na#

By googling I didn't find any other projects without the servlet (web), so I hope this is duable.

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

Hi,

Please look into the samples directory also for the web.xml. You'll need to declare the JNDI settings. Also, you will need to create a database schema in SCP cockpit and assign that to your application. So it make the "default" lookup find the correct database. This is all explained in the samples directory - please read the file "readme.txt".

Regards,
Ivan

0 Kudos

ivan.mirisola Hey, I have declared JNDI settings and I read the "readme.txt" file. Currently the problem is bounding the database. In Servlet they're using init method to bound database, I tried the same thing in tomcat but unfortunately it didn't work. I am wondering do I need to use Servlet also in this case in order to get this work? Using Servlet would however use the http protocol and it would close my websocket connection, which is what I am trying to avoid. Regards. Mokhtar

RESOLVED

Thanks ivan.mirisola for you help, I was able to get this solved by copying the original demo project which was done with JDBC Persistence and then I implemented websocket server and removed servlet. I believe that I needed a library (com.sap.security.server.csi) and test folder which had some related informations to persistence. I found out later that there was also SAP Agreement license in pom.xml and integration tests, which I read that you need those for it also.

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

Cool! I'm glad you have finally made it work.

Accepted Solutions (0)

Answers (0)