How to get Database connection

(Deepak Kumar Purohit) #1

I am trying to store some additional details to H2 Db.
As per my understanding, Corda is using Hikari Connection pool. How to call or better to say how to get Db connection for my application.

Basically I want create Hibernate SessionFactory,Session object etc…, to persist my Domain objects.

Thanks for any help.

(Rick Parker) #2

Hi Deepak,

We haven’t progressed this area as much as we will do in the future, so support for this will improve. It should be possible with the existing code though. Please take a look at the class HibernateObserver which is part of our vault code for creating ORM’d copies of the states in the vault. There’s a method in there called makeSessionFactoryForSchema that utilises a connection provider that hooks into our underlying database connectivity to H2 (Hikari via Exposed). You will need to do quite a bit of work, but it should be possible to follow that pattern and get something working.

If you’re willing to give that a go, then we’ll do our best to help out here if and when you have further questions.

(Richard Green) #3

Just going to add something here in case others have difficulty with the h2 interface - ensure you are using the correct client version to match the server side api we have used. Currently it’s at 1.4.192 so any 1.4 should be ok, but if you are having connectivity issues, then check the version of com.h2database:h2 located in build.gradle in the node directory

(Deepak Kumar Purohit) #4

Hi Rick,

Thanks for responding. I tried Using HibernateObserver and makeSessionFactoryForSchema APIs but facing problem in instantiation the classes as they have some internal dependencies.

(Mike Hearn) #5

The Corda node provides regular JDBC access. If you want to write a program that stores data along-side the node data, setting up Hibernate in your own program and just pointing it at that JDBC URL should work. You don’t need to reuse the nodes internal connection pooling.

(Deepak Kumar Purohit) #6

Thanks Mike for responding.

I already tried that Plain JDBC stuff. But the problem with that is, it will store in some different location.
We want to store the data in respective nodes.
For that we need the DB URL , but is changing when ever we modify and piece of code and build it.
Any approach we can get the DB URL which is being used internally by Nodes.

Thank you.

(Rick Parker) #7

You can control the port that H2 exposes the node database on. It’s controlled via the node configuration file (node.conf) which you will find inside the node runtime directory. The dataSource.url setting has an option called AUTO_SERVER_PORT in the H2 URL. If that is set to zero it will use a random port. You can set it to a specific port number if you wish. How are you launching the node?

Where/when are you looking to do database access? From within the node or from outside?

(Deepak Kumar Purohit) #8

Thank you @parkri.
We are launching the nodes for IDEA for debugging purpose
Well we are trying get DB connection within the nodes to store that Node specific data within the node.

(Rick Parker) #9

And when do you want to get a database connection? In a CordaPluginRegistry service initialisation? Or in a flow?

(Deepak Kumar Purohit) #10

Thank You @parkri for responding

Basically I want to do something like this:

Node will be running on different machines. Before Using those Nodes I want to implement some authentications logic which will be validated form DB.
As nodes will be running on different physical machines, As per my understanding they should have different DB . So here I want to use the inbuilt DB of the nodes.

So for this I am creating DB connection from some Flow.

(Mike Hearn) #11

So you want to do custom RPC authentication? RPC is the primary and soon to be only way to tell a node what to do, as we’re splitting the HTTP REST server out into a separate process that communicates with the node via RPC. The RPC authentication/authorization system already allows you to restrict what flows a user can start, so, I think there should not be any need to look up user credentials from within a flow.

RPC auth is handled by a regular JAAS module. You can find our simple config-file driven implementation in the file ArtemisMessagingServer.kt, it is called NodeLoginModule:

It works by delegating to an implementation of RPCUserService:

For now you could patch the Corda source to use your own implementation. This is probably a better way to go than trying to do it in your own custom flows.

Support for better user authentication schemes is on our roadmap. If it’d unblock you and help you move faster, we can talk about the Corda team prioritising authentication upgrades to allow the use of LDAP, Active Directory and JDBC.