How can the Corda node be extended to work with databases other than H2?

爱⌒轻易说出口 提交于 2019-12-17 16:31:29

问题


I have run Corda with PostgreSQL. How can Corda be extended to work with other databases? Is it enough to add the database driver as a Gradle dependency and try connecting to other databases?


回答1:


Corda uses Hibernate 5.x with HikariCP JDBC connection pool.

All queries are generated by Hibernate, expect one query from the finance module (CashSelection uses a JDBC PreparedStatement with SQL constructs specific to each database vendor).

Configuring the database

At runtime, a node looks up the JDBC settings in its node.conf file. By default, it connects to the embedded H2 server (see the file /node/src/main/resources/reference.conf in the Corda source code).

To use Corda with other databases(*) or connect with non-default settings, the node.conf file needs to set the node's data source properties:

dataSourceProperties = {
    dataSourceClassName = "org.postgresql.ds.PGSimpleDataSource"
    dataSource.url = "jdbc:postgresql://[HOST]:[PORT]/postgres"
    dataSource.user = [USER]
    dataSource.password = [PASSWORD]
}
database = {
    transactionIsolationLevel = READ_COMMITTED
}
jarDirs = [PATH_TO_JDBC_DRIVER_DIR]

where dataSourceProperties are the typical JDBC settings, the database entry contains Hibernate specific settings, and jarDirs is a list of paths to the directories containing the driver (e.g. jarDirs = ['/Library/postgres-lib']).

Configuring the database in deployNodes

You can avoid configuring the database settings manually by passing the settings in deployNodes. For example:

node {
    name "O=Bank C,L=Tokyo,C=JP"
    p2pPort 10010
    webPort 10011
    rpcSettings {
        address("localhost:10036")
        adminAddress("localhost:10037")
    }
    cordapps = ["$project.group:finance:$corda_release_version"]
    rpcUsers = ext.rpcUsers
    extraConfig = [
        ‘dataSourceProperties.dataSource.url’ : ‘jdbc:postgresql://localhost:32774/postgres’,
        ‘dataSourceProperties.dataSourceClassName’ : ‘org.postgresql.ds.PGSimpleDataSource’,
        ‘dataSourceProperties.dataSource.user’ : ‘postgres’,
        ‘dataSourceProperties.dataSource.password’ : ‘postgres’,
        ‘jarDirs’ : [ ‘/Library/Java/postgres’ ]
    ]
}

Adding the driver

Once these settings have been added, the JDBC driver needs to be added to the location specified in extraConfig.jarDirs after the node has been deployed.

The alternative is to add the JDBC driver dependency in node/build.gradle (as it is done for PostgreSQL). Whenever a node is compiled/built, it will contain the driver out-of-the-box.

(*) Corda Open Source is shipped with the H2 file database, and has experimental support for Postgres and Sql Server. Corda Enterprise has wider tested support for other databases including Sql Server, AzureSQL, Postgres, and Oracle databases. Despite using Hibernate/JDBC, some queries may require modification in order to be compatible across all supported databases. This compatibility is supported by Corda Enterprise.




回答2:


Just to add to Joel's answer: the option schema = [SCHEMA] is not available in Corda Open Source, some drivers allow to set up the current schema in JDBC URL string. For Corda 3.X dataSourceProperties keys containing . should be wrapped in double quotes to properly override the default settings (described in https://github.com/corda/corda/issues/4037) e.g.

dataSourceProperties = {
    "dataSource.url" = "jdbc:postgresql://[HOST]:[PORT]/postgres"
    ...

or in deployNodes:

extraConfig = [
               'dataSourceProperties': [
                          '"dataSource.url"': 'jdbc:postgresql://localhost:5432/postgres',
                          ...  


来源:https://stackoverflow.com/questions/50232016/how-can-the-corda-node-be-extended-to-work-with-databases-other-than-h2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!